awk variable assignment in a file


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting awk variable assignment in a file
# 1  
Old 01-11-2006
Question awk variable assignment in a file

Hi All,

I have a little awk script which uses a variable (x):

Code:
awk -v x=0 'NF != 6 { ++x } END { print "This batch had " x " errors out of ", NR" records"}'

But when I've tried to put the command in a file I can't seem to declare the variable. I've managed to simplify the code so that I don't use the variable but this changes the output. I'm doing this because I'm converting it to perl with a2p. I've just converted the output perl ( my $X; $X = 0; ) so that my output is correct but I've really like to know how to declare awk variables in files. Can any one help?

My simplified code looks like this:

Code:
NF != 6 { x++ }
END { print "This batch had " x " errors out of ", NR" records"}

Many thanks,
Smilie

Last edited by rbatte1; 06-08-2018 at 08:54 AM..
# 2  
Old 01-11-2006
I don't really understand your question but maybe this is what you are looking for:
Code:
eval $(awk -v x=0 'NF != 6 {++x} END {print "ERROR_CNT="x";RECORD_CNT="NR}' yourdata)
echo "$ERROR_CNT of $RECORD_CNT had errors"

This turns your print statement into variable assignments that can be used in the shell.
Code:
2 of 4 had errors

# 3  
Old 01-11-2006
Hi tmarikle,

Thanks for your response. I don't think I described the problem very clearly; let me try again: I can run my command as it is on the command line and it works fine but if I want to run it from a file:

Code:
awk -f awk3 mydata

Where awk3 is my command file - how do I declare the variable within the file (awk3)?

I can do this:

Code:
awk -f -v x=0 awk3 mydata

But to get an exact perl output from a2p I need to define the variable 'x' in awk3 - this is where I have a problem... This is a stumbling block for me when it comes to writing multi-line awk scripts anyway so I'd like to overcome it.

Hope this is a little clearer.

Many thanks,

Smilie

Last edited by rbatte1; 06-08-2018 at 08:55 AM..
# 4  
Old 01-11-2006
Quote:
Originally Posted by pondlife
Where awk3 is my command file - how do I declare the variable within the file (awk3)?
Maybe I'm still unclear but here's another attempt. Awk variables are defined upon first use, however, you can explicitly define variables throughout the script but awk provides an initialization procedure called BEGIN.
Code:
awk '
    BEGIN { x=0 }
    NF != 6 { ++x}
    END { print "This batch had " x " errors out of ", NR" records" }' yourdata

Is that more what you're looking for?
# 5  
Old 01-12-2006
Computer That's it!

Hi tmarikle,

That's what I needed! Thanks!

If I place what's between the quotes in a file I can then run the command:

Code:
awk -f awk5 mydata

Which means that
  1. I now know how to declare variables in a file.
  2. I can now use it to create my perl using: a2p awk5

Cheers,

Smilie

Last edited by rbatte1; 06-08-2018 at 08:55 AM..
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Optimize multiple awk variable assignment

how can i optimize the following: TOTALRESULT="total1=4 total2=9 total3=89 TMEMORY=1999" TOTAL1=$(echo "${TOTALRESULT}" | egrep "total1=" | awk -F"=" '{print $NF}') TOTAL2=$(echo "${TOTALRESULT}" | egrep "total2=" | awk -F"=" '{print $NF}') TOTAL3=$(echo... (4 Replies)
Discussion started by: SkySmart
4 Replies

2. Shell Programming and Scripting

Variable Assignment

Hi I am facing a problem. export local_folder=/opt/app/ cd /opt/app/abc/ abcversion="abc*" (abcga5 is inside /opt/app/abc/) echo $abcversion (it echoes the correct version as abcga5 ) Now when I reuse the value of abcversion for a below path: export... (6 Replies)
Discussion started by: ankur328
6 Replies

3. Shell Programming and Scripting

Same Variable Assignment

Hi I have a strange problem: In my shell script I am performing a copy task: . prop.txt cp -r $dir/ $dir/archive $dir is fetched from a property file (prop.txt) which stores its value dir=/opt/data Now the problem is another dir1 comes into picture. I only want to add... (1 Reply)
Discussion started by: ankur328
1 Replies

4. Shell Programming and Scripting

[awk] printing value of a variable assignment from a file

Heyas Me try to print only the value of a (specific) variable assignment from a file. What i get (1): :) tui $ bin/tui-conf-get ~/.tui_rc TUI_THEME dot-blue "" "$TUI_DIR_INSTALL_ROOT/usr" "$TUI_DIR_INSTALL_ROOT/etc/tui" "$TUI_PREFIX/share/doc/tui" "$TUI_PREFIX/share/tui"... (2 Replies)
Discussion started by: sea
2 Replies

5. Shell Programming and Scripting

Variable assignment inside awk

Hi, Was hoping someone could help with the following: while read line; do pntadm -P $line | awk '{if (( $2 == 00 && $1 != 00 ) || ( $2 == 04 )) print $3,$5}'; done < /tmp/subnet_list Anyone know if it is possible to assign $3 and $5 to separate variables within the {} brackets? Thanks... (14 Replies)
Discussion started by: CiCa
14 Replies

6. Shell Programming and Scripting

awk variable assignment-- inside braces or not?

So, in awk, I've always put my variable assignments inside of the curly braces, just like dad, and grandpa, and the 26 generations before them. But today I came upon an awk statement that had them outside the braces-- blasphemy! Seriously, though, is there any practical difference? I was... (3 Replies)
Discussion started by: treesloth
3 Replies

7. Shell Programming and Scripting

AWK Variable assignment issue Ksh script

Hi There, I am writing a ksh script which assigns variable values from file "A" and passes that variables to file "B". While passing the parameters an additional "$" sign is being assigned to awk -v option. Could any one help me with this please. #!/bin/ksh head -1... (3 Replies)
Discussion started by: Jeevanm
3 Replies

8. Shell Programming and Scripting

variable assignment using awk

Guys, Could you please help me out. I need two values in two variables using awk from the o/p of grep. example:- grep sdosanjh <filename> sdosanjh myhostname myfilename NOW WHAT I WANT IS :- sdosanjh should be in variable (say NAME) myhostname should be in variable (say... (8 Replies)
Discussion started by: sdosanjh
8 Replies

9. UNIX for Dummies Questions & Answers

@ in a variable assignment

Hello Everybody, Does anyone know what the @ symbol means in a csh script, if used with a variable assignment as below @ line = 1 why not just use.... set line=1 Many thanks rkap (1 Reply)
Discussion started by: rkap
1 Replies

10. UNIX for Dummies Questions & Answers

Variable assignment for file names.

I am trying to process error files in selected directories. I can count the files that are there and export the contents to a file for either emailing or printing. The next step is to move the files to a processed directory with the name changed to .fixed as the last extension. for file in... (2 Replies)
Discussion started by: jagannatha
2 Replies
Login or Register to Ask a Question

Featured Tech Videos