[英]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.
此外,在哈希之间,使用另一个分隔符:^ 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 and can be typed by using Ctrl-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
I'd probably use awk for that. 我可能会使用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]
}
}'
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.
例如,如果#或^ A可以合法地出现在数据中而不是分隔符,则可能需要对其进行调整。 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.
您可能需要使用nawk或gawk或其他东西,我不完全确定简单的awk是否可以处理控制字符上的拆分。
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. $result
现在是一个包含您感兴趣的元素的数组。只需将脚本的输出传递给这个元素。
here's a possible awk solution 这是一个可能的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.