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

3 Answers 3

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
4
  • Pardon my newness, but why are we using 'p' and can you explain what second sed statement is doing? Jun 5, 2016 at 18:33
  • p means "print". -n means "don't print unless told to". -f- means "read the script from the standard input".
    – choroba
    Jun 5, 2016 at 18:35
  • How does it know to print? Does it look by default if the first field is same in both files? Jun 5, 2016 at 18:47
  • @user3299633: No, a number before p stands for the line number.
    – choroba
    Jun 5, 2016 at 18:56
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.

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!

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .