I found many similar posts but they don't quite work for me. So I am posting this question.

I have a file test1.txt

1 2 3 4 5
6 7 8 9 0
a b c d e

and another file test2.txt

a b c d e f g
h i j k l m n
o p q r s t u 

I want two take the 6th column from test2.txt and the last column from test1.txt and form a new file test3.txt:

f 5
m 0
t e

How can I do that ?

  • What did you try so far? Better if you post your efforts. – bu5hman May 8 '20 at 18:57
paste test2.txt test1.txt | awk '{print $6, $NF}' >test3.txt
  • Redirect output: >test3.txt
  • paste merges the lines of the two files side by side
  • The result is piped to awk which prints the sixth and last columns

NF (sans dollar sign) is an awk built-in variable which holds the number of fields. As fields are referenced by $expr, the last field can be referenced as $NF.

  • What does $NF signify? – physu May 6 '20 at 8:07
  • @physu See update – rowboat May 6 '20 at 8:38

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.