开发者

Getting n-th line of text output

I have a script that generates two lines as output each time. I'm really just interested in the second line. Moreover I'm only interested in the text that appears between a pair of #'s on the second line. Additionally, between the hashes, another delimiter is used: ^A. It would be great if I can also break apart each part of text that is ^A-delimited (Note that ^A is SOH special character a开发者_运维技巧nd can be typed by using 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 

should get you 2/3 of the way.


Improving Grumdrig's answer:

your.program | head -n 2| tail -1 | cut -d# -f2 


I'd probably use awk for that.

   your_script | awk -F# 'NR == 2 && NF == 3 { 
                            num_tokens=split($2, tokens, "^A")
                            for (i = 1; i <= num_tokens;  ++i) { 
                              print tokens[i]
                            } 
                          }' 

This says

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 

Obviously this makes a lot of assumptions. You might need to tweak it if, for example, # or ^A can appear legitimately in the data, without being separators. But something like that should get you started. You might need to use nawk or gawk or something, I'm not entirely sure if plain awk can handle splitting on a control character.


bash:

read
read line
result="${line#*#}"
result="${result%#*}"
IFS=$'\001' read result -a <<< "$result"

$result is now an array that contains the elements you're interested in. Just pipe the output of the script to this one.


here's a possible awk solution

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
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