-   Linux - Newbie (
-   -   Does anyone know of a bash script can search & replace txt in a file. (

jimwelc 01-16-2005 03:08 AM

Does anyone know of a bash script can search & replace txt in a file.
Hello All,

I am new to bash scripting world and I am trying to search a file and replace text within it. Basically I have a file that looks like this:

#---------File "A.spec" snippet ------------
Summary: My file
Name: ctasetup
Version: 1.0.0
Release: 1
#---------File "A.spec" snippet ------------

What I would like to do in my script is right to search for the line "Version: " "Release: " and replace the text on that line to something different.

Any help you can provide will be greatly appreciated.

Best Regards,

Dark_Helmet 01-16-2005 03:24 AM

You can try this:

sed -i.backup -e 's/Version: 1.0.0/Version: 1.2.3/' -e 's/Release: 1/Release: 2/' A.spec
That command will replace "Version: 1.0.0" with "Version: 1.2.3" and similarly, "Release: 1" with "Release 2". Obviously, change the "1.2.3" and "2" to match whatever it is your situation needs. It will make the changes "in file", but will create a copy of your original file, and save it as A.spec.backup in case something goes wrong.

You can do lots of interesting things with sed expressions. It's worth the time to read up on them.

Something I forgot to mention. That command will replace any matching text within the file. So if you want to replace only the copies of "Version" and "Release" that occur on their own lines, then this command will work:

sed -i.backup -e 's/^Version: 1.0.0$/Version: 1.2.3/' -e 's/^Release: 1$/Release: 2/' A.spec

stomfi 01-16-2005 03:35 AM

Obviously you can use a one line sed program to do what you want

But I thought you might like to see how clever you can be with awk grep and sed

This is part of of script for replacing lines in a recipe application using Runtime Revolution for the GUI bit and the shell for information processing, soon to be published on my page at Cool Solutions, but it shows the method.
These scripts are naive as they are meant for naive newbies

#!/bin/bash indexno lineno newline type
#replace lineno for indexno in notes.txt with newline
if [ $1 -lt 1 ]

if [ $TYPE -lt 1 ]
#This is a modification line
#First select all the notes for this recipe index
#to find out which line to replace
awk -v IDX="$INDEX" 'BEGIN{RS = "#"}{if(NR == IDX){print $0}}' $NOTEFILE |\
awk '{if(length() > 3) print $0}'|\
awk -v LINNO="$2" '{if(NR == LINNO) print $0}' > $CTMP/thisone.txt
CLINE=`cat $CTMP/thisone.txt`
if [ ${#CLINE} -lt 1 ]
echo "Couldn't find line $2"
#Now we find the line number in the original file
CLINNO=`grep -n -E "$CLINE" $NOTEFILE | awk -F":" '{print $1}'`
#We use sed to insert the new line then delete the old line
cp $NOTEFILE $CTMP/newnotes.txt
sed -e "$CLINNO"i"$NLINE" -e "$CLINNO"d $CTMP/newnotes.txt > $CTMP/notes.txt

You can find a lot more scripts at this level on my pages at

thecha 09-14-2008 05:50 PM


Originally Posted by Dark_Helmet (Post 1411103)
You can try this:

sed -i.backup -e 's/Version: 1.0.0/Version: 1.2.3/' -e 's/Release: 1/Release: 2/' A.spec

I need to do something very similar to this... I need to edit a text file to change all instances of letters with accents to their HTML equivalent for example becomes è here is what I tried.


sed -i.backup -e 's//ì/' -e 's//è/' -e 's//à/' -e 's//ù/'  input.txt

However here is a small section of the resulting text file. It seems to be inserting other weird symbols too.


Se c'√egrave;
it should be


Se c'è
The & seems to be inserted as for some reason?

keefaz 09-14-2008 06:02 PM

maybe try ' export LC_ALL=c ' before issuing your commands

Kenhelm 09-14-2008 07:07 PM

Change & to \&
The character '&' has a special meaning in a sed s command.
Preceding it with '\' escapes it into a literal character.

thecha 09-15-2008 01:13 AM

Thank you Kenhelm it worked perfectly!

All times are GMT -5. The time now is 04:35 PM.