How to grep a single field from a line? | Unix Linux Forums | Shell Programming and Scripting

  Go Back    


Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

How to grep a single field from a line?

Shell Programming and Scripting


Tags
shell scipt, unix

Closed Thread    
 
Thread Tools Search this Thread Display Modes
    #1  
Old 11-12-2013
Little Little is offline
Registered User
 
Join Date: Feb 2013
Last Activity: 17 November 2014, 1:28 PM EST
Posts: 181
Thanks: 32
Thanked 9 Times in 8 Posts
How to grep a single field from a line?

hi,

i have a text file with some fields.
file.txt

Code:
aaa|bbb|ccc|1-sh|2-sh|5-sh
ddd|eee|fff|1-sh|4-sh

i am selecting a line using grep command using the combination of 1st 3 column.

Code:
grep "aaa|bbb|ccc" file.txt

output:
aaa|bbb|ccc|1-sh|2-sh|5-sh

now i want to cut a field from this line.
if i am passing 2 as a parameter to a program, then it should cut "2-sh", if i specify 5 then it should cut 5-sh and so on.
Sponsored Links
    #2  
Old 11-12-2013
RavinderSingh13 RavinderSingh13 is online now
Registered User
 
Join Date: May 2013
Last Activity: 26 November 2014, 10:02 AM EST
Location: Chennai
Posts: 808
Thanks: 222
Thanked 196 Times in 189 Posts
Hi,

Could you please use the following code.


Code:
echo "aaa|bbb|ccc|1-sh|2-sh|5-sh" | awk -F"|" '{print $5}'

Output will be as follows.


Code:
2-sh

kindly let me know if you have any queries for same.



Thanks,
R. Singh
Sponsored Links
    #3  
Old 11-12-2013
RudiC RudiC is offline Forum Advisor  
Registered User
 
Join Date: Jul 2012
Last Activity: 26 November 2014, 7:23 AM EST
Location: Aachen, Germany
Posts: 4,703
Thanks: 80
Thanked 1,169 Times in 1,099 Posts
Your specification is a bit difficult to interpret. If you, in a line that begins with "aaa|bbb|ccc", want that field that begins with your parameter value (2 or 5), try
Code:
awk '-F|' '/^aaa|bbb|ccc/ {for (i=4; i<=NF; i++) if ($i ~ "^"fn) print $i}' fn=2 file
2-sh


Last edited by RudiC; 11-12-2013 at 07:04 AM..
    #4  
Old 11-12-2013
Akshay Hegde's Avatar
Akshay Hegde Akshay Hegde is offline Forum Advisor  
Forum Advisor
 
Join Date: Nov 2012
Last Activity: 26 November 2014, 4:59 AM EST
Posts: 1,634
Thanks: 78
Thanked 532 Times in 486 Posts
Try something like this


Code:
$ cat <<eof | awk -F"|" '/^aaa\|bbb\|ccc/{for(i=1;i<=NF;i++)if($i~search"-sh")print $i}' search=1
aaa|bbb|ccc|1-sh|2-sh|5-sh
ddd|eee|fff|1-sh|4-sh
eof     

1-sh
$ cat <<eof | awk -F"|" '/^aaa\|bbb\|ccc/{for(i=1;i<=NF;i++)if($i~search"-sh")print $i}' search=2
aaa|bbb|ccc|1-sh|2-sh|5-sh
ddd|eee|fff|1-sh|4-sh
eof

2-sh
$ cat <<eof | awk -F"|" '/^aaa\|bbb\|ccc/{for(i=1;i<=NF;i++)if($i~search"-sh")print $i}' search=5
aaa|bbb|ccc|1-sh|2-sh|5-sh
ddd|eee|fff|1-sh|4-sh
eof

5-sh

@RudyC I think you missed back slash


Code:
$ cat <<eof | awk -F"|" '/^aaa|bbb|ccc/{for(i=1;i<=NF;i++)if($i~search"-sh")print $i}' search=1
aaa|bbb|ccc|1-sh|2-sh|5-sh
ddd|eee|fff|1-sh|4-sh
aaa|1-sh
eof

1-sh
1-sh

$ cat <<eof | awk -F"|" '/^aaa\|bbb\|ccc/{for(i=1;i<=NF;i++)if($i~search"-sh")print $i}' search=1
aaa|bbb|ccc|1-sh|2-sh|5-sh
ddd|eee|fff|1-sh|4-sh
aaa|1-sh
eof

1-sh

Sponsored Links
    #5  
Old 11-12-2013
RudiC RudiC is offline Forum Advisor  
Registered User
 
Join Date: Jul 2012
Last Activity: 26 November 2014, 7:23 AM EST
Location: Aachen, Germany
Posts: 4,703
Thanks: 80
Thanked 1,169 Times in 1,099 Posts
@Akshay Hegde: Not needed for my mawk. But, doesn't hurt, either. Thanks for pointing out.
Sponsored Links
    #6  
Old 11-12-2013
Akshay Hegde's Avatar
Akshay Hegde Akshay Hegde is offline Forum Advisor  
Forum Advisor
 
Join Date: Nov 2012
Last Activity: 26 November 2014, 4:59 AM EST
Posts: 1,634
Thanks: 78
Thanked 532 Times in 486 Posts
@ RudiC : I tried in gawk so posted..
Sponsored Links
Closed Thread

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
sed command to grep multiple pattern present in single line and delete that line vivek d r Shell Programming and Scripting 14 05-14-2013 09:07 AM
Compare Field in Current Line with Field in Previous moutaye Shell Programming and Scripting 9 03-27-2012 06:18 AM
Multiple lines in a single column to be merged as a single line for a record Bhuvaneswari Shell Programming and Scripting 1 08-11-2011 04:16 AM
grep multiple words in a single line anduzzi Shell Programming and Scripting 11 06-25-2009 12:39 PM
replace single field out of 60 ajp7701 Shell Programming and Scripting 2 04-21-2008 08:17 PM



All times are GMT -4. The time now is 11:03 AM.