0

Real nit picky Linux question.

I have a text file, call it userec. I also have a string variable 'var_a'.

I want to concatenate the string value, let just say it's 'howdy' to the top of the text file.

So something like

echo $var_a | cat usrec > file_out

where it pipes the output from the echo $var_a as a file and adds it to the top of file_out and then adds the rest of the usrec file.

So if the userec file contains just the line 'This is the second line' then the contents of file_out should be:

howdy
This is the second line.

problem is that's not what the command is doing and I do not want to create a variable to store var_a in. This is running from a script and I don't want to create any extra flack to have to clean up afterwards.

I've tried other variations and I'm comming up empty.

Can anyone help me?

Carbon
  • 313
  • 3
  • 11

2 Answers2

0

If you give cat any file names then it does not automatically read its standard input. In that case, you must use the special argument - in the file list to tell it to read the standard input, and where to include it in the concatenated output. Since apparently you want it to go at the beginning, that would be:

echo $var_a | cat - usrec > file_out
John Bollinger
  • 160,171
  • 8
  • 81
  • 157
0

I would simply do :

echo $var_a > file_out
cat usrec >> file_out
Richard
  • 11
  • 3