Testing success of AWK code in KSH script


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Testing success of AWK code in KSH script
# 1  
Old 03-30-2009
Question Testing success of AWK code in KSH script

I need a bit of help here on something simple.

I have a KSH script (must be KSH) that needs to change 2 positional variables in a CSV script.

The CSV script looks like this:

00001,010109,01/01/2009 00:01:01
00008,090509,09/05/2009 13:47:26


My AWK script will change $2 and $3 based on the value of $1.
The values in $1 are distinct and must match to one of the values in the first column with every call to this script.

Code:
cat csv_file.csv | awk 'BEGIN {FS=","; OFS=FS}
  	{
  		if($1 == one) {
  			$2 = two
  			$3 = three
  		}
  	print 
  	}' one="${one}" two="${two}" three="${three}" > tmp_file.csv

So far it works fine.

What I'm wondering how to do is to test if the AWK code successfully completed.
If I call $? I always get a status of 0 (since the cat does work fine).

How do I verify that the AWK code did not error out?
# 2  
Old 03-30-2009
Hammer & Screwdriver

What about checking to see if "tmp_file.csv" was created?
Or, in alternate, if that file has a recent date/time stamp.
# 3  
Old 03-30-2009
Quote:
Originally Posted by joeyg
What about checking to see if "tmp_file.csv" was created?
Or, in alternate, if that file has a recent date/time stamp.
I thought of that, but the code as written creates tmp_file.csv regardless of finding a match or not.
# 4  
Old 03-30-2009
Tools what about checking for non-zero file

Code:
if [ ! -s myfile ]
   then
   echo "created a zero-length file"
fi

# 5  
Old 03-30-2009
Unfortunately, that does not work either.

I have to take whatever data is in csv_file.csv (orig. file) and carry it through to the tmp file (tmp_file.csv). I need to move the entire 'file' which consists of many 'records' and only change the data in position 2 and 3 on a match to the data in position 1.

The print outside of the IF statement in the awk code allows all data to pass through to the tmp file, so even when the IF statement fails (awk) I still get a file with all data, but no match.

Was hoping to catch this in the awk statement. Guess I'll need to write more code to catch this type of error earlier.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

sed commands success / fail from commandline vs ksh script

solaris 5.10 Generic_138888-03 sun4v sparc SUNW,Sun-Fire-T200 I need a sed command that tests true when presented with lines that contain either forward and backslash. input file: c:/myFile.txt c:\yourFile.txt It doesn't appear that sed (in my environment anyway) supports... (4 Replies)
Discussion started by: msutfin
4 Replies

2. Shell Programming and Scripting

awk issue expanding variables in ksh script

Hi Guys, I have an issue with awk and variables. I have trawled the internet and forums but can't seem to get the exactt syntax I need. I have tried using awk -v and all sorts of variations but I have hit a brick wall. I have spent a full day on this and am just going round in circles. ... (3 Replies)
Discussion started by: gazza-o
3 Replies

3. Shell Programming and Scripting

Extract record in shell script,so far using awk without success

hello, I require help in the following. There is a report similar to the ones shown here. I need to do couple of things 10/07 12:47 0003210042 R TENN ANVISER0 DELF96A.V1.O.TENRREG 120710 124657 U 00000 DELFOR 1 4331 10/07 12:47 0003210043 S TENN... (2 Replies)
Discussion started by: rakeshv
2 Replies

4. Shell Programming and Scripting

for loop in awk script using ksh

Guys, I am new in awk , I face problem while i try to use for loop in awk, I am using ksh, i am trying to set a for loop which runs as man times as the records in a file , the for loop like for(a=1;a<=5;a++) is working in my awk script but the one i need is not working :wall: for example ... (8 Replies)
Discussion started by: djahmed
8 Replies

5. Shell Programming and Scripting

help with ksh/awk/sed script, random # of fields

Hello all, I'm working on an attendance callout script for a school district. I need to change our current layout for the vendor. Currently the data is in the form of: studentid,period,building, Heres a sample of some made up records: 500,1,30, 500,2,30, 500,3,30, 500,6,30,... (7 Replies)
Discussion started by: axo959
7 Replies

6. Shell Programming and Scripting

Awk in ksh shell script - help

Hello, Im a beginner. Im writing a ksh script with awk. Is it possible to assign the output of the awk to a shell variable? Like, shell_variable= awk '$1 == "shell" {abc= $2 }' /tmp/cust_det echo $shell_variable Please excuse my ignorance. Thanks in advance. (4 Replies)
Discussion started by: Nic_writes
4 Replies

7. Shell Programming and Scripting

need help with simple awk/ksh script

I need help finding out why this script wont run. The chmod is okay, but i get an error saying that I need '&&' on line 5. (18 Replies)
Discussion started by: tefflox
18 Replies

8. Shell Programming and Scripting

passing variables to awk from ksh script

I'm trying to write a ksh script that uses awk, but I want to pass variables to awk. For example (not working): if ];then searchstr=$1 lsof -i | awk '{if($9~/SEARCHSTR/) print $2} SEARCHSTR=$searchstr' else echo "usage: $0 <search string>" fi I tried several options. Is it... (3 Replies)
Discussion started by: rein
3 Replies

9. Shell Programming and Scripting

Testing ssh connection from KSH script

Hi. I have a kornshell script that runs on a daily basis as a cron job. Part of what the script does is copy the folder contents from another server to the current server (server where KSH script is running). I have a scp command, as follows: scp $REMOTE_HOST:$REMOTE_FILE_DIR/* $TMP_DIR ... (8 Replies)
Discussion started by: dmilks
8 Replies

10. Shell Programming and Scripting

script ksh/awk/grep

How can I extract the 9th line of a text file. thanks vm.:confused: (2 Replies)
Discussion started by: hoang
2 Replies
Login or Register to Ask a Question