Organizational Research By

Surprising Reserch Topic

Question:add line at top and bottom of file before sending it to pipe


I have a text file, say, text.

What I want to do is to put the text file into pipe (as in cat text |), but with added line at top and bottom - but without creating a new file (because the text file is kind of big) or modifying it.

Is that possible? I was thinking echo $(echo "line"; cat text; echo "line") but I don't like that.

asked Sep 13, 2013 in Java Interview Questions by anonymous
edited Sep 12, 2013
0 votes
23 views



Related Hot Questions

2 Answers

0 votes
From man bash:

    (list) list is executed in a subshell environment (see COMMAND EXECUTION ENVIRONMENT below). Variable assignments and builtin commands that affect the shell's environment do not remain in effect after the command completes. The return status is the exit status of list.

So this should work:

(echo first line; cat file; echo last line) | some_command

As suggested by chepner, when you don't need a subshell you can use {} instead of (). In this case, the ; is mandatory including for the last command in the group.
answered Sep 13, 2013 by anonymous
edited Sep 12, 2013
0 votes
From man bash:

    (list) list is executed in a subshell environment (see COMMAND EXECUTION ENVIRONMENT below). Variable assignments and builtin commands that affect the shell's environment do not remain in effect after the command completes. The return status is the exit status of list.

So this should work:

(echo first line; cat file; echo last line) | some_command

As suggested by chepner, when you don't need a subshell you can use {} instead of (). In this case, the ; is mandatory including for the last command in the group.
answered Sep 13, 2013 by anonymous
edited Sep 12, 2013

...