-3

I have file1 whose content is like this :

file1

The 6 columns are obtained from 6 different hosts before presenting them using "-exec cp" command. This is just FYI.

Now, I have a list of 6 hostnames (file2) which I want to add as 1st row to file1.

Content of file2 is given below.

HOST1 
HOST2 
HOST3 
HOST4 
HOST5 
HOST6 

I need the final output like this.

final output

I am able to add a column but not a row.

  • And while you are editing your post please include a question. "help me!" is not a question. BTW I would be interested in how you add a column, as that is more difficult than adding a row. – Anthon May 17 '16 at 11:15
  • @don_crissti - my hosts list are actually in a column. I want them to be added as the 1st row in the output file (as i've shown for final output) – Karthik May 17 '16 at 12:33
  • @Anthon - Thank you for the feedback. I already found how to add a column. I use the paste option. paste -d"\t" file1 file2 – Karthik May 17 '16 at 12:33
  • 1
    @Karthik first, please edit your question, remove the images and replace them with text. You can use the formatting tools to make it look like code. Make sure you show both input files and your desired output. That way, we can copy the text you give us and test our answers to make sure they are what you need. Your question shows a row, how were we supposed to know your file actually has a column? – terdon May 17 '16 at 12:42
0

Here's one way:

awk -vhead="$(tr '\n' ' ' <file2)" 'BEGIN{print head}{print}' file1 > newfile

The tr command replaces newlines with spaces, converting the "column" in file2 into a "row". This is passed to awk as the head variable which is printed before anything else. Then, each line of the input file is simply printed.

Alternatively, you can do the whole thing in awk:

awk 'NR==FNR{printf "%s ",$0; next}FNR==1{print ""}1;' file2 file1 > newfile

NR is the current input line number and FNR the line number of the current file. The two will be equal only while the 1st file is being read. The printf "%s ",$0; next will print the current line without a \n at the end and skip to the next one. The FNR==1{print ""} is just to add a \n after the header has been printed and the 1; is awk shorthand for "print this line".

  • With GNU sed, you could do sed -i '1r file1' file2, I think? – steeldriver May 17 '16 at 12:28
  • @steeldriver that seems to do something different. It appends the first line of file2, then repeats the 1st of file1 twice, and at the end adds the rest of file2. – terdon May 17 '16 at 12:33
  • @terdon - cat, cp & sed doesn't seem to do the job. I want something like this -->> link. Paste seems to do the job for adding columns. i want for row now. – Karthik May 17 '16 at 12:38
  • @Karthik OK, see the updated answer. This is the sort of thing you need to make clear in your question though. – terdon May 17 '16 at 12:55
0
( echo $(cat file2) ; cat file1 ) | column -t > file3
  • It doesn't give the desired output. The resultant file is very distorted. – Karthik May 18 '16 at 8:58
  • It works even with arbitrary long hostnames and arbitrary spaced values. It is assuming the line ending is the UNIX one and that you use UNIX tool like cat to read the file. What tool are you using to read your file. As long as you don't provide what is asked in the comment of your question nobody can helps you I'm afraid. – Emmanuel May 18 '16 at 9:35
  • i am using cat to view the resultant file on the unix host. – Karthik May 20 '16 at 9:57

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.