The UNIX and Linux Forums  
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.

Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
.
google unix.com



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

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
| help | unix | grep (GNU grep) 2.5.1 | advanced regex syntax MykC UNIX for Dummies Questions & Answers 4 10-15-2009 10:52 AM
| help | unix | grep - Can I use grep to return a string with exactly n matches? MykC UNIX for Dummies Questions & Answers 7 10-13-2009 01:57 PM
MEM=`ps v $PPID| grep -i db2 | grep -v grep| awk '{ if ( $7 ~ " " ) { print 0 } else hariza Shell Programming and Scripting 4 09-18-2008 02:56 AM
grep and fetching lines after grep arghya_owen Shell Programming and Scripting 2 07-16-2008 07:25 AM
Make grep -c display like grep -n? Jerrad Shell Programming and Scripting 2 08-25-2006 12:20 AM

Reply
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 2 Weeks Ago
bittoo bittoo is offline
Registered User
  
 

Join Date: Aug 2009
Posts: 16
Exclamation Using grep and cut within awk

Hi

My input file looks like as follows: say a.txt

Code:
"aaaa cc","224 AW","ss cccccc","P06 09/10","dddddd"
"aaaa cc","224 AW","ss cccccc","P06 09/10","dddddd"
"aaaa cc","224 AW","ss cccccc","P06 09/10","dddddd"
"aaaa cc","224 AW","ss cccccc","P06 09/10","dddddd"
"aaaa cc","224 AW","ss cccccc","P06 09/10","dddddd"
There is another file b.txt which acts like lookup of "AW" in a.txt, like

Code:
1 AW
2 AS
.....
My output should be like:
Code:
aaaa      2242009W1000001(last 5 characters -> 00001 is look up value for AW from b.txt).
Am able to achieve till "aaaa 2242009W10" using the following command

Code:
sed -e 's/ /,/g' -e 's/\//,/g' -e 's/"//g' a.txt|awk -F"," '{printf "%-10s%3s%s%s%s%s\n",$1,$3,"20",$8,"W",$9}'
Kindly help in getting the "grep AW b.txt| cut -d" " -f1" part in awk command itself, so that seperate script can be avoided.

Thanks in Advance.

Last edited by radoulov; 2 Weeks Ago at 09:21 AM.. Reason: Use code tags, please!
  #2 (permalink)  
Old 2 Weeks Ago
aigles's Avatar
aigles aigles is online now Forum Advisor  
Registered User
  
 

Join Date: Apr 2004
Location: Bordeaux, France
Posts: 1,414
Try and adapt the following awk script :
Code:
awk -F '[[:space:],/]' '
NR==FNR { lookup[$2] = $1 ; next }
{
   gsub(/"/,"");
   printf "%-10s%3s20%sW%s%05d\n",$1,$3,$8,$9,lookup[$4];
}
' b.txt a.txt
jean-Pierre.
  #3 (permalink)  
Old 2 Weeks Ago
Scrutinizer Scrutinizer is online now
Registered User
  
 

Join Date: Nov 2008
Posts: 594
Code:
sed -e 's|"||g;s|[ /]|,|g' b.txt a.txt |awk -F, '!$3{A[$2]=$1;next}{printf "%-10s%s20%sW%s%05d\n",$1,$3,$8,$9,A[$4]}'
  #4 (permalink)  
Old 1 Week Ago
bittoo bittoo is offline
Registered User
  
 

Join Date: Aug 2009
Posts: 16
Both the commands work perfectly.. Can u guys please explain
"!$3{A[$2]=$1;next}" part of the command.. Thanx a lot for ur help
  #5 (permalink)  
Old 1 Week Ago
Scrutinizer Scrutinizer is online now
Registered User
  
 

Join Date: Nov 2008
Posts: 594
Sure:
If $3 does not exist (in other words: if the input originates from b.txt) then set the array A with key $2 to the value of $1. next means skip the next commands and re-enter the loop. The rest of the code gets executed when $3 contains a value, i.e. when the input originates from a.txt.
  #6 (permalink)  
Old 1 Week Ago
bittoo bittoo is offline
Registered User
  
 

Join Date: Aug 2009
Posts: 16
Thanks Scrutinizer....Understood much better now..
Sponsored Links
Reply

Bookmarks

Tags
awk, cut, grep

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

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
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -4. The time now is 07:47 AM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0