Code Comments
Programming Forum and web based access to our favorite programming groups.Hello,
First, sorry about my bad english :-(
How do compare a shell-variable and a col of a file?
I mean:
echo $variable|awk '{if ($variable == $1) print $0}' input.dat
(Print all lines of input.dat where $variable identically with first col of
input.dat)
tia,
Stefan Simon
Post Follow-up to this message> How do compare a shell-variable and a col of a file?
I have it find out:
awk -F: 'BEGIN{"echo root"|getline test}{if (test==$1)print $0}' /
etc/passwd
but how it works on a shell-script?
#!/bin/bash
variable=`cat old_passwd|cut -d':' -f1`
for user in $variable
do
line=`awk -F: 'BEGIN{"echo $user"|getline test}{if (test==$1)print
$0}'
/etc/passwd`
echo $line
done
Why do it not work?
tia
Stefan Simon
Post Follow-up to this message
Stefan Simon wrote:
>
>
> I have it find out:
>
> awk -F: 'BEGIN{"echo root"|getline test}{if (test==$1)print $0}'
/etc/passwd
>
> but how it works on a shell-script?
>
> #!/bin/bash
> variable=`cat old_passwd|cut -d':' -f1`
> for user in $variable
> do
> line=`awk -F: 'BEGIN{"echo $user"|getline test}{if (test==$1)pr
int $0}'
> /etc/passwd`
The correct way of writing the above line would be:
line=`awk -F: -v user="$user" '$1 == user {print}' /etc/passwd`
In reality, though, there's no need for a shell script or explicit loop
since you can do the whole thing trvially in awk like this:
awk -F: 'NR == FNR { users[$1] = ""; next }
$1 in users { print }' old_passwd /etc/passwd
Regards
Ed.
Post Follow-up to this messageTry this.
awk -F: '{ if ( ID == $1 ) print $0 }' ID=root /etc/passwd
It should work fine. You can pass a variable to awk from the command line af
ter the statement. The fields in /etc/passwd are : separated that is the rea
son for the
-F:
Irish
Post Follow-up to this messagePowered by vBulletin
Copyright 2000-2006 Jelsoft Enterprises Limited.