problem in redirecting records using nawk


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting problem in redirecting records using nawk
# 1  
Old 02-06-2012
problem in redirecting records using nawk

I am trying to redirect record to two files using nawk if-else.

Code:
 
#Identify good and bad records and redirect records using if-then-else 
nawk -F"|" '{if(NF!=14){printf("%s\n",$0) >> "$fn"_bad_data}else{printf("%s\n",$0) >> $fn}}' "$fn".orig

"$fn".orig is the source file name
bad records to "$fn"_bad_data andgood records to fn

when i execute the above command i get an error stating

Code:
 
ERROR:
 
nawk: syntax error at source line 1
 context is
        {if(NF!=14){printf("%s\n",$0) >> >>>  "$fn"_bad_data <<< }else{printf("%s\n",$0) >> $fn}}
nawk: illegal statement at source line 1

Smilie

Please help me understand the error and give a solution.
Thanks in advance..
# 2  
Old 02-06-2012
You will need to define variable $fn with -v and call it with fn in awk
Code:
nawk -v fn=$fn -F"|" '{if(NF!=14){printf("%s\n",$0) >> fn"_bad_data"}else{printf("%s\n",$0) >> fn }}' $fn.orig

Hope that helps.

Regards
Peasant.
# 3  
Old 02-06-2012
Quote:
Originally Posted by Peasant
You will need to define variable $fn with -v and call it with fn in awk
Code:
nawk -v fn=$fn -F"|" '{if(NF!=14){printf("%s\n",$0) >> fn"_bad_data"}else{printf("%s\n",$0) >> fn }}' $fn.orig

Hope that helps.

Regards
Peasant.
Thanks Peasant...
I tried above but it worked Smilie

Code:
 
sam:/data/input: nawk -v fn=$fn -F"|" '{if(NF!=14){printf("%s\n",$0) >> fn"_bad_data"}else{printf("%s\n",$0) >> fn }}' $fn.orig
 
