Pass input and output file as parameter to awk script


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Pass input and output file as parameter to awk script
# 1  
Old 07-20-2009
Pass input and output file as parameter to awk script

Hi,
i am new to awk. I am using csv2pipe script(shown below)

BEGIN { FS=SUBSEP; OFS="|" }
{
result = setcsv($0, ",")
print
}
# setcsv(str, sep) - parse CSV (MS specification) input
# str, the string to be parsed. (Most likely $0.)
# sep, the separator between the values.
#
# After a call to setcsv the parsed fields are found in $1 to $NF.
# setcsv returns 1 on sucess and 0 on failure.
#
# By Peter Strvmberg aka PEZ.
# Based on setcsv by Adrian Davis. Modified to handle a separator
# of choice and embedded newlines. The basic approach is to take the
# burden off of the regular expression matching by replacing ambigious
# characters with characters unlikely to be found in the input. For
# this the characters "\035".
#
# Note 1. Prior to calling setcsv you must set FS to a character which
# can never be found in the input. (Consider SUBSEP.)
# Note 2. If setcsv can't find the closing double quote for the string
# in str it will consume the next line of input by calling
# getline and call itself until it finds the closing double
# qoute or no more input is available (considered a failiure).
# Note 3. Only the "" representation of a literal quote is supported.
# Note 4. setcsv will probably missbehave if sep used as a regular
# expression can match anything else than a call to index()
# would match.
#
function setcsv(str, sep, i) {
gsub(/""/, "\035", str)
gsub(sep, FS, str)
while (match(str, /"[^"]*"/)) {
middle = substr(str, RSTART+1, RLENGTH-2)
gsub(FS, sep, middle)
str = sprintf("%.*s%s%s", RSTART-1, str, middle,
substr(str, RSTART+RLENGTH))
}
if (index(str, "\"")) {
return ((getline) > 0) ? setcsv(str (RT != "" ? RT : RS) $0, sep) : !setcsv(str "\"", sep)
} else {
gsub(/\035/, "\"", str)
$0 = str
for (i = 1; i <= NF; i++)
if (match($i, /^"+$/))
$i = substr($i, 2)
$1 = $1 ""
return 1
}
}

I run it as
nawk -f csv2pipe.awk raw.txt

It works fine but gives the ouput on the console. I need it on the $1 parameter.
how should I modify "print" in the script so that the output is received on $1 instead of console.

I tried the following:
print > $1 ---> Doesn't work
print > "temp" --> Creates a new file temp and saves the output

Can anybody please help me out.

Thanks in advance.
# 2  
Old 07-20-2009
Keep your awk in standard format: read data from stdin and write data to the stdout. Caller tell the in and out using standard method like:
Code:
nawk -f csv2pipe.awk raw.txt > result.txt

If you need only some of output to file, then you need tell in your awk-block. Ex.
Code:
nawk -v outfile="somefile.txt" -f csv2pipe.awk raw.txt > result.txt

Now you can use stdout to print result.txt and some output to file somefile.txt using in awk:
Code:
print "...."  >> outfile
printf "....." >> outfile

# 3  
Old 07-20-2009
hey Kshiji,
Thanks for the reply.

Please note that I have to do this inside the script.
I cannot use to store in an output file like
nawk -f csv2pipe.awk raw.txt > result.txt

I want to use something like
nawk -f csv2pipe.awk raw.txt out.txt

And get the result in out.txt ie $1 parameter to the script.

Can you suggest a way to do this?
# 4  
Old 07-20-2009
I can't see the light ?
What difference is using set output in command line or in awk ?

You also didn't try:
Code:
awk -v outfile="somefile.txt" -f csv2pipe.awk raw.txt
and then in awk redirect all your print/printf using variable outfile, which you have set in commandline.
print .... >> outfile

Variable setting is more readable as using command line arguments in awk.

Doc, including awk, look "Command-line arguments" in Awk-section.
# 5  
Old 07-21-2009
Thanks, it worked out for me at the first time. Sorry for not updating the blog.

---------- Post updated 07-21-09 at 12:47 PM ---------- Previous update was 07-20-09 at 03:37 PM ----------

Hi,
By using print > out in my script and running it as

/bin/pgawk -v out="raw.txt" -f csv2pipe.awk "raw.txt"

I don't have nawk installed, so i decided to use pgawk.
I want to convert csv file (raw.txt) into pipe deliminated file with same name.

This command works fine for files with small size, ie it converts the entire data from csv to pipe.
But when the data is huge, like 4000 lines, it converts only 300 lines and save them. I am not able to get the entire data into the new file.

i tried print >> out, but it produces some other huge output.

Also, every time I run the above command, it makes another file name "awkprof.out". Is there a way to not to create this file and get the complete output of csv file into pipe deliminated file in case of huge files.

Please help me out.
Thanks in advance.
# 6  
Old 07-21-2009
You can't read and write same file in same time. With small file it maybe works. Output to the tmpfile. If there is problem in csv2pipe.awk, you need to show it.
# 7  
Old 07-21-2009
Got it. it works fine with a tmp file. Thanks a lot for ur time.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Need list of input and output parameter of task in a text file, using shell script

//file begin ===== //some code task abcd_; input x; input y,z; //some comment output w; //some comment reg p; integer q; begin //some code end endtask : abcd_ //some code //file end ===== expected output from above... (1 Reply)
Discussion started by: rishifrnds
1 Replies

2. Shell Programming and Scripting

How pass the input parameter to a file in the script ?

OS version: RHEL 6.7 myTextFile.txt file is referred within Script1.sh script, I only execute Script1.sh and I want the input variable to be passed inside myTextFile.txt . Any idea how I can do this ? $ cat script1.sh cat myTextFile.txt $ cat myTextFile.txt $1 Requirement1.... (4 Replies)
Discussion started by: kraljic
4 Replies

3. Shell Programming and Scripting

How to pass the parameter in xml file in UNIX shell script?

Hi, I have an XML file like the following... <?xml version="1.0" encoding="UTF-8" standalone="yes"?> <ONDEMAND_JOB VERSION="5.1" LOCALE="en_US"> <IMPORT_JOBSET TC_CONNECTION_NAME="default" ENVIRONMENT="PRD" USERNAME="Administrator" PASSWORD="AdminPassword" CALENDAR="Main Monthly Calendar"... (3 Replies)
Discussion started by: Debalina Roy
3 Replies

4. Shell Programming and Scripting

How to pass the parameter in xml file in UNIX shell script?

Hi, I have an XML file like the following... <?xml version="1.0" encoding="UTF-8" standalone="yes"?> <ONDEMAND_JOB VERSION="5.1" LOCALE="en_US"> <IMPORT_JOBSET TC_CONNECTION_NAME="default" ENVIRONMENT="PRD" USERNAME="Administrator" PASSWORD="AdminPassword" CALENDAR="Main Monthly Calendar"... (2 Replies)
Discussion started by: Debalina Roy
2 Replies

5. Post Here to Contact Site Administrators and Moderators

Unable to pass shell script parameter value to awk command in side the same script

Variable I have in my shell script diff=$1$2.diff id=$2 new=new_$diff echo "My id is $1" echo "I want to sync for user account $id" ##awk command I am using is as below cat $diff | awk -F'~' ''$2 == "$id"' {print $0}' > $new I could see value of $id is not passing to the awk... (0 Replies)
Discussion started by: Ashunayak
0 Replies

6. Shell Programming and Scripting

How to pass the password as input parameter to scp

Dear all Does anybody know how to pass the password as input parameter to scp or rsync in unix scripts? I have tried echo <password> | scp filename username@<ip address>:/filepath/ . But it does not work. BTW, I dont want to setup ssh trust between servers in this adhoc task. Regards,... (2 Replies)
Discussion started by: eldonlck
2 Replies

7. Shell Programming and Scripting

AWK Script to convert input file(s) to output file

Hi All, I am hoping someone can help me with some scripting I need to complete using AWK. I'm trying to process multiple fixed files to generate one concatenated fixed file in a standard format. The Input file is:- aaaa bbbbb ccccc 1 xxxx aaa bbb aaaa bbbbb ccccc 2 abcd aaa CCC... (9 Replies)
Discussion started by: jason_v_brown
9 Replies

8. Shell Programming and Scripting

awk pass $LOGDIR parameter

hi guys i need your help , i wrote one script which is as below #!/bin/ksh ########################################################### LOGDIR=/export/home/xyz/logs EMAILFile=$LOGDIR/xxs_email.log BOX=$(uname -a | awk '{print $2}') awk '{if ($4 >= 30) {print $1 " " $3 " HAS LAG of "... (1 Reply)
Discussion started by: tapia
1 Replies

9. Shell Programming and Scripting

read a file as input and pass each line to another script

Hi, I am trying to write a ftp script which will read a file for filenames and ftp those files to another server. Here's my ftp script, but it's scanning the current directory for file names. My question is how can I pass multiple files (these files will have the name of data files that need to... (0 Replies)
Discussion started by: sajjad02
0 Replies

10. UNIX for Dummies Questions & Answers

PASS parameter to AWK

Hi, Can i pass a parameter(not a file name) as a parameter to a awk program? eg; $awk -f test 1 2 3 here test is the filename...and 1,2,3 are the i/p parameters? thank you:-) (2 Replies)
Discussion started by: unisam
2 Replies
Login or Register to Ask a Question