bloodstorm17
bloodstorm17

Reputation: 521

Grep for one thing then cut from the line below

Okay so I can't figure this out. Like a file that I using grep in order to get certain information has it set-up like this.

#aaaa
<numbers 123456>

blah
blah
blah

#bbbb
<numbers 2156488>

blah
blah
blah

#cccc
<numbers 5478624>

blah
blah
blah

What I am doing is I am greping for aaaa or bbbb or cccc and the information that i really need is the numbers. As in when i grep for aaaa the thing i want to obtain is really just the numbers right below it. In this case it would 123456

I know how to grep for aaaa but i don't know how to go to the next line and cut the number.

Upvotes: 0

Views: 6753

Answers (6)

askmish
askmish

Reputation: 6674

Try this one:

for i in "aaaa bbbb"
do
sed -n '/'"$i"'/,+1 p' test |tail -n1|cut -d' ' -f2| sed 's/.$//'
done

Its not very efficient but does the job fine.

Upvotes: 0

Jonathan Leffler
Jonathan Leffler

Reputation: 753970

It looks more like a job for sed to me:

sed -n '/^#[a-z]\{4\}/{ N; s/#.*\n<numbers //; s/>//p; }'

The -n says don't print by default. The /^#[a-z]\{4\}/ looks for lines like #aaaa; the actions inside { ... } apply only to such lines. The N means 'read the next line'; the first s/// removes the material before the number; the second removes the trailing > and prints.

There are other options on how to do the 'delete irrelevant material', such as just one substitute command: s/[^0-9]*\([0-9][0-9]+\).*/\1/; this captures the first string of digits and removes everything else. It will handle more variations in the input than the more constrained regular expressions originally shown.

Output from sample data:

123456
2156488
5478624

This isn't automatically a job for sed; change it so that the interesting information was the third line after the match and it would be getting fiddly in sed (though N;N;N; probably does what's wanted).

Upvotes: 2

slitvinov
slitvinov

Reputation: 5768

If you are not insisting on grep
+0 to get rid of ">"

awk 'f{print $2+0; exit} /^#aaaa/{f=1}' foo.txt

Or

awk 'f{print $2+0; f=0} /^#aaaa/{f=1}' foo.txt

Upvotes: 0

perreal
perreal

Reputation: 97948

Using sed:

sed -n '/aaaa/{
N
s/[^0-9]*\([0-9]*\).*/\1/p
}' input_file

Upvotes: 5

bloodstorm17
bloodstorm17

Reputation: 521

@ Lars Kotthoff

Your suggestion to use the -A 1 option worked perfectly!

The answer using your input is this:

grep "aaaa" file -A 1 | grep "<numbers" | cut -d" " -f2 | cut -d">" -f1

I would love to give you the credit for this one!

Upvotes: 0

Mihai Maruseac
Mihai Maruseac

Reputation: 21435

You can use the -C option of grep to show one line of context. Then you can tail on the last line (-n 1), cut using spaces and selecting the second field, cut again using > and selecting the first field. Thus:

$ grep aaaa file -C 1 | tail -n 1 | cut -f2 -d' ' | cut -d'>' -f1
123456

will give back the number you're requesting.

The most important part is the -C option of grep

Upvotes: 4

Related Questions