LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices

Reply
 
Search this Thread
Old 01-16-2005, 03:08 AM   #1
jimwelc
Member
 
Registered: Nov 2004
Location: Boston
Posts: 30

Rep: Reputation: 15
Question 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
 
Old 01-16-2005, 03:24 AM   #2
Dark_Helmet
Senior Member
 
Registered: Jan 2003
Posts: 2,786

Rep: Reputation: 369Reputation: 369Reputation: 369Reputation: 369
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

Last edited by Dark_Helmet; 01-16-2005 at 03:33 AM.
 
Old 01-16-2005, 03:35 AM   #3
stomfi
Member
 
Registered: Aug 2002
Location: Brisbane, Australia
Distribution: Ubuntu
Posts: 35

Rep: Reputation: 15
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
 
Old 09-14-2008, 05:50 PM   #4
thecha
LQ Newbie
 
Registered: Sep 2008
Posts: 5

Rep: Reputation: 0
Question

Quote:
Originally Posted by Dark_Helmet View Post
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?

Last edited by thecha; 09-14-2008 at 05:51 PM.
 
Old 09-14-2008, 06:02 PM   #5
keefaz
Senior Member
 
Registered: Mar 2004
Distribution: Slackware
Posts: 4,617

Rep: Reputation: 136Reputation: 136
maybe try ' export LC_ALL=c ' before issuing your commands
 
Old 09-14-2008, 07:07 PM   #6
Kenhelm
Member
 
Registered: Mar 2008
Location: N. W. England
Distribution: Mandriva
Posts: 330

Rep: Reputation: 140Reputation: 140
Change & to \&
The character '&' has a special meaning in a sed s command.
Preceding it with '\' escapes it into a literal character.
 
Old 09-15-2008, 01:13 AM   #7
thecha
LQ Newbie
 
Registered: Sep 2008
Posts: 5

Rep: Reputation: 0
Smile

Thank you Kenhelm it worked perfectly!
 
  


Reply

Tags
character, escape, find, replace, text


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Help Strip / Remove attachments (Replace with TXT file if Possible) Vince0000 Linux - General 2 09-17-2010 07:46 PM
A cool script to search within a hell of TXT files guarriman Linux - General 7 12-08-2004 08:21 AM
Script to search and replace in text file - kinda... jeffreybluml Programming 45 11-07-2004 06:37 PM
Search and replace text in file using shell script? matthurne Linux - Software 2 11-02-2004 11:11 AM
trying to search and replace text file for single & multiple line breaks separately brokenfeet Programming 7 08-29-2003 02:56 PM


All times are GMT -5. The time now is 10:08 PM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration