Another approach is to use ed (I learnt it from Don C on this forum, thanks!)
Let's say I need to insert content of file f2 into file f1 before line starting with 3.
This script says go to the beginning of the file (H),
then find first line starting with 3 (/^3)
then step one line up (-1)
then read in content of f2 (r f2)
then print out whole new content with (p), then quit (Q)
Result
Almost...
The H command in ed doesn't move to the beginning of the file, it tells ed that if any of the following commands fail, ed should print a textual diagnostic message (or Help message) explaining what went wrong instead of just printing a question mark.
And, if I understand the requirements correctly, instead of printing results to the terminal, we want to update the file we are editing in place. So, in this case we would need something more like:
Where ed -s file1 uses ed to open a file named file1 without printing the size of the file when we open it and without printing the size of the file again when the script writes the updated file and <<EOF says that the following lines until we find a line that contains only EOF is to be treated as commands to be read and execute by ed. And the commands given to ed are:
H to turn on verbose help messages,
/^unix/-1r file2 to search for the first line starting with the string unix, move up one line from there, and read the contents of the file named file2 into the editing buffer after that line,
w to write the updated contents of the editing buffer back to the file named on the command line, and
q to cleanly quit. The q command can be skipped here and get the same results; I just like to tell ed to quit instead of having it hit EOF on the here-document when it is looking for another command to execute.
The things that could go wrong in this script include:
not finding a line that starts with the string unix, and
not finding a file named file2.
Getting a message likescript, line 2: no match or script, line 2: cannot open input file instead of just a ? helps the user of your script figure out what went wrong. That is why I give ed the H command (even though I know my script will work perfectly ).
This User Gave Thanks to Don Cragun For This Post:
Hi, it's my first post to this forum. I just started bash and I'm stuck at one issue. I want to include content of a file in another file after a certain line. I'm using sed for inserting one line but how to insert all content of a file ?
For example i have a file list.txt with a few lines and... (4 Replies)
Hi,
I am getting crazy after days on looking at it:
Bash in Ubuntu 12.04.1
I want to do this:
pattern="system /path1/file1 file1"
new_pattern=" data /path2/file2 file2"
file to edit: data.db
- I need to search in the file data.db for the nth occurrence of pattern
- pattern must... (14 Replies)
Hi,
I would like to know how, using sed, be able to insert contents of file2 in file1 after say the second occurrence of a given string? e.g.
> cat file1
banana
apple
orange
apple
banana
pear
tangerine
apple
> cat file2
I don't like apples
What would be the sed command to insert... (5 Replies)
Hi friends, here is my problem.
I have three files like this..
cat file1.txt
=======
unix is best
unix is best
linux is best
unix is best
linux is best
linux is best
unix is best
unix is best
cat file2.txt
========
Windows performs better
Mac OS performs better
Windows... (4 Replies)
suppose i have original file:
original.txt:
hello
how are you
you are wonderful
what time is it
I went to the store last night. and some apple juice
then i have another file:
anotherfile.txt:
with my friends mary, john and harry.
We had a great time.
We bought food
Suppose... (1 Reply)
Hi ,
I'm looking for some code that can copy and paste form file1 to file2 with 2 criterial meet.
file1:
test "sp-j1"
test "sp-j2"
test "sp-j3"
test "sp-j4"
file2:
sub Pre_Shorts1 (Status_Code, Message$)
global Status
!if Message$ <> "" then print... (3 Replies)