我有一个脚本,每次生成两行输出。 我只对第二行感兴趣。 另外,我只对第二行中出现在一对#之间的文本感兴趣。 此外,在这些井号之间,还使用另一个分隔符:^A。 如果我能够分开每个通过^A分隔的文本部分,那就太好了(请注意,^A是SOH特殊字符,可以使用Ctrl-A键输入)。
我有一个脚本,每次生成两行输出。 我只对第二行感兴趣。 另外,我只对第二行中出现在一对#之间的文本感兴趣。 此外,在这些井号之间,还使用另一个分隔符:^A。 如果我能够分开每个通过^A分隔的文本部分,那就太好了(请注意,^A是SOH特殊字符,可以使用Ctrl-A键输入)。
output | sed -n '1p' #prints the 1st line of output
output | sed -n '1,3p' #prints the 1st, 2nd and 3rd line of output
your.program | tail +2 | cut -d# -f2
这应该可以帮助你完成三分之二的工作。
your.program | head -n 2| tail -1 | cut -d# -f2
我可能会使用awk来完成这个任务。
your_script | awk -F# 'NR == 2 && NF == 3 {
num_tokens=split($2, tokens, "^A")
for (i = 1; i <= num_tokens; ++i) {
print tokens[i]
}
}'
这句话是
1. Set the field separator to #
2. On lines that are the 2nd line, and also have 3 fields (text#text#text)
3. Split the middle (2nd) field using "^A" as the delimiter into the array named tokens
4. Print each token
显然,这里有很多假设。如果数据中可能会出现#或^A等分隔符以外的字符,你可能需要进行一些调整。但是像这样的东西应该可以让你开始工作。你可能需要使用nawk或gawk之类的工具,我不确定普通的awk是否能够处理控制字符的分割。
bash:
read
read line
result="${line#*#}"
result="${result%#*}"
IFS=$'\001' read result -a <<< "$result"
$result
现在是一个包含你感兴趣的元素的数组。只需将脚本的输出导向此处即可。
这里有一个可能的 awk 解决方案
awk -F"#" 'NR==2{
for(i=2;i<=NF;i+=2){
split($i,a,"\001") # split on SOH
for(o in a ) print o # print the splitted hash
}
}' file