acessing awk array element while getline < "file"


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting acessing awk array element while getline < "file"
# 1  
Old 06-26-2008
acessing awk array element while getline < "file"

I am attempting to write a awk script that reads in a file after awk array elements are assigned and using those elements while reading in the new file. Does this make sense?

Code:
/pattern/ {tst[$2]=$3}
(( getline < "file" ) > 0 ) {
        x[$2]=x[$2] " "tst[$2]
}

When I print tst[$2] in the END statement it is assigned, but when I try to print tst[$2] in the while statement with getline is shows unasigned. How do I reference tst[$2] while reading in file with getline???

Thanks in advance.
# 2  
Old 06-26-2008
I suppose FS has been modified,
try to force the split (assuming default OFS value, otherwise use whatever you need):

Code:
/pattern/ { tst[$2] = $3 }
(( getline < "file" ) > 0 ) {
        split($0, t, OFS)
        x[t[2]] = x[t[2]] FS tst[t[2]]
}

# 3  
Old 06-26-2008
Thanks for the reply radoulov, still can't get this figured out. I think if I could reference it right, it will work. How come the following code prints out null, but if I use the same print statement in the END statement it will be successful

Code:
/pattern/ {tst[$2]=$3}
(( getline < "file" ) > 0 ) {
        print tst[23]   #will print null
}
END {
printf "%s--%s\n",tst[23],"END"  # is successful

# 4  
Old 06-26-2008
Question Requirements unclear

Where is all the input coming from? Are you using only a file named "file" for all your input needs and can you post the entire awk script along with some input data?
# 5  
Old 06-26-2008
In first part of awk script tst array is set for use in the getfile section of script. Problem I have got is, it looks like I can't use tst array while reading in and setting x array.


Code:
#!/bin/ksh
tput clear

dir=/my/dir/here/ #all files are gzip'd
ifile=/tmp/ifile
ofile=/tmp/ofile
$file=$dir$file.gz
echo "Enter the date and hour to run this script on [YYMMDDHH]: \c"
read file


#tool is run to generate $ofile here, file is semicolon delimited


/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
                   }
                   }


###This section not working because I can't access###
###array tst, to "test" I tried printing tst array, but can't get it to work.

