print column value after exact match of variables in file


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting print column value after exact match of variables in file
# 1  
Old 11-17-2011
print column value after exact match of variables in file

I have file like below

Code:
  summit              hvar_rgrpd_10d_hvams17_                 _kgr_prod.rec       checksum            checksum            us        europe
  summit              hvar_rgrpd_10d_hvams17_                 _kgr_prod.xml       var                 summit              us        Europe

now I have two variables

Code:
hvar_rgrpd_10d_hvams17_ and _kgr_prod.xml

now I need to print column 6 if there is exact match for var1 and var2 for field 2 and field 3
like it should give me
Code:
 US

whatever I'm trying it's always giving two records of US . but it should give me field 6 value of first row as field2 and field3 are exact match to var1 and var2.
# 2  
Old 11-17-2011
Please show the code.
# 3  
Old 11-17-2011
this is how it looks like but hard coded value

Code:
awk '/hvar_rgrpd_10d_hvams17_/ { if ( $3 ~ /_kgr_prod.rec/) print $6}'

but question how to pass the variable , it doesn't show up with any outcome

Code:
 var1=hvar_rgrpd_10d_hvams17_
var2=_kgr_prod.rec
awk -v first="$var1" -v second="$var2" '/first/ { if ( $3 ~ /second/) print $6}' filename

# 4  
Old 11-17-2011
You can run this script:
Code:
#!/usr/bin/ksh
mVar1='hvar_rgrpd_10d_hvams17_'
mVar2='_kgr_prod.xml'
while read m1 m2 m3 m4 m5 m6 m7; do
  if [[ "${mVar1}" = "${m2}" && "${mVar2}" = "${m3}" ]]; then
    echo ${m6}
  fi
done < File

# 5  
Old 11-17-2011
you need to check the exact match, not what '~' does which is a regex match:
Code:
nawk -v f2=hvar_rgrpd_10d_hvams17_ -v f3=_kgr_prod.rec '$2==f2 && $3==f3 {print $6}' myFile

# 6  
Old 11-17-2011
Quote:
Originally Posted by manas_ranjan
but question how to pass the variable , it doesn't show up with any outcome

Code:
 var1=hvar_rgrpd_10d_hvams17_
var2=_kgr_prod.rec
awk -v first="$var1" -v second="$var2" '/first/ { if ( $3 ~ /second/) print $6}' filename

this does a regex match of the string 'first' and the string 'second':
Code:
awk -v first="$var1" -v second="$var2" '$0 ~ first { if ( $3 ~ second) print $6}' filename

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Compare 1st column from 2 file and if match print line from 1st file and append column 7 from 2nd

hi I have 2 file with more than 10 columns for both 1st file apple,0,0,0...... orange,1,2,3..... mango,2,4,5..... 2nd file apple,2,3,4,5,6,7... orange,2,3,4,5,6,8... watermerlon,2,3,4,5,6,abc... mango,5,6,7,4,6,def.... (1 Reply)
Discussion started by: tententen
1 Replies

2. Shell Programming and Scripting

awk to update file based on partial match in field1 and exact match in field2

I am trying to create a cronjob that will run on startup that will look at a list.txt file to see if there is a later version of a database using database.txt as the source. The matching lines are written to output. $1 in database.txt will be in list.txt as a partial match. $2 of database.txt... (2 Replies)
Discussion started by: cmccabe
2 Replies

3. Shell Programming and Scripting

Input file needs to match a column and print the entire line

I have a file with class c IP addresses that I need to match to a column and print the matching lines of another file. I started playing with grep -if file01.out file02.out but I am stuck as to how to match it to a column and print the matching lines; cat file01.out 10.150.140... (5 Replies)
Discussion started by: lewk
5 Replies

4. Shell Programming and Scripting

Regex: print matched line and exact pattern match

Hi experts, I have a file with regexes which is used for automatic searches on several files (40+ GB). To do some postprocessing with the grep result I need the matching line as well as the match itself. I know that the latter could be achieved with grep's -o option. But I'm not aware of a... (2 Replies)
Discussion started by: stresing
2 Replies

5. Shell Programming and Scripting

awk Print New Column For Every Two Lines and Match On Multiple Column Values to print another column

Hi, My input files is like this axis1 0 1 10 axis2 0 1 5 axis1 1 2 -4 axis2 2 3 -3 axis1 3 4 5 axis2 3 4 -1 axis1 4 5 -6 axis2 4 5 1 Now, these are my following tasks 1. Print a first column for every two rows that has the same value followed by a string. 2. Match on the... (3 Replies)
Discussion started by: jacobs.smith
3 Replies

6. Shell Programming and Scripting

print when column match with other file

Hello all, please help. There are two file like this: file1: 1197510.0 294777.7 9666973.0 21.6 1839.8 1197510.0 294777.7 9666973.0 413.2 2075.9 1197510.0 294777.7 9666973.0 689.3 2260.0 ... (1 Reply)
Discussion started by: attila
1 Replies

7. Shell Programming and Scripting

print lines with exact pattern match

I have in a file domain.com. 1909 IN A 1.22.33.44 domain.com. 1909 IN A 22.33.44.55 ns1.domain.com. 1699 IN A 33.44.55.66 ns2.domain.com. 1806 IN A 77.77.66.66 I need to "grep" or "awk" out the lines starting with domain.com. as follows. domain.com. 1909 IN A 1.22.33.44 domain.com.... (3 Replies)
Discussion started by: anilcliff
3 Replies

8. Shell Programming and Scripting

Strings from one file which exactly match to the 1st column of other file and then print lines.

Hi, I have two files. 1st file has 1 column (huge file containing ~19200000 lines) and 2nd file has 2 columns (small file containing ~6000 lines). ################################# huge_file.txt a a ab b ################################## small_file.txt a 1.5 b 2.5 ab ... (4 Replies)
Discussion started by: AshwaniSharma09
4 Replies

9. Shell Programming and Scripting

exact string match ; search and print match

I am trying to match a pattern exactly in a shell script. I have tried two methods awk '/\<mpath${CURR_MP}\>/{print $1 $2}' multipath perl -ne '/\bmpath${CURR_MP}\b/ and print' /var/tmp/multipath Both these methods require that I use the escape character. I am guessing that is why... (8 Replies)
Discussion started by: bash_in_my_head
8 Replies

10. Shell Programming and Scripting

Awk+Grep Input file needs to match a column and print the entire line

I'm having problems since few days ago, and i'm not able to make it works with a simple awk+grep script (or other way to do this). For example, i have a input file1.txt: cat inputfile1.txt 218299910417 1172051195 1172070231 1172073514 1183135117 1183135118 1183135119 1281440202 ... (3 Replies)
Discussion started by: poliver
3 Replies
Login or Register to Ask a Question