Could you please try following and let me know if this helps you.
Output will be as follows. EDIT: Following may help you in same too, a little different from previous one(not having array here).
Output will be as follows.
Thanks,
R. Singh
Last edited by RavinderSingh13; 09-29-2016 at 09:35 AM..
These 2 Users Gave Thanks to RavinderSingh13 For This Post:
R. Singh's answer is great.
You can also write it this way :
Comments :
I don't support the ++ operator in the pattern section of the script . This is why i++ appears in the action section ( i.e between {} )
Parenthesis are useless in the first pattern section.
Last edited by blastit.fr; 09-29-2016 at 07:13 PM..
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)
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 ,
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)
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 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)
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,
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, 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)