Insert lines at specific location in file

Hi There

I have this file that I would like to add entries to, however, there is a "}" as the last line that I need to keep. Basically i would like to know how I can write a script that will add new lines at the second to last line position (ie always add new line above the close bracket)

Now, I know how to do this by grep'ing tailing, appending etc using temporary files to put stuff in, but I wanted to do it in one simple command rather than 5 or 6 commands with other temp files involved that I have to remove afterwards (it looks very messy)

Is there any way to do this simply ????

stuff
stuff
stuff
<NEED TO ENTER LINE HERE>
}
ed file<<!
-1a
my new line
.
w
q
!

Try this...

sed '$ i this is  line you want to add' filename

Output:

u142115@linux2alm:~/aps/aps4/product/den> cat sc.txt
stuff
stuff
stuff
}
u142115@linux2alm:~/aps/aps4/product/den> sed '$ i this is the line' sc.txt
stuff
stuff
stuff
this is the line
}

Check out Here Documents

In exaple 18-2 a file is opend with vi and some lines are added. You could check for the last } in your file and then use the O to insert lines above this one.

If you always know that your } is on the last line (without trailing newline) you can also use G (jumping to the end of the file) and the use O to insert above this line.

The content as well the vi commands are embedded in the script as a so called here document as described in the example.

# try this
# inserting the string "after 10" after the line beginning with 10

'/^10 /i\
after 10' file

when I run this I get

[my.server] # sed '$ i this is the line' testfile
sed: command garbled: $ i this is the line
[my.server] #

Give an attempt on this... All works well here...

x="this is the line"
sed '$ i '"$x"'' file

must be a different version of sed unfortunatley, cos it doesnt work for me :frowning:

For older versions of sed:

sed '$i\
this is  line you want to add' file

how to append a string to an existing line!!!

sed "s/full_line/& your_string/" file

What about this...?This is more specific for the symbol "}"

 sed 's/\}/new\n&/'  file

O/p:

u142115@linux2alm:~/aps/aps4/product/den> cat sc.txt
stuff
stuff
stuff
}
u142115@linux2alm:~/aps/aps4/product/den> sed 's/\}/new\n&/' sc.txt
stuff
stuff
stuff
new
}

I mean with i or a command - like sami98 written -

# try this
# inserting the string "after 10" after the line beginning with 10

'/^10 /i\
after 10' file
Reply With Quote

how to append "is good" to line 10??

input:
10 sed
11 perl
12 java

output:
10 sed is good
11 perl
12 java

You can insert or append new line and not a string to existing line with i or a command.
To append a string,
If you have the original line, you can use change command to change original line with the modified line.

sed "1c\
original_line_plus_new_string" file
sed "10 s/$/is good/" file

@anbu23

sed "10 s/$/is good/" file # append the string "is good" to the end of line nr. 10
that is fine!!! ... but not exactly:

but I mean:
append the string "is good" to the end of the line that begins with "10" :frowning:

sed "/^10/s/$/is good/" file

thank u, anbu23 !!