Display file contents if it has corresponding line number

1

I have two files in the following format:

File A
4
5
8
9

File B
1 text
2 text1
3 text2
4 text3
5 text4
6 text5
7 text6
8 text7
9 text8

What I want to have for my output is just the following (the second field in File B if it corresponds to the line number in File A):

test3
test4
text7
text8

user3299633

Posted 2016-06-05T18:10:57.207

Reputation: 141

Answers

1

You can create a simple sed script from the file A by appending p to each line, then run the script in another sed with -n not to print the other lines. Moreover, there's no need to save the generated script, you can pipe it between the seds:

sed 's/$/p/' A | sed -nf- B

choroba

Posted 2016-06-05T18:10:57.207

Reputation: 14 741

Pardon my newness, but why are we using 'p' and can you explain what second sed statement is doing? – user3299633 – 2016-06-05T18:33:20.257

p means "print". -n means "don't print unless told to". -f- means "read the script from the standard input". – choroba – 2016-06-05T18:35:46.403

How does it know to print? Does it look by default if the first field is same in both files? – user3299633 – 2016-06-05T18:47:24.323

@user3299633: No, a number before p stands for the line number. – choroba – 2016-06-05T18:56:04.117

1

$ awk 'FNR==NR{seen[$1];next;} FNR in seen' FileA FileB
text3
text4
text7
text8

How it works

  • FNR==NR{seen[$1];next;}

    While reading the first file, this adds each number as a keey to the associative array seen.

    FNR is the line number of the current file and NR is the line number among all lines read. So when FNR==NR, then we are still reading the first file, FileA in this case. seen[$1] creates a key in seen with value $1. next tells awk to skip the rest of the commands and start over on the next line.

  • FNR in seen

    While reading the second file, this prints any line if its line number is in seen.

    FNR in seen is a condition. It evaluates to true if the line number FNR is a key in array seen. Since we have specified no action for this condition, the default action is taken which is to print the line.

John1024

Posted 2016-06-05T18:10:57.207

Reputation: 13 893

1

You're doing a relational "join" operation, so use the standard UNIX join command:

$ join fileA.txt fileB.txt
4 text3
5 text4
8 text7
9 text8

To only get the second field from the second file, add -o 2.2:

$ join -o 2.2 fileA.txt fileB.txt
text3
text4
text7
text8

Both files needs to be sorted on the join field (the first column in this example) for this to work.

Cheers!

Kusalananda

Posted 2016-06-05T18:10:57.207

Reputation: 1 941