variable assignment using awk


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting variable assignment using awk
# 1  
Old 07-28-2009
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 HOSTNAME)

3rd coloumn i am not bothered about.


Can you please suggest if we can do this by pipe the output of grep to awk ? Smilie

I tried the following but not able to get the value of val1 using echo.

grep sdosanjh <filename> | awk '{val=$1}{val1=$2}{print val, val1}'

Because my next line will be making a ssh connection with the myhostname
i.e. ssh $val1 [Something like that]

Last edited by sdosanjh; 07-28-2009 at 06:39 PM..
# 2  
Old 07-28-2009
yes you can use awk...
Code:
NAME=`awk '/sdosanjh/{print $1}' filename`
HOSTNAME=`awk '/sdosanjh/{print $2}' filename`

# 3  
Old 07-28-2009
Code:
set `grep sdosanjh filename`
echo $1 $2 $3

You need to provide test for failure otherwise you will get some gibberish.
these are called positional parameters and there are 9 of them. To have more you need to use shift command.
There is no need to use awk here.
Another interesting construct for long file is:
Code:
cat filename | grep sdosanjh |
while read var1 var2 var3
do
echo $var1 $var2 $var3
done

If you have less variables than items, all remainder of the read line gets packed into var3

Last edited by gch; 07-28-2009 at 06:49 PM..
# 4  
Old 07-28-2009
thnaks gch... your solution looks good and working fine...
# 5  
Old 07-28-2009
Quote:
Originally Posted by sdosanjh
thnaks gch... your solution looks good and working fine...
That construct is very powerful, and permits also to rearrange order of strings in a file.
You could do:
Code:
....
do
echo $var3 $var2 $var1 >> newfile
done
....

# 6  
Old 07-29-2009
Gch,

Good solution. A bit modification :

Code:
grep sdosanjh filename |
while read var1 var2 var3
do
echo $var1 $var2 $var3
done

# 7  
Old 07-29-2009
Quote:
Originally Posted by panyam
Gch,

Good solution. A bit modification :

Code:
grep sdosanjh filename |
while read var1 var2 var3
do
echo $var1 $var2 $var3
done

This is how it should be done in this particular case. Mine just illustrates, for less experienced, the fact that you can chain several commands before that while.
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

Help with variable assignment

Hi, In AIX I have a variable with , (coma) separated values assigned to it like shown below var1=apple,boy,chris i want to convert this to var1='apple','boy','chris' the number of values assigned to var1 might change and it could be from 1 to n any suggestions please? (3 Replies)
Discussion started by: rahul9909
3 Replies

9. Shell Programming and Scripting

awk variable assignment in a file

Hi All, I have a little awk script which uses a variable (x): 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... (4 Replies)
Discussion started by: pondlife
4 Replies

10. 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
Login or Register to Ask a Question

Featured Tech Videos