Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Korn Shell / AWK Help

Status
Not open for further replies.

SamDurai

Programmer
Aug 30, 2009
10
US
I have a file input.txt with the following sample data. I am trying to pattern match and assign the value to a variable. The problem is that I the assigned values are not retained outside of the nawk loop. Is there away for me to retain the variable values within the nawk loop to process later in the script (outside the nawk loop) ?

Thanks in advance for your help.

/tmp > cat input.txt
VAR1 Hello
VAR2 World
/tmp >
/tmp > cat match.ksh
cat input.txt | nawk '{
if (index($0, "VAR1") > 0) my_var1= $2
if (index($0, "VAR2") > 0) my_var2= $2
} END {
printf ("%5s %5s\n",
my_var1,
my_var2) ;
}' >> exec_out.log

v_var1=$my_var1
v_var2=$my_var2
echo v_var1=$v_var1 v_var2=$v_var2
/tmp >
/tmp >
/tmp > match.ksh
v_var1= v_var2= [ Ideally I want v_var1=Hello v_var2=World in the output if values are retained ]
/tmp >
/tmp >
/tmp > cat exec_out.log
Hello World
/tmp >
/tmp >
 
It isn't a "nawk loop" so much as a "nawk process" in the pipeline. The variables defined in nawk will only "live" for as long as the process does, and by the time you try to assign or echo them the nawk process has already terminated.

Incidentally, you don't need to cat files into nawk; it can read its own files by placing them after the script.

Typically I would do something like this to assign those values to variables:

Code:
#!/usr/bin/ksh

v_var1=$(nawk '$1 == "VAR1" { print $2 }' input.txt)
v_var2=$(nawk '$1 == "VAR2" { print $2 }' input.txt)
echo v_var1=$v_var1 v_var2=$v_var2

Annihilannic.
 
Thanks for the quick response and timely help.
 
Annihilannic,

How can you copy the and pest the code snippets like this from shell terminal? would you please tell me how to do it?

Sorry it is off the topic,

Thanks
 
Which part are you confused about? The copying and pasting? Or putting it in a code box:

Code:
like this?

Copying and pasting depends on what OS and terminal software you are using, so let us know and we can help. Typically I use xterm-like terminal software, which means you just select text to copy (no Ctrl-C or Edit | Copy required), and either Ctrl-V (in Windows) or middle-click (Linux) to paste.

To place code in a code box just surround it in [ignore]
Code:
 ...
[/ignore] tags.

Annihilannic.
 
test....
Code:
cat *.txt

My question was the later: putting the code in code box...

Thanks for providing the the tag...

Supper...

Demis
 
You can see all of the tags available by clicking the Process TGML link beside the checkbox at the bottom of the the message entry form.

Annihilannic.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top