Awk output to file in IF statement


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Awk output to file in IF statement
# 1  
Old 04-07-2009
Awk output to file in IF statement

Hi all,

I currently have the following script in 1) which outputs to a file if the 3rd column is equal to the message_id value. This works. However what I want to do is incorporate an else statement and output to a different file if the 3rd column doesnt equal the message_id values. I tried that script shown in 2) but it doesnt work. Any ideas?

1)

for message_id in 15 16 17; do
cat $path/temp/hourly_file|egrep -v "val1|val2"|awk -F, '{
if ($3=="'$message_id'")
print $0
}'|sort -t',' -k4 >> $path/temp/outputfile_msgs
done

2)

for message_id in 15 16 17; do
cat $path/temp/hourly_file|egrep -v "val1|val2"|awk -F, '{
if ($3=="'$message_id'")
print $0 >> $path/temp/outputfile_msgs
else
print $0 >> $path/temp/other_outputfile_msgs
}'
done

Many thanks!
# 2  
Old 04-07-2009
Error Find the "Word" in the File and display in the Screen or redirect to some file

Sorry
# 3  
Old 04-07-2009
Yep that's what I'm trying to do but I can't get it to work with an else statement in Awk....
# 4  
Old 04-07-2009
Code:
for message_id in 15 16 17; do
awk -F, -v msg="${message_id}" -v excl='val1|val2'  '$0 !~ excl && $3==msg' $path/temp/hourly_file |sort -t',' -k4 >> $path/temp/outputfile_msgs
done

# 5  
Old 04-07-2009
Hi, thanks for the response.

Isn't that just doing what my original script in 1) does? What I want to do is output to a file if the 3rd coulmn does match a message_id value, and output to a DIFFERENT file if the 3rd column doesn't match one of the message_id values.

Thanks
# 6  
Old 04-07-2009
Quote:
Originally Posted by Donkey25
Hi, thanks for the response.

Isn't that just doing what my original script in 1) does? What I want to do is output to a file if the 3rd coulmn does match a message_id value, and output to a DIFFERENT file if the 3rd column doesn't match one of the message_id values.

Thanks
Code:
for message_id in 15 16 17; do
awk -F, -v msg="${message_id}" -v excl='val1|val2' -v msgMatch="$path/temp/match" -v msgNomatch="$path/temp/nomatch" '$0 !~ excl { out=($3==msg) ? msgMatch : msgNomatch; print > out' $path/temp/hourly_file
done

# 7  
Old 04-07-2009
Hi,

Thanks again.

I'm getting a syntax error. I think there's a missing curly right bracket but I don't know where it should go!
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

awk to reformat output if input file is empty, but not if file has data in it

The below awk improved bu @MadeInGermany, works great as long as the input file has data in it in the below format: input chrX 25031028 25031925 chrX:25031028-25031925 ARX 631 18 chrX 25031028 25031925 chrX:25031028-25031925 ARX 632 14... (3 Replies)
Discussion started by: cmccabe
3 Replies

2. Shell Programming and Scripting

Convert Update statement into Insert statement in UNIX using awk, sed....

Hi folks, I have a scenario to convert the update statements into insert statements using shell script (awk, sed...) or in database using regex. I have a bunch of update statements with all columns in a file which I need to convert into insert statements. UPDATE TABLE_A SET COL1=1 WHERE... (0 Replies)
Discussion started by: dev123
0 Replies

3. UNIX for Dummies Questions & Answers

How to pass cat file in awk statement?

Hi, I am working on kernel parameters, want to check values under /proc/sys/kernel below I tried for kernel.sem SEMMNS: 4096 cat /proc/sys/kernel/sem | awk '{print $2}' awk '{ if ($2 < 33000) print }' /proc/sys/kernel/sem |awk '{print $2}' 32000 The above... (7 Replies)
Discussion started by: stew
7 Replies

4. Shell Programming and Scripting

Need help with awk statement to break nth column in csv file into 3 separate columns

Hello Members, I have a csv file in the format below. Need help with awk statement to break nth column into 3 separate columns and export the changes to new file. input file --> file.csv cat file.csv|less "product/fruit/mango","location/asia/india","type/alphonso" need output in... (2 Replies)
Discussion started by: awk-admirer
2 Replies

5. Shell Programming and Scripting

Help with awk statement to format text file.

Hello, I am fairly new to shellscripting and have written a script to check on messages file and report failed logins: Here is the original file: Jul 17 03:38:07 sfldmilx086 sshd: error: PAM: Authentication failure for houghn97 from 10.135.77.201 Jul 17 03:38:07 sfldmilx086 sshd: error:... (2 Replies)
Discussion started by: neilh1704
2 Replies

6. Shell Programming and Scripting

Awk script to run a sql and print the output to an output file

Hi All, I have around 900 Select Sql's which I would like to run in an awk script and print the output of those sql's in an txt file. Can you anyone pls let me know how do I do it and execute the awk script? Thanks. (4 Replies)
Discussion started by: adept
4 Replies

7. Shell Programming and Scripting

KSH - How to use a file as input to an IF or AWK statement

Hi, I have a ksh script where I have an awk statement to exclude a few items... but my "few items" has now grown substantially and I'm looking for a nice compact way of doing this. Maybe putting the exceptions into a file or something? Any suggestions greatly appreciated. # Excluded items... (1 Reply)
Discussion started by: right_coaster
1 Replies

8. Shell Programming and Scripting

Parse file using awk and work in awk output

hi guys, i want to parse a file using public function, the file contain raw data in the below format i want to get the output like this to load it to Oracle DB MARWA1,BSS:26,1,3,0,0,0,0,0.00,22,22,22.00 MARWA2,BSS:26,1,3,0,0,0,0,0.00,22,22,22.00 this the file raw format: Number of... (6 Replies)
Discussion started by: dagigg
6 Replies

9. Shell Programming and Scripting

Redirect output of print statement to file

:confused: I have a ksh script which gathers data from a file. I need to find a way to direct the output to a new file. The code looks something like this: DUP_FILE=`touch /export/.../.../dup_social_$1` while (($# > 0 )) do # Make sure the file exists if ] then print "$1: does not... (4 Replies)
Discussion started by: Muga801
4 Replies

10. HP-UX

SQL statement output to Log file-How?

Hi all, I need to bring the message to log file.Teradat/Hp-Ux script: ----- ### Update Log Table bteq <<- EOC .run file ${SRC_DATA}/logon.txt .run file ${SRC_DATA}/dbstagebteq.txt .set format off .set foldline off all .set sidetitles off ... (1 Reply)
Discussion started by: vsubbu1000
1 Replies
Login or Register to Ask a Question