ksh 字符串操作 $#@!?

发布于 2024-11-03 14:41:03 字数 928 浏览 3 评论 0原文

所以我有一个包含这种内容的假脱机文件。

SQL> select file_name from dev_files;

FILE_NAME
------------------------------------------------------------------
file1.txt
file2.doc
file3.pdf
total.xls

4 rows selected.

SQL> spool off

我正在编写一个 ksh 脚本来将这些文件加载​​到 ftp 服务器中并更新日志文件。我被困住了。这是经过多次尝试后我的糟糕代码的一部分。

dump="spoolfile.txt"

while read line;
do

if [[`expr match "$line" 'SQL'` !=3]] && [[`expr match "$line" 'FILE_NAME'` !=9]] && [[`expr match "$line" '---------'` !=9]]
then
    ftp -inv $tgt_server <<EOT
    quote user $uname
    quote password $pword
    mput $src_path/$line
    quit
    EOT

    echo "sent $line" >> sent_files.log
fi
done < $dump

我如何确保“未选择任何行”并说“选择了 4 行”。没读过吗?可以是任意数字,而不是 4 对应的文件数。在没有文件的情况下,假脱机文件如下所示。这 '。'也失踪了。

SQL> select file_name from dev_files;

no rows selected

SQL> spool off

So I have this spool file having this kind of content.

SQL> select file_name from dev_files;

FILE_NAME
------------------------------------------------------------------
file1.txt
file2.doc
file3.pdf
total.xls

4 rows selected.

SQL> spool off

I am writing a ksh script to load these files into a ftp server and update a log file. and I am stuck up bigtime. Here is the portion of my poor code after many tries.

dump="spoolfile.txt"

while read line;
do

if [[`expr match "$line" 'SQL'` !=3]] && [[`expr match "$line" 'FILE_NAME'` !=9]] && [[`expr match "$line" '---------'` !=9]]
then
    ftp -inv $tgt_server <<EOT
    quote user $uname
    quote password $pword
    mput $src_path/$line
    quit
    EOT

    echo "sent $line" >> sent_files.log
fi
done < $dump

how do i ensure that "no rows selected" and say "4 rows selected." are not read? there can be any number instead of 4 corresponding to number of files. In the case of no files the spool file looks like this. the '.' is also missing.

SQL> select file_name from dev_files;

no rows selected

SQL> spool off

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

a√萤火虫的光℡ 2024-11-10 14:41:03

这是一个更简单的解决方案:不要试图找到要忽略的行,而是标记要处理的行:

select concat("JHFDGFSH ",file_name) from dev_files

现在您需要做的就是忽略任何不以 JHFDGFSH 开头的行。

[编辑]如果您不能这样做:当您遇到只有 ---- 的行时开始阅读,并在第一个空行处停止。除非 Oracle 开始分页,否则这应该有效。

并向上游提出一个问题,告诉他们他们的界面很脆弱,并且最终会崩溃。

如果没问题,那么当它出现故障并且生产中断时,这不是你的问题,也不是你的错。天数;只需展示问题并说“看到了吗?你想要这样。”

此外,KSH 可能不是正确的工具;看看awk。这应该可行:

/^---+/,/^[ \t]*$/ { print; }

如果您不能使用 awk,那么将每一行视为文件名并忽略那些不存在的行怎么样?

只要没有人创建名为 FILE_NAME 的文件,选择 4 行。SQL> 即可。 spool off,只要确保正确引用所有字符串,就应该可以找到。

Here is a much more simple soltion: Instead of trying to find lines to ignore, mark the lines to process:

select concat("JHFDGFSH ",file_name) from dev_files

Now all you need to do is to ignore any line that doesn't start with JHFDGFSH.

[EDIT] If you can't do that: Start reading when you hit a line that's only ---- and stop at the first empty line. That should work unless Oracle starts to page.

And open an issue for upstream to tell them that their interface is brittle and that it will break eventually.

If that's OK, it's not your problem and not your fault when it breaks and production is down for a couple of days; just show the issue and say "see? You wanted it this way."

Also KSH is probably not the right tool; have a look at awk. This should work:

/^---+/,/^[ \t]*$/ { print; }

If you can't use awk, how about treating every line as a file name and ignore those which don't exist?

As long as no one creates a file named FILE_NAME, 4 rows selected. or SQL> spool off, you should be find as long as you make sure you properly quote all strings.

烟燃烟灭 2024-11-10 14:41:03

我同意 SQLPlus 中关于“关闭标题”等的大多数评论,

但这很简单;-)

while read line;
do
   case ${line} in 
     -------* ) continue ;;
     FILE_NAME ) continue ;;
     SQL[>] ) continue ;;
     *rows\ selected ) continue ;;
   esac 

   # with enough case match targets, you can probably eliminate
   # the if/fi completely. Good luck.
   if [[`expr match "$line" 'SQL'` !=3]] .....
   ftp ...
   fi
done < $dump

您必须尝试在 case 语句中放入哪些值才能完全清理它。

另请注意,对于任何不寻常的字符,例如“>”,最好将它们作为字符类 [>] 包含在内(同样需要您进行一些实验)

最后,请注意如果要在大小写匹配目标中包含空格字符,请用双引号 (") 括住短语,或者转义每个 WS 字符,例如 '\'。

I agree with most of the comments about 'turn off headings' etc in the SQLPlus,

but this is Easy Peasy ;-)

while read line;
do
   case ${line} in 
     -------* ) continue ;;
     FILE_NAME ) continue ;;
     SQL[>] ) continue ;;
     *rows\ selected ) continue ;;
   esac 

   # with enough case match targets, you can probably eliminate
   # the if/fi completely. Good luck.
   if [[`expr match "$line" 'SQL'` !=3]] .....
   ftp ...
   fi
done < $dump

You'll have to experiment with what values to put in the case statement to clean it up completely.

Also note that for any unusual characters, like '>', it is probably better to include them as a character class [>] (again requiring some experimentation on your part)

And finally, note that if you want to include white-space chars in the case match targets, either surround the phrase in dbl-quotes (") or escape each WS char like '\'.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文