System Administration
cancel
Showing results for 
Search instead for 
Did you mean: 

Replace whole line using sed or awk

SOLVED
Go to solution
Ross W
Advisor

Replace whole line using sed or awk

Hi,

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
15 REPLIES
Michael Tully
Honored Contributor

Re: Replace whole line using sed or awk

Is this what you after?

$ sed 's /string/new string/g' old_filename >new_filename
Anyone for a Mutiny ?
Con O'Kelly
Honored Contributor

Re: Replace whole line using sed or awk

Hi Ross

Don't know if this is what you're after:

sed 's///' > /tmp/newfile.

Then mv the newfile over the old.

Cheers
Con
RAC_1
Honored Contributor

Re: Replace whole line using sed or awk

sed 's/string/new string/g' file > new_file
There is no substitute to HARDWORK
Ross W
Advisor

Re: Replace whole line using sed or awk

Very similar, but I need the whole line replaced, not just the search string text.

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
twang
Honored Contributor

Re: Replace whole line using sed or awk

john korterman
Honored Contributor
Solution

Re: Replace whole line using sed or awk

Hi,
the slow approach:

#!/usr/bin/sh
while read LINE
do
echo "$LINE" | grep -iq brown
if [ "$?" = "0" ]
then
echo "Replaced line"
else
echo "$LINE"
fi
done <$1


regards,
John K.
it would be nice if you always got a second chance
Ionut Grigorescu_2
Super Advisor

Re: Replace whole line using sed or awk

awk '{x=0;for(i=1;i<=NF;i++){if($i ~ /brown/) print "new line/string";x=1;break}};if (x==0) print $0}' your_file > new_file
If it weren't for STRESS I'd have no energy at all
Caesar_3
Esteemed Contributor

Re: Replace whole line using sed or awk

Hello!

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
Rolf Modin
Advisor

Re: Replace whole line using sed or awk

awk ' toupper($0) ~ /BROWN/ {print "new row"}'

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.

Rolf Modin
Advisor

Re: Replace whole line using sed or awk

Hm...

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'.


Stephen Day
Occasional Advisor

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.

It was like that when I got here.
Rolf Modin
Advisor

Re: Replace whole line using sed or awk

shame on me!

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.
Rolf Modin
Advisor

Re: Replace whole line using sed or awk

shame on me!

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.
Robert Salter
Respected Contributor

Re: Replace whole line using sed or awk

You could put it in a script if you have to do this more than once using a couple of different variables i.e;

# 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.
Time to smoke and joke
Ross W
Advisor

Re: Replace whole line using sed or awk

close