LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
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 11-13-2010, 06:11 PM   #1
ieatbunnies
Member
 
Registered: Feb 2010
Posts: 37

Rep: Reputation: 14
Would you recommend sed to .....


I have a document.
I want to replace all the c's and C's in it with %1
all the f's and F's in it with %2

i can get sed to do one of them but for some reason i can't get the script to do all the conversions at once.
 
Old 11-13-2010, 06:15 PM   #2
Dark_Helmet
Senior Member
 
Registered: Jan 2003
Posts: 2,786

Rep: Reputation: 373Reputation: 373Reputation: 373Reputation: 373
Have you tried:
Code:
sed -e <pattern1> -e <pattern2> -e . . .
 
Old 11-13-2010, 06:15 PM   #3
aluser
Member
 
Registered: Mar 2004
Location: Massachusetts
Distribution: Debian
Posts: 557

Rep: Reputation: 42
My bet is that you're missing a "g" at the end of your substitution commands. e.g.
Code:
$ echo 'abcdefghABCDEFGH' | sed 's/[fF]/%2/g 
> s/[cC]/%1/g'
ab%1de%2ghAB%1DE%2GH
Without 'g', s/// only replaces the first instance of the search pattern that it finds in each line.

edit: oops, I missed Dark Helmet's post. He's right, maybe the problem is confusion about how to do multiple sed commands at once. The multiple -e options is a cleaner way to do it than the literal newline that I used.

Last edited by aluser; 11-13-2010 at 06:17 PM. Reason: race with Dark Helmet
 
Old 11-13-2010, 08:09 PM   #4
ieatbunnies
Member
 
Registered: Feb 2010
Posts: 37

Original Poster
Rep: Reputation: 14
Code:
#!/bin/bash
#
#
for fl in *.txt; do
mv $fl $fl.old
sed -e 's/d/%2/' -e 's/a/%1/' -e 's/e/%3/' $fl.old > $fl
done
cause i want to do all the txt files in that directory and make a backup.
but that seemed to do the trick the -e option. i want to be able to select the file i want to do this too so more reading for me .;'


thanks peeps.
 
Old 11-14-2010, 06:06 AM   #5
crts
Senior Member
 
Registered: Jan 2010
Posts: 1,620

Rep: Reputation: 451Reputation: 451Reputation: 451Reputation: 451Reputation: 451
Quote:
Originally Posted by ieatbunnies View Post
Code:
#!/bin/bash
#
#
for fl in *.txt; do
mv $fl $fl.old
sed -e 's/d/%2/' -e 's/a/%1/' -e 's/e/%3/' $fl.old > $fl
done
cause i want to do all the txt files in that directory and make a backup.
but that seemed to do the trick the -e option. i want to be able to select the file i want to do this too so more reading for me .;'


thanks peeps.
Hi,

You do not need a loop. sed can do this:
Code:
sed -i.old 's/[cC]/%1/g;s/[fF]/%2/g' *.txt
[EDIT]
Just read that you want to select the file, so you do need a loop for your purpose. But you do not need to manually make a backup first. The -i option in sed will create a backup of the file if you give it an extension, e.g. -i.old will create a backup file with extension '*.old ' and before editing the file.

Last edited by crts; 11-14-2010 at 06:12 AM.
 
  


Reply


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
sed: how to change MAC address string with sed cold Linux - Software 5 08-02-2010 08:43 AM
bash script with grep and sed: sed getting filenames from grep odysseus.lost Programming 1 07-17-2006 12:36 PM
[sed] "Advanced" sed question(s) G00fy Programming 2 03-20-2006 01:34 AM
sed and escaping & in something like: echo $y | sed 's/&/_/g' prx Programming 7 02-04-2005 12:00 AM
Insert character into a line with sed? & variables in sed? jago25_98 Programming 5 03-11-2004 07:12 AM

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

All times are GMT -5. The time now is 11:14 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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration