Code Comments

Programming Forum and web based access to our favorite programming groups.
For Programmers: Free Programming Magazines | New: Database administration forum
Registration is free! Edit your profileCalendarFind other membersFrequently Asked QuestionsSearch -> 
Post New Thread











Thread
Author

compare shell-input and file-input
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



Report this thread to moderator Post Follow-up to this message
Old Post
Stefan Simon
03-20-04 01:23 AM


Re: compare shell-input and file-input
> 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



Report this thread to moderator Post Follow-up to this message
Old Post
Stefan Simon
03-20-04 01:23 AM


Re: compare shell-input and file-input
I solved the problem.

Stefan



Report this thread to moderator Post Follow-up to this message
Old Post
Stefan Simon
03-20-04 01:23 AM


Re: compare shell-input and file-input

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.


Report this thread to moderator Post Follow-up to this message
Old Post
Ed Morton
03-20-04 01:23 AM


Re: compare shell-input and file-input
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 af
ter the statement. The fields in /etc/passwd are : separated that is the rea
son for the
-F:

Irish


Report this thread to moderator Post Follow-up to this message
Old Post
irish
03-20-04 01:23 AM


Sponsored Links




Last Thread Next Thread Next
Search this forum -> 
Post New Thread

AWK archive

Show a Printable Version Send to friend Email This Page to Someone! subscribe to this thread Receive updates to this thread
Computer Consultants
Programming Jobs
Visual Basic Controls
SQL Server Programming
Webservices
Java Security
Visual Studio
C# Programming
Visual J++
Software engineering
Open source Software
Perl Programming
PHP Programming
ASP Programming
ASP .NET Programming
Visual Basic Programming
Windows Scripting Host
Java Programming
Java Help
Java Beans
VBScript
Cobol
MAC Applications
Unix Programming
Forum Jump:
All times are GMT. The time now is 11:35 AM.

 
Free MCSE Braindumps | Real Estate Topics

Programming forum archive

Copyrights CodeComments.com 2004 - 2006

Powered by vBulletin Copyright 2000-2006 Jelsoft Enterprises Limited.