Suppose I have 2 files
File-1 map.txt
1 tony
2 sean
3 jerry
4 ada
File-2 relation.txt
tony sean
jerry ada
ada sean
Expected-Output result.txt
1 2
3 4
4 2
My code was:
awk 'FNR==NR{map[$1]=$2;next;} {$1=map[$1]; $2=map[$2]; print $0}' map.txt relation.txt > output.txt
But I got the left column only:
1
3
4
It seems that something wrong near $2=map[$2].
Very appreciated if you could help.
You've got the mapping creation the wrong way around, it needs to be:
map[$2] = $1
Your current script maps numbers to names whereas what you seem to be after is a map from names to numbers.
The following transcript shows the corrected script:
pax> cat m.txt
1 tony
2 sean
3 jerry
4 ada
pax> cat r.txt
tony sean
jerry ada
ada sean
pax> awk 'FNR==NR{map[$2]=$1;next;}{$1=map[$1];$2=map[$2];print $0}' m.txt r.txt
1 2
3 4
4 2
Using awk.
awk 'FNR==NR{map[$2]=$1;next;}{print map[$1], map[$2]}' m.txt r.txt
Related
I have sample data
Person Owe
John 1
John 2
John -1
John -10
John 5
John 9
John -4
John 9
John 2
John 3
I was using script file to parse this and the command is
awk -f script data
, script content:
NR == 2 { print "Starting Here"; x = $2; next}
{x+= $2} #This x+=$2
END{ print x }
Output:
Starting Here
16
But when I remove {} from {x+=$2}
Output:
Starting Here
John 2
John -1
John -10
John 5
John 9
John -4
John 9
John 2
John 3
16
Q) Why does it print if I don't have a {} around the variable addition?
Answering own question:
A rule needs an action. If action is missing it defaults to printing the line.
Just mentioning x+=$2 is a rule, And if we give {x+=$2} or x+=$2{} those have/are action.
This question already has an answer here:
awk: two files are queried
(1 answer)
Closed 4 years ago.
I know this is a simple logic with AWK but I cannot figure it out. I've used "join" to do similar tasks or splitting, but I'd like it written in AWK. Can someone provide this?
File1
JASON 11 BLACK
JOHN 19 BROWN
JOSH 13 GOLD
File2
JOHN 5910
JOSH 5711
JACOB 1980
JIMMY 1050
JASON 9614
Output
JASON 9614 11 BLACK
JOHN 5910 19 BROWN
JOSH 5711 13 GOLD
Following awk may help here.
awk 'FNR==NR{a[$1]=$2 OFS $3;next} ($1 in a){print $1,$2,a[$1]}' FILE1 FILE2
I am trying to run a script that I have set up but when I go to sort the contents and display the text the content is printed but the sort command is ignored and the information is just printed. I tried this code format using awk and the sort function is ignored but I am not sure why.
Command I tried:
sort -t, -k4 -k3 | awk -F, '{printf "%-18s %-27s %-15s %s\n", $1, $2, $3, $4 }' c_list.txt
The output I am getting is:
Jim Girv 199 pathway rd Orlando FL
Megan Rios 205 highwind dr Sacremento CA
Tyler Scott 303 cross st Saint James NY
Tim Harding 1150 Washton ave Pasadena CA
The output I need is:
Tim Harding 1150 Washton ave Pasadena CA
Megan Rios 205 highwind dr Sacremento CA
Jim Girv 199 pathway rd Orlando FL
Tyler Scott 303 cross st Saint James NY
It just ignores the sort command but still prints the info I need in the format from the file.
I need it to sort based off the fourth field first the state and the third field next the town then display the information.
An example where each field is separated by a comma.
Field 1 Field 2 Field 3 Field 4
Jim Girv, 199 pathway rd, Orlando, FL
The problem is you're doing sort | awk 'script' file instead of sort file | awk 'script' so sort is sorting nothing and consequently producing no output while awk is operating on your original file and so producing output from that. You should have noticed that your sort command is hanging too for lack of input and you should have mentioned that in your question.
To demonstrate:
$ cat file
c
b
a
$ sort | awk '1' file
c
b
a
$ sort file | awk '1'
a
b
c
Suppose I have these 2 tab delimited files, where the second column in first file contains matching values from first column of the second file, I would like to get an output like this:
FileA:
1 A
2 B
3 C
FileB:
A Apple
C Cinnabon
B Banana
I would like an output like this:
1 Apple
2 Banana
3 Cinnabon
I can write a script for this, but I would like to know how to make it in awk or perl in one line.
awk 'BEGIN{FS=OFS="\t"}NR==FNR{a[$1]=$2;next}{$2=a[$2]}1' f2 f1
GNU sed oneliner
sed -r 's:\s*(\S+)\s+(\S+):/\\s*\\S\\+\\s\\+\1/s/\\(\\s*\\S\\+\\s\\+\\)\1/\\1\2/:' fileB | sed -f - fileA
..output:
1 Apple
2 Banana
3 Cinnabon
The command you want is this:
$ awk 'FNR==NR{a[$1]=$2 FS $3;next}{$2=a[$2]; print}' file2 file1
1 Apple
2 Banana
3 Cinnabon
I have the following situation:
The file1.dat is like:
1 2
1 3
1 4
2 1
and the file2.dat is like:
1 2
2 1
2 3
3 4
I want to find the differences between the second file from the first. I tried wit grep -v -f file1 file2 but my real files are bigger than this two and when I tried with it the shell never ended is work.
The result should be:
2 3
3 4
The files are sorted and they have the same number of elements. Any way to find a solution with awk?
Seems like you want lines in file2 that are not in file1:
$ awk 'FNR==NR{a[$0];next}!($0 in a)' file1 file2
2 3
3 4
However it's simpler to use comm:
$ comm -13 file1 file2
2 3
3 4