我正在尝试使用awk.
基本上,问题是为什么这个有效:
awk -F';' '{print $3 ";" $4 ";" }' file
Run Code Online (Sandbox Code Playgroud)
但这不会:
awk -F';' '{print '`echo '$3 ";" $4 ";"'`' }' file
Run Code Online (Sandbox Code Playgroud)
我的最终目标是将上述内容封装在一个命令中,以便我能够做到
my_cmd ';' 3 6 7 8 file(s)
Run Code Online (Sandbox Code Playgroud)
它应该显示#3,6,7,8由;from分隔的字段file(s)。
编辑我的帖子: 我发现我的问题是 echo 当然插入了一个新的行字符,这会导致 awk 出现问题 :o) \c 解决了这个问题,还有一些 \" 转义必须做(见下文)。
awk -F';' '{print '"`echo '$3 \";\" $4 \";\"\c'`"' }' file(s)
Run Code Online (Sandbox Code Playgroud)
现在我只剩下用命令来更改它,该命令将动态生成一个字符串,如
$2 ";' $5 ";' $6 ";' $9 ";"(数字和字段应该是输入)等,它应该在'{print '和' }'
感谢 cbuckley,我发现了我的单行命令:(问题已解决)。
cut -d"$1" -f `shift; echo $* | sed 's/[^ 0-9]\{1,\}.*$//;s/[ ]$//;s/[ ]\{1,\}/,/g'` `shift; printf "%s\n" $(echo $*) | grep -v '^[0-9]$'`
Run Code Online (Sandbox Code Playgroud)
这里 $* 是输入参数,如果上面是名为 say filterc 的 .rc 文件中的别名或函数,那么 Synopsys 将是:
filterc delimiter column1 [column2 [column3...]] file1 [file2 [file3...]]
Run Code Online (Sandbox Code Playgroud)
在哪里:
分隔符 - 仅一个字符
column1..n - 代表列的数字
file1..n - 要过滤的文件,这里假设文件的名称不会仅来自数字,并且所有文件的格式都相同。
您的命令听起来非常类似于cut:
cut -d ';' -f 1,3 <<EOT
one;two;three
foo;bar;quux
EOT
one;three
foo;quux
cut -d '-' -f 2,4 <<EOT
one-two-three-four
five-six-seven-eight
EOT
two-four
six-eight
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
382 次 |
| 最近记录: |