- Community Home
- >
- Servers and Operating Systems
- >
- Operating Systems
- >
- Operating System - HP-UX
- >
- Replace whole line using sed or awk
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
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
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
тАО07-30-2003 10:06 PM
тАО07-30-2003 10:06 PM
I need to replace an entire line that contains a certain string with another line.
Does anyone know how I could do this? If awk or sed are not the best tools to do this I am open to suggestions.
Sorry if I am duplicating, but I have been reading man pages and trawling the posts on this website for the last 2 hours.
Cheers
Ross
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО07-30-2003 10:15 PM
тАО07-30-2003 10:15 PM
Re: Replace whole line using sed or awk
$ sed 's /string/new string/g' old_filename >new_filename
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО07-30-2003 10:18 PM
тАО07-30-2003 10:18 PM
Re: Replace whole line using sed or awk
Don't know if this is what you're after:
sed 's/
Then mv the newfile over the old.
Cheers
Con
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО07-30-2003 10:25 PM
тАО07-30-2003 10:25 PM
Re: Replace whole line using sed or awk
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО07-30-2003 10:29 PM
тАО07-30-2003 10:29 PM
Re: Replace whole line using sed or awk
For Example:
I have a file which sometimes contains this text.
quick brown fox
and sometimes contains
slow brown fox
I want to search for "brown" and then replace the entire line.
Any ideas?
Thanks
Ross
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО07-30-2003 10:31 PM - last edited on тАО03-23-2017 03:06 AM by VidyaVI
тАО07-30-2003 10:31 PM - last edited on тАО03-23-2017 03:06 AM by VidyaVI
Re: Replace whole line using sed or awk
Have a look at this thread, it may helps:
https://community.hpe.com/t5/System-Administration/bd-p/itrc-156#.WNOdhE27odk
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО07-30-2003 10:56 PM
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО07-31-2003 11:33 PM
тАО07-31-2003 11:33 PM
Re: Replace whole line using sed or awk
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО08-01-2003 01:41 AM
тАО08-01-2003 01:41 AM
Re: Replace whole line using sed or awk
Script on perl:
#!/usr/bin/perl
open (FD, "SRC_FILENAME");
open (FDN, ">TRGET_FILENAME");
foreach (
{
if (/STRING_FOR_REPLACE/)
{
print FDN "NEW_STRING_LINE";
}
else
{
print FDN "$_";
}
}
close (FDN);
close (FD);
#END
Caesar
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО08-01-2003 02:00 AM
тАО08-01-2003 02:00 AM
Re: Replace whole line using sed or awk
I should think this would do what I expect You to ask for.
If brown exist anyware in the input row, case independent, the whole input line is replaced by a new standard row.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО08-01-2003 02:18 AM
тАО08-01-2003 02:18 AM
Re: Replace whole line using sed or awk
awk ' toupper($0) ~ / BROWN[\.,!? ]/ {print "new row"}' input > output
This version acts only when 'brown' is used as a sepatate word, and not for instnace as 'brownies'. It accepts ".", ",", "!", "?" and " " as the character after 'brown'.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО08-01-2003 05:53 AM
тАО08-01-2003 05:53 AM
Re: Replace whole line using sed or awk
A nice simple approach:
Say the string you are looking for is STRING_TO_BE_REPLACED.
And the line you want to insert is LINE_TO_REPLACE_WITH.
The input file is 'file', the output file is 'new_file':
cat file | sed 's/^.*STRING_TO_BE_REPLACED.*$/LINE_TO_REPLACE_WITH/' >new_file
The ^ is a start of line anchor, the '.*' is a wildcard, the $ is a end of line anchor.
Personally I think this looks a lot cleaner than most of the other ways to do this I've seen here.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО02-10-2005 02:48 AM
тАО02-10-2005 02:48 AM
Re: Replace whole line using sed or awk
This is how it works well:
awk '{if(toupper($0) ~ / BROWN[;\.,!? ]/) {print "new row"} else {print $0}}' input
The text "brown" will trigger a complete different standard line to be printed instead of the line "brown" is found in. But the line will only be replaces if "brown" begins with a space, and if the character after "brown" is one of ";.,!?" or space. The check will ignore case.
If it looks like "[" or "]" that should be replaces with sqare brackets instead.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО02-10-2005 02:48 AM
тАО02-10-2005 02:48 AM
Re: Replace whole line using sed or awk
This is how it works well:
awk '{if(toupper($0) ~ / BROWN[;\.,!? ]/) {print "new row"} else {print $0}}' input
The text "brown" will trigger a complete different standard line to be printed instead of the line "brown" is found in. But the line will only be replaced if "brown" begins with a space, and if the character after "brown" is one of ";.,!?" or space. The check will ignore case.
If it looks like "[" or "]" that should be replaces with sqare brackets instead.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО02-10-2005 05:26 AM
тАО02-10-2005 05:26 AM
Re: Replace whole line using sed or awk
# file1 contains line to insert
REPLN=`cat file1`
# or set the variable with the line to be #inserted
REPLN="dumb ol fox"
sed "s/^.*$2.*$/$REPLN/ $1 > $1.a
mv $1.a $1 # overwrite the original file
run it
swtchln.sh myfile brown
course there's lots of mods you can make to this, but you get the gist of it.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО02-01-2009 05:22 PM
тАО02-01-2009 05:22 PM