Reading input record from inside nawk


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Reading input record from inside nawk
# 1  
Old 05-31-2011
Reading input record from inside nawk

Hi friends,

I have small query with reg to awk search pattern..

below is my sample file and code which i tried..

Code:
$ cat file.txt
xxx,yyyyy,messageID,sha
xxxx,errorcode,messageID,name

in the above sample file - let assume I know the errorcode(2nd record) using which I want to find out sha record(1st line last column), the key is messageID which would be common for both the record.

I have tried following awk syntax but failing Smilie

Code:
nawk -F"," '{if($0~/errorcode/) {var=$3;if($0~/var/ && $0!~/errorcode/) print $NF}}' file.txt

Thanks
SHa

---------- Post updated at 03:51 AM ---------- Previous update was at 03:47 AM ----------

Just to add above statement for better understanding...the record which I am searching in above syntax can be anywhere not always one line above or below...thats y i have not tried getline etc....

Thanks
Sha
# 2  
Old 05-31-2011
something like:

Code:
#  nawk -F"," '{($2~"errorcode")  ? t[$3]=$2 :  s[$3]=$4 }END{for (x in t){print x,t[x],s[x]}}' infile
messageID errorcode sha

should be close to what you're after.

HTH

Last edited by Tytalus; 05-31-2011 at 10:37 AM.. Reason: tidier version
This User Gave Thanks to Tytalus For This Post:
# 3  
Old 05-31-2011
Cool...worked perfectly...would you please also explain bit on for loop part how does it work....which is bit confusing me.........Smilie

Anyway...Thanks a lot..

Thanks
SHa
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

How to avoid arguments inside Nawk command?

Hi, Here is my command print $2 was meant to select the second column however, it is getting substituted with the second argument that was passed to the script. Can you please tell me how can I resolve this ? (6 Replies)
Discussion started by: mohtashims
6 Replies

2. Shell Programming and Scripting

Substitute variable inside nawk

Hi, I need to set "prd" in the below command to a unix variable nawk '/^#/ {next} FNR==NR {prd;next} !($0 in prd)' So, this is what i did fname=prd // unix shell variable nawk -v fname=$fname '/^#/ {next} FNR==NR {fname;next} !($0 in fname)'But the value of fname i.e "prd" is not... (8 Replies)
Discussion started by: mohtashims
8 Replies

3. Shell Programming and Scripting

Update file record inside read loop

Hi, I am reading file records inside a while loop, and want to update the record when certain condition is met. How can I update a file while being read? I want to avoid using temporary files, copy, rename, ... while IFS=',' read -r f1 f2 do function(f1,f2) if then <add... (1 Reply)
Discussion started by: ysrini
1 Replies

4. UNIX for Dummies Questions & Answers

Reading only first record from the multipe directories

Hi All, I have a requirement, I had a parent directory Land under that we have sub directories Yesterday, Today and Tommorrow And we have a file test.txt under the above directories Yesterday, Today and Tommorrow The data in the file test.txt under Yesterday folder is ... (5 Replies)
Discussion started by: somu_june
5 Replies

5. Shell Programming and Scripting

Reading a file (one record) in a SHL script

I am trying to read a file in a shl script (only one record) and stored in a variable file_number I got the following read -u $BANNER_HOME/xxxxxxx/misc/EFTSQL.dat file_number file_number2 = $file_number + 1 echo $file_number2 > $BANNER_HOME/xxxxxx/misc/EFTSQL.dat EOF It is not working... (2 Replies)
Discussion started by: rechever
2 Replies

6. Shell Programming and Scripting

problem in reading a record from the file

Hi Guys, I need to check whether the last column is RP, If so, then i have to second column and pass it to a select statement as sonid and fetch the value to a variable and run it. This has to be done till the last column is RW. value Fatherid sonid topid ... (8 Replies)
Discussion started by: mac4rfree
8 Replies

7. Shell Programming and Scripting

using cp command inside nawk

Hello I have facing issue while using cp command inside nawk block. #!/bin/ksh my_name=$1 nawk -v my_name1=$my_name 'BEGIN { n = split(my_name1,names,":"); for (i=1;i<=n;i++) { print names; cpcmd = "cp " /tmp/test.txt" " ./sample system(cpcmd) } exit }' exit 0 i'am getting... (1 Reply)
Discussion started by: piscean_n
1 Replies

8. Shell Programming and Scripting

While loop reading a record

Hi, I am trying to create a while loop that will do the following: INFILE= list of new records that need to be added after last previous record while read record do find the last record processed create list of new records output to a file echo "$record">> $NEWFILE done ... (9 Replies)
Discussion started by: shortyball24
9 Replies

9. Shell Programming and Scripting

case command inside awk/nawk

well I found lot of topics about awk..about if command in awk.. but I had to implement this: nawk -F"|" ' $47 ~ /0R0011/ { print > ("/home/user/M/MC.tmp" )} $47 ~ /0R0012/ { print > ("/home/user/M/DuSI.tmp" )} $47 ~ /0R0014/ { print > ("/home/user/M/FF.tmp" )} $47 ~ /0R0018/ { print >... (9 Replies)
Discussion started by: abdulaziz
9 Replies

10. Shell Programming and Scripting

Reading specific contents from 1 input files and appending it to another input file

Hi guys, I am new to AWK and unix scripting. Please see below my problem and let me know if anyone you can help. I have 2 input files (example given below) Input file 2 is a standard file (it will not change) and we have to get the name (second column after comma) from it and append it... (5 Replies)
Discussion started by: sksahu
5 Replies
Login or Register to Ask a Question