The UNIX and Linux Forums  
Hello and Welcome from United States 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
change the filename by adding up 1 each time, tricky one netbanker Shell Programming and Scripting 5 03-07-2008 04:45 PM
Change multiple filename formats with WHILE ScKaSx UNIX for Dummies Questions & Answers 5 02-27-2008 07:11 PM
shortcut for tar cvf - [filename] | gzip > [filename].tar.gz bcamp1973 UNIX for Dummies Questions & Answers 4 12-11-2007 05:45 PM
script to change filename with numbers silversurfer202 Shell Programming and Scripting 5 11-10-2006 10:13 AM
Change new filename with date ?? sabercats Shell Programming and Scripting 9 02-13-2006 05:12 PM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 06-21-2008
timj123 timj123 is offline
Registered User
  
 

Join Date: Jan 2008
Posts: 80
gzcat into awk and then change FILENAME and process new FILENAME

I am trying to write a script that prompts users for date and time, then process the gzip file into awk. During the ksh part of the script another file is created and needs to be processed with a different set of pattern matches then I need to combine the two in the end. I'm stuck at the part where I have to change the FILENAME and FS part. Can anyone shed some light on this for me please.

Thanks

Code:
#!/bin/ksh
tput clear

dir=/my/dir/here/
ifile=/tmp/ifile
ofile=/tmp/ofile

echo "Enter the date and hour to run this script on [YYMMDDHH]: \c"
read file


/usr/bin/gzcat $file | /usr/xpg4/bin/awk '

BEGIN {
print "headers go here"
}


NR == 3 { print $2,$3"  --  "$4,$5 ; print "" ; next }

/^dise / {
        dipce = substr($2,4,1) * 1000 ; next
           }
/^LC/ {
        lec = substr($0,3,3) + dipce
        f++
        s = ""
        for ( i in arr ) { delete arr[i] }
        }
	   f {
		run some more stuff until gzcat is finished
              }

###Here I want to change FILENAME to "'"$ifile"'" ###

#Run some more awk code here while reading in "'"$ifile"'"




END {
Print some stuff from both processed file
}
  #2 (permalink)  
Old 06-22-2008
radoulov's Avatar
radoulov radoulov is offline Forum Staff  
addict
  
 

Join Date: Jan 2007
Location: Варна, България / Milano, Italia
Posts: 2,847
You can use getline to start reading the $ifile whenever you want, but I would prefer something like this: define $ifile as a second argument and then process it as usual: NR == FNR -> action -> next for the first one, action for the second.
FS is not a problem, you can change it anytime you want.
  #3 (permalink)  
Old 06-22-2008
robotronic's Avatar
robotronic robotronic is offline Forum Advisor  
Can I play with madness?
  
 

Join Date: Apr 2002
Location: Italy
Posts: 370
To read lines from a different file other than the one you piped through awk, you need "getline" function.

Code:
getline <"'"$ifile"'"

or

getline var <"'"$ifile"'"
In the first case, $0 will be overwritten with the first line read from $ifile, in the second case you retain $0 of the original file (=the output of gzcat) and set a new variable "var" holding the first line of $ifile.

If you want, you can also avoid that pretty ugly syntax for the file name ("'"$ifile"'") by using "-v" option of awk:

Code:
/usr/bin/gzcat $file | /usr/xpg4/bin/awk -v "ifile=$ifile" ' ... '
and then reference your file with the internal awk variable "ifile", like this:

Code:
getline <ifile
Don't miss awk man page for details on getline.
  #4 (permalink)  
Old 06-22-2008
timj123 timj123 is offline
Registered User
  
 

Join Date: Jan 2008
Posts: 80
Thanks guys, I have learned so much from the both of you. I am still having issues that I can't figure out, here is the script:
Code:
/usr/bin/gzcat $file | /usr/xpg4/bin/awk '

BEGIN {
print "headers go here"
}


NR == 3 { print $2,$3"  --  "$4,$5 ; print "" ; next }

/^dise / {
        dipce = substr($2,4,1) * 1000 ; next
           }
/^LC/ {
        lec = substr($0,3,3) + dipce
        f++
        s = ""
        for ( i in arr ) { delete arr[i] }
        }
	   f {
              if ( $1 == "RRAC" ) {
                s++
                 }
                if ( s ) {
                    if ( $1 == "tsec" ) {
                       lectsec = $2 * 1
                       }
                    if ( $1 == "VAR" ) {
                        car[$2 * 1]+=$8
                        lecCar = $2 * 1
                        c[lec,lectsec,BCar] = 0
                        }
                     c[lec,lectsec,BCar]++
                     if ( c[lec,lectsec,BCar] == 8 ) {
                        car[BCar]+=$8
                        tst[lec"-"BCar]=sprintf("%5.2f",car[BCar] / 12)
                        }
                    }
                if ( $1 == "RRAC-L" ) {
                        f = "" ; next
                   }
                   }


###Here I want to change FILENAME to "'"$ofile"'" ###
#####################################################

{
while (( getline < "'"$ofile"'" ) > 0 ) {
FS = "\;" ; print $1,$3,$4 ; next }
}
#####################################################
END {
for ( i in tst ) { print i,tst[i] }

    } '

/usr/bin/rm $ifile
/usr/bin/rm $ofile
When I change FS it also changes the output of the following:
Code:
for ( i in tst ) { print i,tst[i] }
I can't figure this out??? Why does this change the output of the array?
  #5 (permalink)  
Old 06-23-2008
robotronic's Avatar
robotronic robotronic is offline Forum Advisor  
Can I play with madness?
  
 

Join Date: Apr 2002
Location: Italy
Posts: 370
You need to set the OFS variable with the desired Output Field Separator, by default a space.
Or try using "nawk". I've noticed this behavior on Solaris:

Code:
> echo "aaa:bbb:ccc" | /usr/xpg4/bin/awk '{ FS=":"; print $2,$3 }'
 
> echo "aaa:bbb:ccc" | /bin/awk '{ FS=":"; print $2,$3 }'
bbb ccc

> echo "aaa:bbb:ccc" | /bin/nawk '{ FS=":"; print $2,$3 }'
bbb ccc
The first command returns null!
  #6 (permalink)  
Old 06-23-2008
ghostdog74 ghostdog74 is offline Forum Advisor  
Registered User
  
 

Join Date: Sep 2006
Posts: 2,509
@OP, use split inside you while loop
Code:
...
while (( getline line< "'"$ofile"'" ) > 0 ) {
 n=split(line,temp,"\")
 print temp[1],temp[3] ......
 next }
}
...
  #7 (permalink)  
Old 06-23-2008
timj123 timj123 is offline
Registered User
  
 

Join Date: Jan 2008
Posts: 80
Thanks guys, I will try them both.
Closed Thread

Bookmarks

Tags
solaris

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 01:28 PM.


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