LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (http://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   Does anyone know of a bash script can search & replace txt in a file. (http://www.linuxquestions.org/questions/linux-newbie-8/does-anyone-know-of-a-bash-script-can-search-and-replace-txt-in-a-file-278332/)

jimwelc 01-16-2005 02: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,
-Jim

Dark_Helmet 01-16-2005 02:24 AM

You can try this:
Code:

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.

EDIT:
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:
Code:

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 02:35 AM

Obviously you can use a one line sed program to do what you want
sed -e "s/YOUR_ORIG_PATTERN/SUBSTITUTE_PATTERN/" INPUT_FILE > OUTPUT_FILE
cat OUTPUT_FILE > INPUT_FILE

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
#edrnote.sh indexno lineno newline type
#replace lineno for indexno in notes.txt with newline
NOTEFILE="$HOME/cookbook/book/notes.txt"
CTMP="$HOME/cookbook"
if [ $1 -lt 1 ]
then
exit
fi
INDEX=$1
LINENO=$2
NLINE="$3"
TYPE=$4

if [ $TYPE -lt 1 ]
then
#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 ]
then
echo "Couldn't find line $2"
exit
fi
#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
fi

You can find a lot more scripts at this level on my pages at
http://www.novell.com/coolsolutions/author/1211.html

thecha 09-14-2008 04:50 PM

Quote:

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

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.

Code:

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.


Quote:

Se c'√«egrave;
it should be

Quote:

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

keefaz 09-14-2008 05:02 PM

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

Kenhelm 09-14-2008 06: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 12:13 AM

Thank you Kenhelm it worked perfectly!


All times are GMT -5. The time now is 12:13 AM.