Help using SYSTEM function in NAWK


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Help using SYSTEM function in NAWK
# 1  
Old 12-30-2010
Help using SYSTEM function in NAWK

I am scanning a file (line by line) for format errors. A line could have multiple errors. Each field in the line is evaluated for errors and sent, along w/ any error messages, to a temporary file. Finally, if any errors were detected, this temporary file is then appended to the errorFile. The tmpFile is created containing all messages, but the errorFile is not created! Any ideas?
Code:
#!/usr/bin/bash
TMPFILE="tmpFile"
ERRFILE="errorFile"
nawk -v TMP=$TMPFILE -v ERR=$ERRFILE '{
  ... scan each field for errors and increment "error" as necessary
  print $1, "Error message ..." >> TMP
  print $2, "Error message ..." >> TMP
  ...
  if (error>0) system("cp TMP ERR")
}' xx.txt


Moderator's Comments:
Mod Comment Please use code tags when posting data and code samples, thank you.

Last edited by Franklin52; 12-30-2010 at 01:28 PM..
# 2  
Old 12-30-2010
I'm not sure if I understand your problem correctly. If not, please clarify.
I think that you are having problem with following cp command ran by system:
Code:
system("cp TMP ERR")

And you are expecting a file named "errorFile" to be created based on content of tmpFile.
If it is so, then problem is following:
argument to system command is being passed as string (In double quotes).HereTMP and ERR won't be treated as variable but just it's literal value.
And I guess there is no file with name "TMP", so cp will fail.
Even if there is a file called "TMP", ERROR file will be created with name "ERR".

You may try like below:
Code:
cmd="cp "TMP" "ERR;
if (error>0) system(cmd)

This User Gave Thanks to anurag.singh For This Post:
# 3  
Old 01-03-2011
That worked. Thank you.
# 4  
Old 01-03-2011
In your requirements, you stated:
Quote:
Originally Posted by aschera
<snip>
Finally, if any errors were detected, this temporary file is then appended to the errorFile.
<snip>
and the proposed to the system() call was:
Quote:
Originally Posted by anurag.singh
<snip>
Code:
cmd="cp " TMP " " ERR;
if (error>0) system(cmd)

<snip>
While correct, you do realize that cp overwrites the errorFile with tempFile -- it does not append. May I suggest:
Code:
#!/usr/bin/bash
ERRFILE="errorFile"

nawk -v ERR=$ERRFILE '{
  ... scan each field for errors
  print $1, "Error message ..." >> ERR
  print $2, "Error message ..." >> ERR
  ...
}' xx.txt

This will append the errors to errorFile, which is only created if an error is found.
# 5  
Old 01-05-2011
I am new to this forum, but not new to forums in general. It is refreshing that others are watching my back. Thanks.

I later realized that the cp would overwrite my file so I changed it to "cat" using the same format. The TMP file is being used to hold a intermediate report on the current record in question. This report potentially contains both errors and correct information, but I do not know until verifying the last field. If all is clean, the script loops back and reviews the next record.

Again, thank you for your advice.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. 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

2. UNIX for Dummies Questions & Answers

Using system function in C

Hi Guys , I want to use system function in C to do the following work. cp <file1> <file2> and then ><file1> e,g cp \var\log\cpm_cpmd_1.log.1 \var\log\cpm_cpmd_1.log.2 and then >\var\log\cpm_cpmd_1.log.1 1. g_config_info.cpmm_config.cpm_log_path=\var\log\ 2. ... (3 Replies)
Discussion started by: meet123321
3 Replies

3. Shell Programming and Scripting

NAWK - looping with match() function

i'm trying to use the "before" output from the match() function as part of the results of each Regex match... but... My input data: (from an input file) i only show the first record in my file.. all other records are similar. mds_ar/bin/uedw92wp.ksh:cat $AI_SQL/wkly_inqry.sql... (2 Replies)
Discussion started by: danmauer
2 Replies

4. UNIX for Dummies Questions & Answers

How to use ${?} and system() function???

Hi All, I am in a process to send the mail automatically with an attachment to bulk users. I have used 'Mailx' and 'Unencode' commands for the same and those worked fine. Please find below the same. X " ( cat /sastemp/body.txt; uuencode test.xls test.xls ) | mailx -s 'testing'... (1 Reply)
Discussion started by: manas6
1 Replies

5. Shell Programming and Scripting

NAWK getline function

Hi, I am using a Solaris OS here. My intention is to print the 2nd field if the first field matches "APPLE=". I am using the "getline" function here (shown below), but it doesn;t work. Can any experts give me some advice? Input: ORANGE= 10 APPLE= 20 MANGO= 30 GRAPES= 40 Output: 20 ... (5 Replies)
Discussion started by: Raynon
5 Replies

6. Shell Programming and Scripting

Nawk Time Function

Hi All, I am using solaris and nawk. Is there any time function in nawk which is simliar to the shell `date` function ? Can any experts show any examples? (4 Replies)
Discussion started by: Raynon
4 Replies

7. UNIX for Dummies Questions & Answers

System() function in <stdlib.h>

Hi, guys ,, I want to know the implementation of System() function in C Unix, and its prototype definition: int system(const char * string) in the header file <stdlib.h> ??! please help me ! because that is part of my project !! (0 Replies)
Discussion started by: someone33
0 Replies

8. Programming

system function in c

Hai Friends I have used the function system() to execute a command. My requirement is that i have to list the files in a directory applying some wildcard paterns. For example if i want to list *.c files i go with the function system("ls *.c"); and the output gets printed on the monitor.... (1 Reply)
Discussion started by: collins
1 Replies

9. Shell Programming and Scripting

Nawk user-defined function

HELP!!!! I am in an on-line shell programming class and have a question. Here is the data: Mike Harrington:(510) 548-1278:250:100:175 Christian Dobbins:(408) 538-2358:155:90:201 Susan Dalsass:(206) 654-6279:250:60:50 (There are 12 contribuors total) This database contains names, phone... (1 Reply)
Discussion started by: NewbieGirl
1 Replies

10. Programming

system() function call...

Hi, Though I should check this myself.. but I don't have a cc compiler at this time.. :( When I compile a c program containing say system(myshell.sh).. do the executable require that the actual script myshell.sh to be present whenever it executes? I guess it needs.. otherwise I can just... (1 Reply)
Discussion started by: Vishnu
1 Replies
Login or Register to Ask a Question