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: 3 January 2014, 6:46 AM EST
Posts: 177
Thanks: 32
Thanked 8 Times in 7 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 offline
Registered User
 
Join Date: May 2013
Last Activity: 21 July 2014, 5:16 PM EDT
Location: Chennai
Posts: 487
Thanks: 161
Thanked 80 Times in 77 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: 23 July 2014, 6:17 PM EDT
Location: Aachen, Germany
Posts: 3,870
Thanks: 62
Thanked 918 Times in 871 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 06: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: 23 July 2014, 4:21 PM EDT
Location: Goa / Bangalore, India
Posts: 1,414
Thanks: 72
Thanked 440 Times in 401 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: 23 July 2014, 6:17 PM EDT
Location: Aachen, Germany
Posts: 3,870
Thanks: 62
Thanked 918 Times in 871 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: 23 July 2014, 4:21 PM EDT
Location: Goa / Bangalore, India
Posts: 1,414
Thanks: 72
Thanked 440 Times in 401 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 08:07 AM
Compare Field in Current Line with Field in Previous moutaye Shell Programming and Scripting 9 03-27-2012 05: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 03:16 AM
grep multiple words in a single line anduzzi Shell Programming and Scripting 11 06-25-2009 11:39 AM
replace single field out of 60 ajp7701 Shell Programming and Scripting 2 04-21-2008 07:17 PM



All times are GMT -4. The time now is 12:13 AM.