You are not logged in.

#1 2013-11-06 16:02:39

nadman10
Member
From: Erie, PA
Registered: 2005-10-15
Posts: 201

[SOLVED]Need to parse output data

I have a program that outputs data to the console in this format:

[1] banana
[2] orange
[7] apple
[17] grape
[42] strawberry

I would like to strip this output and only get the values for [2] and [17]. So my output would be:

orange
grape

My first idea was to pipe the output to grep and then possibly use sed to strip the preceding numbers and brackets. What is the recommended way to do this and why? Is there a better suited utility that I'm not aware of?

Last edited by nadman10 (2013-11-06 16:48:54)

Offline

#2 2013-11-06 16:15:20

karol
Archivist
Registered: 2009-05-06
Posts: 25,440

Re: [SOLVED]Need to parse output data

Grep whatever values you want and remove the brackest with the numbers with e.g.

sed 's/\[.*\] //' <filename>

Last edited by karol (2013-11-06 16:21:00)

Offline

#3 2013-11-06 16:23:09

nadman10
Member
From: Erie, PA
Registered: 2005-10-15
Posts: 201

Re: [SOLVED]Need to parse output data

karol wrote:

Grep whatever values you want and remove the brackest with the numbers with e.g.

sed 's/\[.*\] //' <filename>

Thank you Karol. You always seem to have the correct answers. Is this the most efficient way of doing this? I can't think of more simple way of doing it other than this.

Offline

#4 2013-11-06 16:25:29

karol
Archivist
Registered: 2009-05-06
Posts: 25,440

Re: [SOLVED]Need to parse output data

You can use 'cut'

grep -e "\[2\]" -e "\[17\]" <filename> | cut -d ' ' -f 2

Offline

#5 2013-11-06 16:38:52

Trilby
Inspector Parrot
Registered: 2011-11-29
Posts: 30,332
Website

Re: [SOLVED]Need to parse output data

grep + (cut/sed) = awk.

awk '/\[(2|17)\]/ { print $2; }'

The first equation is the logical one, but the value version would be the inequality:

awk > grep + (cut|sed)

Last edited by Trilby (2013-11-06 16:39:58)


"UNIX is simple and coherent" - Dennis Ritchie; "GNU's Not Unix" - Richard Stallman

Offline

#6 2013-11-06 16:44:00

karol
Archivist
Registered: 2009-05-06
Posts: 25,440

Re: [SOLVED]Need to parse output data

Trilby wrote:

grep + (cut/sed) = awk.

Very true.

Trilby wrote:
 awk '/\[(2|17)\]/ { print $2; }'

I wanted OP to google / RTFM this himself tongue


The idea to use sed was correct, nadman10. You should have googled

remove "square brackets" sed

and you would have found out the answer yourself: http://www.unix.com/showthread.php?t=159143

The slightly tricky part with the square brackets is that they have special meaning, so you have to escape them i.e. precede them with '\'.

Offline

#7 2013-11-06 16:48:40

nadman10
Member
From: Erie, PA
Registered: 2005-10-15
Posts: 201

Re: [SOLVED]Need to parse output data

karol wrote:

You can use 'cut'

grep -e "\[2\]" -e "\[17\]" <filename> | cut -d ' ' -f 2

Your grep syntax is much more simple than mine. I'm going to use this in conjunction with what you suggested with sed. Thanks for your help guys!

Offline

#8 2013-11-06 16:52:59

Trilby
Inspector Parrot
Registered: 2011-11-29
Posts: 30,332
Website

Re: [SOLVED]Need to parse output data

karol wrote:

I wanted OP to google / RTFM this himself tongue

Touche.

To justify my seeming inconsistency (with other thread(s)), I think providing working awk commands reveals what can be done which highlights what there is to learn about.  One would likely still need to read up on the tool to understand how/why it is done and how else it could be applied.


"UNIX is simple and coherent" - Dennis Ritchie; "GNU's Not Unix" - Richard Stallman

Offline

#9 2013-11-06 16:53:42

karol
Archivist
Registered: 2009-05-06
Posts: 25,440

Re: [SOLVED]Need to parse output data

nadman10 wrote:
karol wrote:

You can use 'cut'

grep -e "\[2\]" -e "\[17\]" <filename> | cut -d ' ' -f 2

Your grep syntax is much more simple than mine.

This wasn't meant to be simple, but readable.
You can compress

grep -e "\[2\]" -e "\[17\]"

to

grep -E '\[(2|17)\]'

but I think stepwise refinement is a better approach, so I started with something that's IMHO easier to figure out.

Last edited by karol (2013-11-06 16:54:34)

Offline

#10 2013-11-06 17:01:04

nadman10
Member
From: Erie, PA
Registered: 2005-10-15
Posts: 201

Re: [SOLVED]Need to parse output data

It wasn't that I didn't RTFM, I was looking for the most efficient way to it.

Offline

Board footer

Powered by FluxBB