2

I have a Tab separated file as follows:

A B HM 1 BN 2 

I would like to add another column to this file such that this new column becomes the first column of the file as shown below:

New A B 201507 HM 1 201507 BN 2 

How can I do this?

1 Answer 1

4

Use paste:

paste -d"\t" file1 file2 

Where:

  • -d specifies the dlimiter between the two files (\t is a tabulator).
  • file1 contains the lines you want to prepend.
  • file2 contains the other lines.

Edit: Another solution with awk:

awk '{getline l < "file2"; print $0"\t"l} ' file1 

Where:

  • getline reads the next line from file2 into the variable called l, which is then printed after the line of file1 followed by a tab \t.

When file1 would contain:

New 201507 201507 

...and file2 contains:

A B HM 1 BN 2 

...the output would be:

New A B 201507 HM 1 201507 BN 2 
4
  • Thank you for your answer. However, I do not seem to be having the paste command... Not sure why. I am working from a company system. Commented Sep 11, 2015 at 18:14
  • @activelearner What os are you running on that system? Commented Sep 11, 2015 at 18:15
  • Linux hostname 2.6.32-358.41.1.el6.x86_64 #1 SMP Mon Apr 21 15:58:42 EDT 2014 x86_64 x86_64 x86_64 GNU/Linux Commented Sep 11, 2015 at 18:16
  • @activelearner See my edit, I added an awk solution. Commented Sep 11, 2015 at 18:21

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.