LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
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, 02: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, 02:24 AM   #2
Dark_Helmet
Senior Member
 
Registered: Jan 2003
Posts: 2,786

Rep: Reputation: 374Reputation: 374Reputation: 374Reputation: 374
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 02:33 AM.
 
Old 01-16-2005, 02: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, 04: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 04:51 PM.
 
Old 09-14-2008, 05:02 PM   #5
keefaz
LQ Guru
 
Registered: Mar 2004
Distribution: Slackware
Posts: 6,552

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

Rep: Reputation: 170Reputation: 170
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, 12: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 06:46 PM
A cool script to search within a hell of TXT files guarriman Linux - General 7 12-08-2004 07:21 AM
Script to search and replace in text file - kinda... jeffreybluml Programming 45 11-07-2004 05:37 PM
Search and replace text in file using shell script? matthurne Linux - Software 2 11-02-2004 10:11 AM
trying to search and replace text file for single & multiple line breaks separately brokenfeet Programming 7 08-29-2003 01:56 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 01:52 PM.

Main Menu
Advertisement
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
Open Source Consulting | Domain Registration