{
while (( getline < "'"$ofile"'" ) > 0 ) {
FS = "\;" 
	x[$2"-"$4]+=tst[$5"-1"] ;
        x[$5"-"$7]+=tst[$5"-2"] ;
        x[$8"-"$10]+=tst[$5"-3"];
        x[$11"-"$13]+=tst[$5"-4] ;
}
}
#####################################################
END {
for ( i in x ) { print i,x[i] }

    } '

/usr/bin/rm $ifile
/usr/bin/rm $ofile

# 6  
Old 06-27-2008
This action is executed for every record (while, I suppose, your array in not yet build):

Code:
{
  while (( getline < "'"$ofile"'" ) > 0 ) {
  FS = "\;" 
    x[$2"-"$4]+=tst[$5"-1"] ;
        x[$5"-"$7]+=tst[$5"-2"] ;
        x[$8"-"$10]+=tst[$5"-3"];
        x[$11"-"$13]+=tst[$5"-4] ;
  }
}

Try moving it into the END block.

You can read both files in parallel if you need, the array infile is accessible consider this:
Code:
zsh-4.3.4% head *file
==> getlinefile <==
1 getlinefile
2 getlinefile

==> infile <==
1 infile
2 infile
zsh-4.3.4% awk '{ infile[$1] = $0 }
quote> (getline < "getlinefile") {
quote>   print infile[$1], $0
quote>   }' infile
1 infile 1 getlinefile
2 infile 2 getlinefile

This is not a good habit:

Code:
"'"$ofile"'"

Use this instead:

Code:
awk -v ofile="$ofile" ...

# 7  
Old 06-27-2008
Thanks, I did not think of that. Getting closer but still not there. I moved to end statement like you sugested, but can you explain the following code:

Code:
(( getline < "file" ) > 0 ) {
        printf "%s--%s\n",tst["23"],"WHILE"     #this prints the element of tst[23]
	printf "%s\n",tst["\42"20+3"\42"]	#this print null???  why???

}

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Bash script - Print an ascii file using specific font "Latin Modern Mono 12" "regular" "9"

Hello. System : opensuse leap 42.3 I have a bash script that build a text file. I would like the last command doing : print_cmd -o page-left=43 -o page-right=22 -o page-top=28 -o page-bottom=43 -o font=LatinModernMono12:regular:9 some_file.txt where : print_cmd ::= some printing... (1 Reply)
Discussion started by: jcdole
1 Replies

2. UNIX for Dummies Questions & Answers

Using "mailx" command to read "to" and "cc" email addreses from input file

How to use "mailx" command to do e-mail reading the input file containing email address, where column 1 has name and column 2 containing “To” e-mail address and column 3 contains “cc” e-mail address to include with same email. Sample input file, email.txt Below is an sample code where... (2 Replies)
Discussion started by: asjaiswal
2 Replies

3. Shell Programming and Scripting

how to use "cut" or "awk" or "sed" to remove a string

logs: "/home/abc/public_html/index.php" "/home/abc/public_html/index.php" "/home/xyz/public_html/index.php" "/home/xyz/public_html/index.php" "/home/xyz/public_html/index.php" how to use "cut" or "awk" or "sed" to get the following result: abc abc xyz xyz xyz (8 Replies)
Discussion started by: timmywong
8 Replies

4. Shell Programming and Scripting

awk command to replace ";" with "|" and ""|" at diferent places in line of file

Hi, I have line in input file as below: 3G_CENTRAL;INDONESIA_(M)_TELKOMSEL;SPECIAL_WORLD_GRP_7_FA_2_TELKOMSEL My expected output for line in the file must be : "1-Radon1-cMOC_deg"|"LDIndex"|"3G_CENTRAL|INDONESIA_(M)_TELKOMSEL"|LAST|"SPECIAL_WORLD_GRP_7_FA_2_TELKOMSEL" Can someone... (7 Replies)
Discussion started by: shis100
7 Replies

5. Shell Programming and Scripting

Need to parse file "x" lines at a time ... awk array?

I have files that store multiple data points for the same device "vertically" and include multiple devices. It repeats a consistant pattern of lines where for each line: Column 1 is a common number for the entire file and all devices in that file Column 2 is a unique device number Column 3 is... (7 Replies)
Discussion started by: STN
7 Replies

6. Shell Programming and Scripting

cat $como_file | awk /^~/'{print $1","$2","$3","$4}' | sed -e 's/~//g'

hi All, cat file_name | awk /^~/'{print $1","$2","$3","$4}' | sed -e 's/~//g' Can this be done by using sed or awk alone (4 Replies)
Discussion started by: harshakusam
4 Replies

7. Shell Programming and Scripting

awk's getline < "-" seems not work for pipe

Hi all, I have an gawk script to get user's input, So I use getline name < "-" (or getline name < "/dev/stdin") in my script They both work fine when my script deals with files. But it is broken for pipes. When I try "some command | my awk script", the variable name just gets an empty... (17 Replies)
Discussion started by: qiulang
17 Replies

8. Shell Programming and Scripting

Error to "find" a matching array element in a directory

Hi, I have defined an array which holds a couple of elements which are nothing but files names. I want to find the files in a directory for the matching file name(array elements) with less than 1 day old. When I am trying to execute the code (as below), it gives an error. Your help in this... (1 Reply)
Discussion started by: mkbaral
1 Replies

9. UNIX for Dummies Questions & Answers

Explain the line "mn_code=`env|grep "..mn"|awk -F"=" '{print $2}'`"

Hi Friends, Can any of you explain me about the below line of code? mn_code=`env|grep "..mn"|awk -F"=" '{print $2}'` Im not able to understand, what exactly it is doing :confused: Any help would be useful for me. Lokesha (4 Replies)
Discussion started by: Lokesha
4 Replies

10. Shell Programming and Scripting

awk: getline NOM < "-" script does not stop

Well , i have: $ cat example.dat 1 2 3 4 5 $ cat getline3.awk function getName (NOM) { printf "Enter a filename: " getline NOM < "-" # get response return NOM } BEGIN { printf ("\nREP: %s\n",getName(N)) } {print} This example works fine: (2 Replies)
Discussion started by: Klashxx
2 Replies
Login or Register to Ask a Question