| Author |
compare shell-input and file-input
|
|
| Stefan Simon 2004-03-19, 8:23 pm |
| 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
| |
| Stefan Simon 2004-03-19, 8:23 pm |
| > 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
| |
| Stefan Simon 2004-03-19, 8:23 pm |
| I solved the problem.
Stefan
| |
| Ed Morton 2004-03-19, 8:23 pm |
|
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)print $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.
| |
|
| Try 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 after the statement. The fields in /etc/passwd are : separated that is the reason for the
-F:
Irish
|
|
|
|