Question about Shell command grep

Need help with C, C++, perl, python, etc?

Question about Shell command grep

Postby begatelles » 2007-01-04 12:23

Hey

I was never able to figure out how to send the output from grep to a text file with all the matching lines appearing as seperate lines in the new file. So i would just use the -n option to at least print the matching line number and thus make it easier to read.

Example: echo `grep abc -n doc` > doc2 will print the matching lines into doc2 as a large literal string, but simply grep abc doc will print them on the screen as seperate lines. Perhaps the echo command is not the appropriate one here? Someone know how to get the same output but in a file?

Perhaps there's a very simple solution to my question but i was never able to figure it out. In my fears there might be a bit of shell scripting involved with a loop adding a newline character somehow after each matching line, but i hope there's a simpler faster solution. Thanks. :)
begatelles
 
Posts: 50
Joined: 2007-01-03 12:24

Postby Lavene » 2007-01-04 12:44

What happen if you skip 'echo'? Just
Code: Select all
grep abc -n doc > doc2

Do the list items actually end with a 'new line' by the way?

Tina

PS: I'm not really a bash expert but I have never used 'echo' when I want output from 'grep' to go to a file...
Lavene
Site admin
 
Posts: 5096
Joined: 2006-01-04 04:26
Location: Oslo, Norway

Postby begatelles » 2007-01-04 18:31

Thanks Tina. :)

It does work without echo. I was trying to print some text for headings actually for each output from grep, so i could form one command with all my searches. Something like this: echo "Name abc: " `grep abc doc` "Name cde: " `grep cde -n doc` > doc2

I wanted an output of this form:

Name abc:
line 1
line 2
Name cde:
line7
line 8

I should have just descriped it this way in my previous post. :D Okay not a big deal, i know. It's just a heading for each search. I guess i shouldn't try and fit them all in one query right?
Last edited by begatelles on 2007-01-04 18:38, edited 1 time in total.
begatelles
 
Posts: 50
Joined: 2007-01-03 12:24

Postby Grifter » 2007-01-04 18:37

for i in abc cde; do echo "Name: $i" >> formfile; grep $i document >> formfile; echo "" >> formfile; done
Eagles may soar, but weasels don't get sucked into jet engines...
Grifter
 
Posts: 1572
Joined: 2006-05-04 07:53
Location: Svea Rike

Postby Grifter » 2007-01-04 18:51

actually you can put in newlines in echo itself, you just need to enable it with -e

echo -e "Name: abc\n`grep -n abc doc1`" >> doc2

but if you want to automate it, make a loop

for i in abc cde efg; do echo -e "Name: $i\n`grep -n $i doc1`" >> doc2; done
Eagles may soar, but weasels don't get sucked into jet engines...
Grifter
 
Posts: 1572
Joined: 2006-05-04 07:53
Location: Svea Rike

Postby begatelles » 2007-01-04 18:53

for i in abc cde; do echo "Name: $i" >> formfile; grep $i document >> formfile; echo "" >> formfile; done


Thank you!!! This worked Perfectly!! You also added the newline after each grep result! :)

Thanks both of you for your quick responses. :)
begatelles
 
Posts: 50
Joined: 2007-01-03 12:24

Postby begatelles » 2007-01-04 19:04

Grifter wrote:actually you can put in newlines in echo itself, you just need to enable it with -e

echo -e "Name: abc\n`grep -n abc doc1`" >> doc2

but if you want to automate it, make a loop

for i in abc cde efg; do echo -e "Name: $i\n`grep -n $i doc1`" >> doc2; done


hehe thanks. I thought newlines were not possible with echo and i went back and removed them from my second post.

This one worked fine too:
echo -e "Name: abc\n`grep -n abc doc`\n\n""Name: cde\n`grep -n cde doc`" >> doc2
I just added the second query to the one you wrote. Perfect.

or simply: echo -e "Name: abc\n`grep -n abc doc`\n\nName: cde\n`grep -n cde doc`" >> doc2

I seriously need to study these things better.

Thanks again. ;)
begatelles
 
Posts: 50
Joined: 2007-01-03 12:24


Return to Programming

Who is online

Users browsing this forum: No registered users and 6 guests

fashionable