nawk: syntax error at source line 1
 context is
        {if(NF!=14){printf("%s\n",$0) >> >>>  fn"_bad_data" <<<
nawk: illegal statement at source line 1

i tried hardcoding the file name too but experieinced the same Smilie Smilie
# 4  
Old 02-06-2012
I'm unable to replicate error using gawk.

Can you hardcode the fn"bad_data" as "fn_bad_data"
That should create fn_bad_data file when ran.

If that works, make a another variable with -v like fn_bad=$fn_bad_data and call in code as fn_bad_data (without the double quotes).
You will have then nawk -v fn=$fn -v fn_bad=$fn_bad_date ...

Regards
Peasant.
This User Gave Thanks to Peasant For This Post:
# 5  
Old 02-07-2012
When I hardcode the file name inside double quotes, say "bad_file.txt", as below nawk works fine.
Code:
nawk -F"|" '{if(NF!=14){printf("%s\n",$0) >> "bad_file.txt"} else {printf("%s\n",$0) >> "good_file.txt"}}' input_file.txt

but when i try using the variable as below i am getting error.
Code:
nawk -F"|" '{if(NF!=14){printf("%s\n",$0) >> "$bd_file"} else {printf("%s\n",$0) >> "$fn"}}' $fn_orig

Code:
 
Error:
nawk: syntax error at source line 1
context is
        {if(NF!=14){printf("%s\n",$0) >> >>>  "$bd_file" <<< }else{printf("%s\n",$0) >> "$fn"}}
nawk: illegal statement at source line 1

bd_file --> bad_file.txt
fn --> good_file.txt

someone plz help me as how to overcome this error.

Last edited by Franklin52; 02-07-2012 at 04:54 AM.. Reason: Code tags
# 6  
Old 02-07-2012
Like Peasant suggests, you need to use awk variables like, for example
Code:
awk -v fn=$fn -v bd_file="${fn}_bad_file.txt" {'if(NF!=14)print >> bd_file;else print >> fn}'

# 7  
Old 02-10-2012
Question

Quote:
Originally Posted by Peasant
I'm unable to replicate error using gawk.

Can you hardcode the fn"bad_data" as "fn_bad_data"
That should create fn_bad_data file when ran.

If that works, make a another variable with -v like fn_bad=$fn_bad_data and call in code as fn_bad_data (without the double quotes).
You will have then nawk -v fn=$fn -v fn_bad=$fn_bad_date ...

Regards
Peasant.

I tried assigning the file names to a variable as below but facing issues with awk syntax

Code:
 
sam:/data/input: awk -v fn=$fn -v bd_file=$bd_file -F"|" '{if(NF!=14)print >> bd_file;else print >> fn}' $fn_orig

awk: syntax error near line 1
awk: bailing out near line 1

Please help me with the syntax.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Redirecting records with non-printable characters

Hi, I have a huge file (50 Mil rows) which has certain non-printable ASCII characters in it. I am cleaning the file by deleting those characters using the following command - tr -cd '\11\12\15\40-\176' < unclean_file > clean_file Please note that I am excluding the following - tab,... (6 Replies)
Discussion started by: rishigc
6 Replies

2. Shell Programming and Scripting

Nawk Problem - nawk out of space in tostring on

Hi.. i am running nawk scripts on solaris system to get records of file1 not in file2 and find duplicate records in a while with the following scripts -compare nawk 'NR==FNR{a++;next;} !a {print"line"FNR $0}' file1 file2duplicate - nawk '{a++}END{for(i in a){if(a-1)print i,a}}' file1in the middle... (12 Replies)
Discussion started by: Abhiraj Singh
12 Replies

3. Shell Programming and Scripting

Nawk if logic problem

nawk '{ fmt="%3s %22s %48s %35s %21s\n"; if ($3==$6 && $1=="STOPLOSS") { tpy="Successful Match"; jnme=$1; sts="File will be loaded"; cntrl=$3; audit=$6; printf (fmt, tpy,jnme,sts,cntrl,audit) >> "'${AUDIT_DATA_FILE}/${AUDIT36}'" }else if ($3!=$6 && $1=="STOPLOSS") { tpy="Mis-Match ";... (4 Replies)
Discussion started by: wawa
4 Replies

4. Shell Programming and Scripting

Redirecting stdout problem

I have a simple bash script that prints sth every 5 seconds. What I do is the following. I redirect the output of the script to a file, tail the file and see that it works and then from another console I delete the file where the output is redirected to. Even though I have deleted the file, the... (2 Replies)
Discussion started by: igurov
2 Replies

5. Shell Programming and Scripting

Nawk script to compare records of a file based on a particular column.

Hi Gurus, I am struggling with nawk command where i am processing a file based on columns. Here is the sample data file. UM113570248|24-AUG-11|4|man1|RR211 Alert: Master Process failure |24-AUG-11 UM113570624|24-AUG-11|4|man1| Alert: Pattern 'E_DCLeDAOException' found |24-AUG-11... (7 Replies)
Discussion started by: usha rao
7 Replies

6. Shell Programming and Scripting

Nawk, creating a variable total from multiple lines(records)

Good Morning/Afternoon All, I am having some trouble creating a variable called "total" to display the sum of the values in a specific field, $6 for example. The data I am working on is in the following form: John Doe:(555) 555-5555:1:2:3 Jane Doe:(544) 444-5556:4:5:6 Moe Doe:(654)... (2 Replies)
Discussion started by: SEinT
2 Replies

7. Shell Programming and Scripting

nawk -- separation of records on basis of number of fields

A file file1.txt exists having records like The delimiter being "|" X|_|Y|_|Z|_| (number of fields 7) A|_|B|_| (number of fields 5) X|_|Z|_|H|_| (number of fields 7) A|_|D|_|S|_| (number of... (4 Replies)
Discussion started by: centurion_13
4 Replies

8. Shell Programming and Scripting

Extract CSV records using NAWK?

Example CSV: $ cat myfile HDR COL_A,COL_B,COL_C X,Y,Z Z,Y,X ... X,W,Z In this example, I know that column names are on the second line. I also know that I would like to print lines where COL_A="X" and COL_C="Z". In this simple example, I know that COL_A = $1 and COL_C = $3, and hence... (6 Replies)
Discussion started by: cs03dmj
6 Replies

9. Shell Programming and Scripting

Redirecting stderr problem

% ls -ld /usr /foo ls: /foo: No such file or directory drwxr-xr-x 14 root wheel 512 May 18 02:49 /usr % ls -ld /usr /foo 1>/dev/null/ /dev/null/: Not a directory. % ls -ld /usr /foo 2>/dev/null/ /dev/null/: Not a directory. ^^Why why why doesn't this work for me. Furthermore, where is... (7 Replies)
Discussion started by: phpfreak
7 Replies

10. Shell Programming and Scripting

nawk problem

How are ya, Heres the problem. I have a line of data that can either be in this format. "20" or "20kg" for the 20kg one i need to be able to read that their is kg on the end of this field and then ignore it and move on to the next line. Can anyone help. Cheers (3 Replies)
Discussion started by: ben_shark
3 Replies
Login or Register to Ask a Question