The UNIX and Linux Forums  
Hello and Welcome from to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.

Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
.
google unix.com



Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
How to pass parameter from sqlplus(procedure completed) to your shell script sanora600 Shell Programming and Scripting 1 08-08-2008 06:55 AM
Pass parameter into script alfredo Shell Programming and Scripting 2 04-08-2008 10:40 PM
how can i pass parameter with spaces to csh script umen Shell Programming and Scripting 1 03-19-2008 12:33 PM
read a file as input and pass each line to another script sajjad02 Shell Programming and Scripting 0 09-25-2004 12:13 AM
Pass Parameter to Another Script rvprod UNIX for Dummies Questions & Answers 4 04-05-2002 01:07 PM

Reply
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Bulgarian Greek Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 07-20-2009
bhaskarjha178 bhaskarjha178 is offline
Registered User
  
 

Join Date: Jul 2009
Posts: 4
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 (permalink)  
Old 07-20-2009
kshji's Avatar
kshji kshji is offline
Registered User
  
 

Join Date: Jun 2009
Location: Finland
Posts: 236
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 (permalink)  
Old 07-20-2009
bhaskarjha178 bhaskarjha178 is offline
Registered User
  
 

Join Date: Jul 2009
Posts: 4
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 (permalink)  
Old 07-20-2009
kshji's Avatar
kshji kshji is offline
Registered User
  
 

Join Date: Jun 2009
Location: Finland
Posts: 236
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 (permalink)  
Old 07-21-2009
bhaskarjha178 bhaskarjha178 is offline
Registered User
  
 

Join Date: Jul 2009
Posts: 4
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 (permalink)  
Old 07-21-2009
kshji's Avatar
kshji kshji is offline
Registered User
  
 

Join Date: Jun 2009
Location: Finland
Posts: 236
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 (permalink)  
Old 07-21-2009
bhaskarjha178 bhaskarjha178 is offline
Registered User
  
 

Join Date: Jul 2009
Posts: 4
Got it. it works fine with a tmp file. Thanks a lot for ur time.
Reply

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -4. The time now is 12:29 AM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0