LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - General
User Name
Password
Linux - General This Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.

Notices


Reply
  Search this Thread
Old 09-06-2006, 06:50 AM   #1
Dr_Death_UAE
Member
 
Registered: Jul 2005
Location: U.A.E
Distribution: FreeBSD,Fedora,Solaris,AIX
Posts: 168

Rep: Reputation: 30
showing grep result without the whole line


Hi, i have text file and i want to get a specific string from it.

i tried many grep opions, but it show me the result within the whole line.
this is how the string looks like:

checkName='INPUT' risk='INPUT'

the file have about 162 of this string. each sting have diffrent input on it.

any advice !

sorry for my English
 
Old 09-06-2006, 07:16 AM   #2
zulfilee
Member
 
Registered: Apr 2004
Location: India
Distribution: Redhat,Fedora
Posts: 430

Rep: Reputation: 39
I cannot understand that.

You mean you will give something like this

grep INPUT file
And it will display
INPUT INPUT
INPUT

[INPUT will be one common pattern]
But is that your req.
 
Old 09-06-2006, 07:20 AM   #3
homey
Senior Member
 
Registered: Oct 2003
Posts: 3,057

Rep: Reputation: 61
Maybe using the -o ...
Code:
 grep -o "checkName='fred'" file.txt
 
Old 09-06-2006, 07:31 AM   #4
kstan
Member
 
Registered: Sep 2004
Location: Malaysia, Johor
Distribution: Dual boot MacOS X/Ubuntu 9.10
Posts: 851

Rep: Reputation: 31
use
grep filename | awk '{print $1 $2}'

$1 = 1st culumn, $2 = 2nd colummn
 
Old 09-06-2006, 09:57 AM   #5
makyo
Member
 
Registered: Aug 2006
Location: Saint Paul, MN, USA
Distribution: {Free,Open}BSD, CentOS, Debian, Fedora, Solaris, SuSE
Posts: 732

Rep: Reputation: 75
Hi.

I'm guessing, but I think he is looking for the specific content of what he called generally INPUT, so if this were a sample:
Code:
checkName='mary' risk='medium'
checkName='bill' risk='low'
checkName='sweetpea' risk='none'
then he would like:
Code:
mary medium
bill low
sweetpea none
which makes more sense. Naturally, we need confirmation from Dr_Death_UAE ... cheers, makyo
 
Old 09-06-2006, 01:05 PM   #6
Dr_Death_UAE
Member
 
Registered: Jul 2005
Location: U.A.E
Distribution: FreeBSD,Fedora,Solaris,AIX
Posts: 168

Original Poster
Rep: Reputation: 30
hi, thanks guys for reply.I want the output of grep be like this:

checkName='mary' risk='medium'
checkName='bill' risk='low'
checkName='sweetpea' risk='none'

after each checkName and risk there is a variable.

the file have many lines on it, within the lines it include "checkName='VARIABLE' risk='VARIABLE'"

sorry again for my bad English
 
Old 09-06-2006, 07:52 PM   #7
kstan
Member
 
Registered: Sep 2004
Location: Malaysia, Johor
Distribution: Dual boot MacOS X/Ubuntu 9.10
Posts: 851

Rep: Reputation: 31
can paste partial of the content here?
 
Old 09-07-2006, 01:30 AM   #8
zulfilee
Member
 
Registered: Apr 2004
Location: India
Distribution: Redhat,Fedora
Posts: 430

Rep: Reputation: 39
That can be done with sed

sed "s/.*checkName='\([^']*\)'.*risk='\([^']*\)'.*/\1 \2/" <filename>


Description:

The content between ' ' has been put inside () .
The first occurrence is \1 and the second one is \2.

You can put any seperator you want between \1 and \2 .

Zulfi.A
 
Old 09-07-2006, 02:06 AM   #9
Dr_Death_UAE
Member
 
Registered: Jul 2005
Location: U.A.E
Distribution: FreeBSD,Fedora,Solaris,AIX
Posts: 168

Original Poster
Rep: Reputation: 30
Hi, Thanks Zulfilee sed gave me the variables, without checkName= & risk=

the output of sed was like this:
mary mediam

i want it to be like this:
checkName='mary' risk='medium'

Thanks again bro.

Im goning to g00gle sed (^_^)
 
Old 09-07-2006, 02:11 AM   #10
zulfilee
Member
 
Registered: Apr 2004
Location: India
Distribution: Redhat,Fedora
Posts: 430

Rep: Reputation: 39
Include that 'checkName=' and 'risk=' inside the () braces.


sed "s/.*\(checkName='[^']*\)'.*\(risk='[^']*\)'.*/\1 \2/" <filename>
 
Old 09-07-2006, 02:11 AM   #11
kstan
Member
 
Registered: Sep 2004
Location: Malaysia, Johor
Distribution: Dual boot MacOS X/Ubuntu 9.10
Posts: 851

Rep: Reputation: 31
maybe add little bit change will do

sed "s/.*checkName='\([^']*\)'.*risk='\([^']*\)'.*/\1 \2/" <filename> | awk '{print checkName=$1 risk=$2}'
 
Old 09-07-2006, 02:21 AM   #12
Dr_Death_UAE
Member
 
Registered: Jul 2005
Location: U.A.E
Distribution: FreeBSD,Fedora,Solaris,AIX
Posts: 168

Original Poster
Rep: Reputation: 30
i did it like this :

sed "s/.*checkName='\([^']*\)'.*risk='\([^']*\)'.*/checkName='\1' risk='\2'/"
FileName
 
Old 09-07-2006, 06:12 AM   #13
makyo
Member
 
Registered: Aug 2006
Location: Saint Paul, MN, USA
Distribution: {Free,Open}BSD, CentOS, Debian, Fedora, Solaris, SuSE
Posts: 732

Rep: Reputation: 75
Hi.

OK, good.

We can all learn from this that it helps to provide a sample of the data we have and the results we want ... cheers, makyo
 
  


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 On
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Using find and grep - how to exclude a result ? graziano1968 Linux - General 13 12-04-2010 10:02 PM
ps -ef | grep iptables gives no result ? markraem Linux - Networking 1 07-07-2004 05:28 AM
how to grep within a script, and test result? bobbyr Programming 4 01-13-2004 12:11 PM
multiline grep result > multiple variable? mikshaw Linux - Software 5 12-25-2003 10:13 PM
ps -ef|grep -v root|grep apache<<result maelstrombob Linux - Newbie 1 09-24-2003 11:38 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - General

All times are GMT -5. The time now is 04:19 AM.

Main Menu
Advertisement
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
Open Source Consulting | Domain Registration