繁体   English   中英

获得第n行文本输出

[英]Getting n-th line of text output

我有一个脚本,每次生成两行作为输出。 我真的只对第二行感兴趣。 此外,我只对第二行中#对的文本感兴趣。 此外,在哈希之间,使用另一个分隔符:^ 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 

应该让你2/3的方式。

改善Grumdrig的答案:

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是否可以处理控制字符上的拆分。

庆典:

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM