Visit Our UNIX and Linux User Community


Using grep and cut within awk


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Using grep and cut within awk
# 1  
Old 11-06-2009
Error 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; 11-06-2009 at 09:21 AM.. Reason: Use code tags, please!
# 2  
Old 11-06-2009
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  
Old 11-06-2009
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  
Old 11-09-2009
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  
Old 11-09-2009
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  
Old 11-10-2009
Thanks Scrutinizer....Understood much better now..

Previous Thread | Next Thread
Test Your Knowledge in Computers #501
Difficulty: Easy
If a piece of code adds two numbers together and stores the result, there should be a comment that explains the process in the source code.
True or False?

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Using a combination of sort/cut/grep/awk/join/paste/sed

I have a file and need to only select users that have a shell of “/bin/bash” in the line using awk or sed please help (4 Replies)
Discussion started by: boyboy1212
4 Replies

2. Shell Programming and Scripting

grep with cut option??

This is my command-line code in my script, passwd=`grep $passwd $userfile | cut -f2 -d: login_users > retrieve` the cut -f2 -d: login_users > retrieve searches and prints the whole column two. what I need is one of the items in the column two only.. what option can I add to my cut... (2 Replies)
Discussion started by: jenimesh19
2 Replies

3. Slackware

How should I cut this line using cut and grep?

not sure how to do it. wan't to delete it using cut and grep ince i would use it in the shell. but how must the command be? grep "64.233.181.103 wwwGoogle.com" /etc/hosts | cut -d the delimeter is just a space. can you help meplease. :D (1 Reply)
Discussion started by: garfish
1 Replies

4. Shell Programming and Scripting

GREP/CUT/AWK to Arrays

hi people, I have a text file containing data, seperated by TAB. I want to process this tab'ed data as variable. how can I assign this? Ex: Code: 11aaa 12000 13aaa 14aaa 15aaa 16aaa 17aaa 21aaa 22000 23aaa 24aaa 25aaa 26aaa 27aaa 31aaa 32000 33aaa 34aaa 35aaa 36aaa 37aaa... (1 Reply)
Discussion started by: gc_sw
1 Replies

5. UNIX for Dummies Questions & Answers

Awk/sed solution for grep,cut

Hi, From the file "example" with lines like below, I need the int value associated with ENG , i.e, 123 SUB: ENG123, GROUP 1 SUB: HIS124, GROUP 1 .. .. Normally , i do grep ENG example | cut -d ' ' -f 2 | cut -c 4-6 Is it possible to do it in simpler way using awk/sed ? ... (5 Replies)
Discussion started by: priyam
5 Replies

6. Shell Programming and Scripting

Sed Awk Cut Grep Combination Help ?

I have been reading for a few hours trying to educate myself enough to accomplish this task, so please know I have performed some research. Unfortunately, I am not a *NIX scripting expert, or a coder. I come from a network background instead. SO, here is my desired outcome. I have some Cisco... (5 Replies)
Discussion started by: abbzer0
5 Replies

7. Shell Programming and Scripting

[grep awk cut] > awk

Hi, I'm very new to scripting. grep $s $filename | awk '{print $2}' | cut -c 1-8 How can I optimize this using a single awk? I tried: awk '/$s/ {print $2}' $filename | cut -c 1-8 However didn't work, I think the awk is not recognizing $s and the verbal is something else. (6 Replies)
Discussion started by: firdousamir
6 Replies

8. Shell Programming and Scripting

cut sed grep or other?

Hi Is there a way to cut the last two characters off a word or number given that this word or number can be of varying length? I have tried something like TEST=`echo $OLD | cut -c 1-5` where $OLD is a variable containing a number like 1234567 which gives a result of 12345. This is fine... (4 Replies)
Discussion started by: rleebife
4 Replies

9. Shell Programming and Scripting

grep and cut....

hi, i have a simple question: in hpux 11i; this cmd : cat mailtest |grep sekar >test1 gives the output file with the name test1. i want to remove the line which contains the "sekar" and put the result in the new file.... what is the command for that?.. (2 Replies)
Discussion started by: sekar sundaram
2 Replies

Featured Tech Videos