How to print in awk matching $1 values ,to $1,$4 example given.?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting How to print in awk matching $1 values ,to $1,$4 example given.?
# 1  
Old 01-03-2013
How to print in awk matching $1 values ,to $1,$4 example given.?

Hi Experts,
I am trying to get the output from a matching pattern but unable to construct the awk command:

file :
Code:
aa bb cc 11
dd aa cc 33
cc 22 45 68
aa 33 44 44
dd aa cc 37
aa 33 44 67

I want the output to be : ( if $1 match to "aa" start of the line,then print $4 of that line, and if $1 matches to "dd" of start of that line then print $4 of that line).

I am trying with this:
Code:
 awk '/^aa/{A=$4};/^dd/{D=$4}END{print A,D}' file

But not working!, it is only printing the last finding seems to be.


The out put needs to be:
Code:
aa 11
dd 33
aa 44
dd 37
aa 67


Thanks.
# 2  
Old 01-03-2013
From what I see is that you are trying to print $1 and $4.

I don't see any complexions here on your output!
# 3  
Old 01-03-2013
awk

Hey,

Try something like this,

Code:
awk '/^aa|^dd/{print $1,$4;}' file

Cheers,
RangaSmilie
This User Gave Thanks to rangarasan For This Post:
# 4  
Old 01-03-2013
Code:
awk '$1=="aa" || $1=="dd" {print $1,$4}'

Code:
aa 11
dd 33
aa 44
dd 37
aa 67

This User Gave Thanks to Jotne For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

awk print matching records and occurences of each record

Hi all , I have two files : dblp.xml with dblp records and itu1.txt with faculty members records. I need to find out how many dblp records are related to the faculty members. More specific: I need to find out which names from itu1.txt are a match in dblp. xml file , print them and show how many... (4 Replies)
Discussion started by: iori
4 Replies

2. UNIX for Advanced & Expert Users

awk print all fields except matching regex

grep -v will exclude matching lines, but I want something that will print all lines but exclude a matching field. The pattern that I want excluded is '/mnt/svn' If there is a better solution than awk I am happy to hear about it, but I would like to see this done in awk as well. I know I can... (11 Replies)
Discussion started by: glev2005
11 Replies

3. Shell Programming and Scripting

awk help - matching a field with certail values

Hello there, I have a file with few fields separated by ":". I wrote a below awk to manipulate this file: awk 'BEGIN { FS=OFS=":" }\ NR != 1 && $2 !~ /^98/ && $8 !~ /^6/{print $0}' $in_file > $out_file What I wanted was that if $8 field contains any of the values - 6100, 6110, 6200 -... (2 Replies)
Discussion started by: juzz4fun
2 Replies

4. Shell Programming and Scripting

Print columns matching to specific values

Hello Friends, I have a CDR file and i need to print out 2 columns with their field position which matches to some constant values, a part of input file CZ=1|CZA=1|DIAL=415483420001|EE=13|ESF=1|ET=|FF=0|9|MNC=99|MNP=9041|MTC=0|NID=2|NOA=international|ON=1| OutPut ... (3 Replies)
Discussion started by: EAGL€
3 Replies

5. Shell Programming and Scripting

Compare values in two files. For matching rows print corresponding values from File 1 in File2.

- I have two files (File 1 and File 2) and the contents of the files are mentioned below. - I am trying to compare the values of Column1 of File1 with Column1 of File2. If a match is found, print the corresponding value from Column2 of File1 in Column5 of File2. - I tried to modify and use... (10 Replies)
Discussion started by: Santoshbn
10 Replies

6. Shell Programming and Scripting

How to print first matching range in awk?

following is input - <Schema> <schema_name>admin</schema_name> <Version>1.1</Version> <schema_name>admin</schema_name> <Version>1.2</Version> </Schema> ... (12 Replies)
Discussion started by: thearpit
12 Replies

7. Shell Programming and Scripting

How to Print from matching word to end using awk

Team, Could some one help me in Printing from matching word to end using awk For ex: Input: I am tester for now I am tester yesterday I am tester tomorrow O/p tester for now tester yesterday tester tomorrow i.e Starting from tester till end of sentence (5 Replies)
Discussion started by: mallak
5 Replies

8. Shell Programming and Scripting

Print lines matching value(s) in other file using awk

Hi, I have two comma separated files. I would like to see field 1 value of File1 exact match in field 2 of File2. If the value matches, then it should print matched lines from File2. I have achieved the results using cut, paste and egrep -f but I would like to use awk as it is efficient way and... (7 Replies)
Discussion started by: SBC
7 Replies

9. Shell Programming and Scripting

Print matching field using awk

Hi All, I have a string like below: str="Hold=True Map=False 'This will map the data' Run=Yes Modify=False" I want to print the field Run=Yes and retrive the value "Yes". I cannot use simple awk command because the position of the "Run" will be different at different times. Is there a way... (6 Replies)
Discussion started by: deepakgang
6 Replies

10. Shell Programming and Scripting

matching multiple values in awk

How will you change the 5th column in the data file with the value in the second column in the error_correction.txt file. You have to match an extra variable, column 3 of the error_correction file with column 6 of the data.txt file. data.txt: vgr,bugatti veron,,3.5,Maybe,6,.......,ax2,....... (0 Replies)
Discussion started by: VGR
0 Replies
Login or Register to Ask a Question