0

I have two files. The first is just a single number. This number is calculated and changes every time. The second file has a bunch of columns, and I do not know the number of rows (which can change). For example, I have something like this:

File 1:

12

File 2:

1 2 3 
4 5 6
7 8 9

I want

1 2 3 12
4 5 6 12
7 8 9 12

My thoughts are that you could do a while not EOF then cat File 1 and File 2. How can I do so?

cuonglm
  • 153,898
Dani
  • 33

5 Answers5

4

Another awk:

$ awk 'BEGIN{getline l <"file1"};{print $0, l}' file2
1 2 3 12
4 5 6 12
7 8 9 12
  • BEGIN block was executed first before reading input file. The first line in file1 was retrieve using getline() function, stored in variable l
  • With each line of file2, we print it content $0 along with l, separated by OFS, which is a space by default.
cuonglm
  • 153,898
  • Works great, thank you! Would you mind explaining exactly what is going on? :) – Dani Aug 25 '15 at 08:45
  • @dani If this solved your problem (or one of the other answers) you shoudl accept it (by clicking the check mark next to the answer) That will let others know the question has been resolved, let others with the same question know what worked for you and give both you and the answerer more reputation – Eric Renouf Aug 26 '15 at 01:33
2

Using awk

$ awk -v n=$(cat file1) '{print $0,n}' file2
1 2 3 12
4 5 6 12
7 8 9 12

On csh/tcsh, try:

awk -v n=`cat file1` '{print $0,n}' file2

How it works

  • -v n=$(cat file1)

    This assigns the contents of file1 to the awk variable n.

  • print $0,n

    This prints each line followed by n.

Using sed

$ sed '1{h;d}; G;s/\n/ /' file1 file2
1 2 3 12
4 5 6 12
7 8 9 12

How it works

  • 1{h;d}

    The first line read is saved in the hold space and then deleted from that pattern space (so it doesn't print).

  • G; s/\n/ /

    For all subsequent lines, we use the get command, G. This appends to the patterns space a newline and the hold space. We then replace that newline with a space.

John1024
  • 74,655
  • 1
    Note that using -v var=value will expand escape sequence in var. – cuonglm Aug 25 '15 at 08:38
  • Thank you, although I think the awk should work, I get: "Illegal variable name." The sed command does work! I'd love to know why the awk command is not working? Also I should add I'm getting the same "Illegal variable name." error when I try to run the first two answers using sed. Thanks everyone for your help. – Dani Aug 25 '15 at 08:44
  • @Dani That sounds like you are using some shell like csh or tcsh. My awk and two of the other sed solutions use $(...) for command substitution. This is supported by all POSIX compatible shells. On csh, the $(...) notation needs to be replaced by backticks: see updated answer. – John1024 Aug 26 '15 at 02:54
0

This should work (in this case file2 gets updated, if you don't want that, remove the -i flag)

   sed -i "s/.*/& $(cat file1)/" file2
hmontoliu
  • 1,947
  • 12
  • 11
0
$ sed -e "s/ *\$/ $(cat file1)/" file2
1 2 3 12
4 5 6 12
7 8 9 12
yaegashi
  • 12,326
0

For sed lovers — read file1 for each line in file2 it is not good idea. Much better to use variable:

my_var=$(<file1) ; sed "s/$/ $my_var/" file2

or more secure

read -r my_var <file1 ; sed "s/$/ $my_var/" file2
Costas
  • 14,916
  • When you say, "read file1 for each line in file2 it is not good idea", are you referring to other sed answers wihch use $(cat file1) within a sed expression? If so, then, it is worth noting that $(cat file1) is expanded by the shell on the command line so sed gets a literal value. – Also, what is more secure about using read -r? Though the -r disables backshash(escape) sequence translation, a variable containing a backslash would be interpreted as an escape character (unless it was embedded in a character range expression, eg. [abc\]. – Peter.O Aug 25 '15 at 12:45
  • @Peter.O In file1 can be more then 1 line thus it break out the script instead of read which takes 1 line only. Are you sure that $(cat) expanding is not provided for each line? – Costas Aug 25 '15 at 15:32
  • Ok, point taken. In that case, $(head -n1 file1) would be equivalent to the read -r method, and thereby safer than either $(cat file1) or $(<file1). Nonetheless, all these methods (includingread -r) are subject to the escape-sequence interpretation. – However, that being said, all 4 methods are fine, if the data is as per the stated specs. – Peter.O Aug 25 '15 at 16:44