LinuxQuestions.org
Did you know LQ has a Linux Hardware Compatibility List?
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
Search this Thread
Old 05-05-2013, 03:34 AM   #1
Hoxygen232
LQ Newbie
 
Registered: Jan 2013
Posts: 28

Rep: Reputation: Disabled
Print only some strings from an output


Hi,

Here is an example:

I have a grep line:
Code:
grep -i -r -H "$WORD" "$DIRECTORY"
with an output like this:
Code:
/media/dir/dir2//dir4/file.txt:/media/dir/dir2/dir3/file_16072008/es6.txt: "content of the file found from grep"/media/dir/dir2/dir3/dir4/file3.txt:/media/dir/dir2/dir3//file.txt:"other found content"
I want to get this output from grep and print only the found content with the file in which it was found, in this example I want to print (with newline between every file):
Code:
/media/dir/dir2/dir3/dir4/file_16072008/es6.txt: "content of the file found from grep"
/media/dir/dir2/dir3/dir4/file.txt:"other found content"
Is that possible?

Thanks

Last edited by Hoxygen232; 05-05-2013 at 04:12 AM.
 
Old 05-05-2013, 09:59 AM   #2
Larry James
Member
 
Registered: Jun 2000
Location: Buffalo, New York
Distribution: Fedora, Ubuntu
Posts: 343

Rep: Reputation: 38
Quote:
Originally Posted by Hoxygen232 View Post
Hi,

Here is an example:

I have a grep line:
Code:
grep -i -r -H "$WORD" "$DIRECTORY"
with an output like this:
Code:
/media/dir/dir2//dir4/file.txt:/media/dir/dir2/dir3/file_16072008/es6.txt: "content of the file found from grep"/media/dir/dir2/dir3/dir4/file3.txt:/media/dir/dir2/dir3//file.txt:"other found content"
I want to get this output from grep and print only the found content with the file in which it was found, in this example I want to print (with newline between every file):
Code:
/media/dir/dir2/dir3/dir4/file_16072008/es6.txt: "content of the file found from grep"
/media/dir/dir2/dir3/dir4/file.txt:"other found content"
Is that possible?

Thanks
Will you post an example of what you like to print? For instance print what you have, then an example of what you prefer to be printed.

commandline:
Code:
This is the command typed.
cli output:
Code:
This is the output of the grep command.
desired output:
Code:
This is the preferred output.
By the way, you might have already provided that in your original post, but it wasn't totally clear to me.

-- L. James

--
L. D. James
ljames@apollo3.com
www.apollo3.com/~ljames
 
Old 05-05-2013, 10:27 AM   #3
grail
Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 7,425

Rep: Reputation: 1876Reputation: 1876Reputation: 1876Reputation: 1876Reputation: 1876Reputation: 1876Reputation: 1876Reputation: 1876Reputation: 1876Reputation: 1876Reputation: 1876
I am not sure I follow either

Firstly, -H is only needed if there is a chance of only one file being returned (but the choice is of course yours).

Secondly, once a file is found using -H or if multiple files, the default output is to list the file name and the corresponding line, example:
Code:
$ cat file1
Fruit,Kiwi
Fruit,Pear
Veggie,carrot
Dairy,Milk
$ cat file2
Item,A,Veggie,tomato,20120903,Bin,42
Item,B,Fruit,Kiwi,20120901,Bin,2
Item,B,Fruit,Grapes,20120903,Bin,12
Item,A,Dairy,Milk,20120921,Fridge,3
Item,B,Fruit,Pear,20120903,Bin,14
Item,C,Veggie,carrot,20120903,Bin,45
Item,B,Veggie,celery,20120903,Bin,32
$ cat file3
------------------------------------------------------------------------
 somehostname
------------------------------------------------------------------------
OS-Users:
        Administrator / Passw0rd

------------------------------------------------------------------------
 Anotherhostname
------------------------------------------------------------------------
OS-Users:
        Administrator / Passw0rd
        User2 / Diff3r3ntPassw0rd
$ grep Fruit file*
file1:Fruit,Kiwi
file1:Fruit,Pear
file2:Item,B,Fruit,Kiwi,20120901,Bin,2
file2:Item,B,Fruit,Grapes,20120903,Bin,12
file2:Item,B,Fruit,Pear,20120903,Bin,14
So i am not sure what else you require?
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] Awk question - Search, print & replace strings shivaa Linux - Newbie 6 12-06-2012 01:03 PM
input and output strings separated by operators chanchong chau Linux - Newbie 3 02-03-2012 11:24 AM
showing the number of strings in the output filename udiubu Programming 3 11-08-2011 12:28 PM
Why child process can't print strings after call signal_handler? chaoxifer Linux - Newbie 2 11-02-2010 10:26 PM
BASH: Output everything between two strings systemparadox Programming 2 12-18-2004 10:26 AM


All times are GMT -5. The time now is 12:55 PM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration