u20sr
1
I want to read from file 1 and pattern match in file two and print field two from the next line.
File 1:
user1
user2
user3
File 2:
name=user1
gud=12345
name=user2
gud=32456
I have this pattern hardcoded but can't work out how to pass file 1 to the pattern match:
awk '/user1/ {getline; print $2}' file2
Output:
12345
Subbeh
2
this should work:
awk -F= 'NR==FNR{n=$2;getline;a[n]=$2;next}{print a[$1]}' file2 file1
u20sr
3
Thanks for that.
All I seem to get is blank lines though?
Subbeh
4
Which OS are you running? On Solaris you might have to use nawk instead of awk
u20sr
5
Spot on
SunOS vq97eda 5.10 Generic_147440-19 sun4v sparc sun4v
.
Almost there.
> cat file1
user1
user2
> cat file2
name=user1
gud=123456
name=user2
gud=987654
name=user4
gud=xxxxxxx
> nawk -F= 'NR==FNR{n=$2;a[n]=$2;next}{print a[$1]}' file2 file1
user1
user2
However, what I want is
123456
987654
Subbeh
6
you forgot to add 'getline' in the script
u20sr
7
What an idiot!
Appreciate patience - works perfectly.
---------- Post updated at 03:14 PM ---------- Previous update was at 02:57 PM ----------
Can I make it a bit more complex. Same logic but with an extra file and and an output depending if they match.
For example: Read file1 get gud from file2 and file3. Compare these. If the same then output to match.out else nonmatch.out.
file3
name=user1
gud=123456
name=user2
gud=
name=user4
gud=111111