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 and can be typed by using Ctrl-A)
Asked
Active
Viewed 4.0k times
6 Answers
77
output | sed -n '1p' #prints the 1st line of output
output | sed -n '1,3p' #prints the 1st, 2nd and 3rd line of output

N 1.1
- 12,418
- 6
- 43
- 61
-
3Your answer is better. Not sure why another one is accepted. Very confusing. – Darth Egregious Aug 12 '14 at 13:50
-
1This is precisely what I was looking for. Thanks! @n-1-1 Do you know if it's possible to specify lines 1,3,5 rather than 1-5 using sed? I couldn't find anything myself. – Olshansky Jun 19 '16 at 01:32
13
your.program | tail +2 | cut -d# -f2
should get you 2/3 of the way.

Grumdrig
- 16,588
- 14
- 58
- 69
-
1Is cut the best utility to use on an undeterminate amount of values that are delimited by ^A? – syker Apr 08 '10 at 17:26
-
1I'm not sure. I don't really understand what your goal is in terms of the ^A's. To turn them into something else? Line breaks? – Grumdrig Apr 08 '10 at 18:23
1
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.

janks
- 2,120
- 16
- 13
0
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.

Ignacio Vazquez-Abrams
- 776,304
- 153
- 1,341
- 1,358
0
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

ghostdog74
- 327,991
- 56
- 259
- 343