- Community Home
- >
- Servers and Operating Systems
- >
- Operating Systems
- >
- Operating System - HP-UX
- >
- Adding second line with sed to file
Categories
Company
Local Language
Forums
Discussions
Forums
- Data Protection and Retention
- Entry Storage Systems
- Legacy
- Midrange and Enterprise Storage
- Storage Networking
- HPE Nimble Storage
Discussions
Forums
Discussions
Discussions
Discussions
Forums
Forums
Discussions
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
- BladeSystem Infrastructure and Application Solutions
- Appliance Servers
- Alpha Servers
- BackOffice Products
- Internet Products
- HPE 9000 and HPE e3000 Servers
- Networking
- Netservers
- Secure OS Software for Linux
- Server Management (Insight Manager 7)
- Windows Server 2003
- Operating System - Tru64 Unix
- ProLiant Deployment and Provisioning
- Linux-Based Community / Regional
- Microsoft System Center Integration
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Community
Resources
Forums
Blogs
- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Printer Friendly Page
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО05-26-2005 10:38 AM
тАО05-26-2005 10:38 AM
text into a file(not end of file) when
replacing one line with sed.
example:
Look for string in file of "#end":
#end
somewhare in file and add "newline" string plus "#end" string afterwords:
newline
#end
It is of course easy to use:
sed s/string1/strings2/
But I just can't get the new line of text added afterwords.
sed s/strings1/strings2\nstring1/ ????
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО05-26-2005 11:00 AM
тАО05-26-2005 11:00 AM
SolutionI tweak this little script everytime I use it as my needs change, but you can get the basic idea. chop up the file, insert your text and reconstruct it.
Here it goes and hope it helps.
FILE=File_to_be_edited
PATTERN="whatever you wish to find"
# find your line number which contains the pattern
findline=`grep -n "${PATTERN}" $FILE|cut -d: -f1`
# cut and paste whatever is before this line into a header section
sed -e "${findline},\$d" ${FILE} > /tmp/file_header
# cut and paste whatever is after this line into a footer section
let f_minus_one=${findline}-1
sed -e "1,${f_minus_one}d" ${FILE} > /tmp/file_footer
# reconstruct your FILE
#
# put the header section first
cat /tmp/file_header > ${FILE}
# add your insertions
echo "my inserted line" >> ${FILE}
echo "another inserted line" >> ${FILE}
# finish off with putting whatever is in the footer section
cat /tmp/file_footer >> ${FILE}
UNIX because I majored in cryptology...
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО05-26-2005 12:00 PM
тАО05-26-2005 12:00 PM
Re: Adding second line with sed to file
sed '/s1/{h;s/s1/s2/;G;}'
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО05-26-2005 04:26 PM
тАО05-26-2005 04:26 PM
Re: Adding second line with sed to file
sed -e 's/string1/&string2/g'
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО05-26-2005 07:05 PM
тАО05-26-2005 07:05 PM
Re: Adding second line with sed to file
sed -e 's/#end/newline\
#end/g'
Else you can go with awk as,
awk '{ if ( $0 ~= "#end" ) { printf "newline\n"$0"\n" }}'
hth.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО05-27-2005 01:14 AM
тАО05-27-2005 01:14 AM
Re: Adding second line with sed to file
I could not however get the others to
work.
# sed "s/#end/{h;s/#end/test/;G;}" rules.test
sed: command garbled: s/#end/{h;s/#end/test/;G;}
Not sure if the syntax is right below. $0 - ??
# awk '{ if ($0 ~= "#end" ) { printf "newline\n"$0"\n" }}' rules.test
awk: syntax error near line 1
awk: illegal statement near line 1
awk: syntax error near line 1
awk: bailing out near line 1
Does not give me a new line for the string
"test" but it seems to be close. Seems like
you could put some kind of switch in between
& and the string "test".
sed "s/#end/&test/g" rules.test
#endtest
------------------------------------
I tried this one already.
# sed -e "s/#end/test\
newline/g" rules.test
testnewline
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО05-27-2005 01:40 AM
тАО05-27-2005 01:40 AM
Re: Adding second line with sed to file
in regards to:
I could not however get the others to work.
# sed "s/#end/{h;s/#end/test/;G;}" rules.test
sed: command garbled: s/#end/{h;s/#end/test/;G;}
you have the syntax wrong; it should be:
sed '/#end/{h;s/#end/test/;G;}' rules.test
not:
sed "s/#end/{h;s/#end/test/;G;}"
aaaaa^, there is no "s" at the begining.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО05-27-2005 01:46 AM
тАО05-27-2005 01:46 AM
Re: Adding second line with sed to file
awk '{ if ( $0 ~= "#end" ) {
printf("newline\n%s\n",$0);}}'
or
awk '/#end/ {printf "newline\n%s\n",$0);}'
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО05-27-2005 01:49 AM
тАО05-27-2005 01:49 AM
Re: Adding second line with sed to file
The sed line Muthukumar showed works interactively, but it hard to do in a script.
You need that new newline character after the 'text for the new line'.
The awk suggestion needs a little tweak.
This works for me:
awk '/^\#end/{print "newline"} {print}'
demo below.
Hein.
#cat x
aap
end #end
noot
#end
#
# awk '/^\#end/{print "newline"} {print}' x
aap
end #end
noot
newline
#end
$ sed -e 's/#end/newline2\
> #end/g' x
aap
end newline2
#end
noot
newline2
#end
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО05-27-2005 03:19 AM
тАО05-27-2005 03:19 AM
Re: Adding second line with sed to file
Assume your data does have a Carriage Return
(^M, control M)
sed 's/#end/ XXXX ^M #end/g' |tr "\r" "\n"
You can insert a ^M, using vi by
Control V and then Control M.
If you know your data NEVER has a certain character like a ~, etc you could use that instead of a control character.
Rory
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО05-27-2005 03:48 AM
тАО05-27-2005 03:48 AM
Re: Adding second line with sed to file
typing sed s/
They both work.
Curt you had a typo though in:
awk '/#end/ {printf "newline\n%s\n",$0);}'
No ")" after 0 and it works.
Hein van den's works also.
Thanks Guys!