LinuxQuestions.org
Review your favorite Linux distribution.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices



Reply
 
Search this Thread
Old 09-06-2005, 01:51 PM   #1
happy78
Member
 
Registered: Aug 2003
Posts: 50

Rep: Reputation: 15
find and replace


Hello guys,

I have a file with thousands of line. Some of them are:

Enter value for a:
val a = tag
....
Enter value for b
val b = tag
.....
Enter value for c:
val c=tag

each "tag" represents an entered value. Assumming I have:
tag #1 = 10
tag #2 = 11
tag #3 = 12

1) How do I use "sed" to find and replace each tag accordingly?
Right now, everytime i use sed 's/10/tag/`, it ends up replacing every tag with 10. I want to be able to replace the 1st tag w/ 10, 2nd tag w/11, 3rd tag with 12.

2) How do I delete any line in a file that starts with lettter "a" using bash script?

Thank you for your time and suggestion.
 
Old 09-06-2005, 01:56 PM   #2
Matir
Moderator
 
Registered: Nov 2004
Location: San Jose, CA
Distribution: Ubuntu
Posts: 8,507

Rep: Reputation: 118Reputation: 118
For your second question, try:
Code:
grep -v '^a' FILE
 
Old 09-06-2005, 02:17 PM   #3
happy78
Member
 
Registered: Aug 2003
Posts: 50

Original Poster
Rep: Reputation: 15
I found solution to #2:

sed -e '/^?/d' afile

delete any line that starts with "?"
 
Old 09-06-2005, 02:24 PM   #4
Matir
Moderator
 
Registered: Nov 2004
Location: San Jose, CA
Distribution: Ubuntu
Posts: 8,507

Rep: Reputation: 118Reputation: 118
Or that. I just thought grep might be simpler for you.

For your first problem, sed is line-based. You'll need to figure out some way to change its state after a match has been found.
 
Old 09-06-2005, 07:03 PM   #5
Dave Kelly
Member
 
Registered: Aug 2004
Location: Todd Mission Texas
Distribution: Linspire
Posts: 213

Rep: Reputation: 31
I'm not sure I can answer you request fully but here is what I just had to do.

I needed to change the line feed ( 0x0A ) to a space (0x20 ) in a file that takes the form:
Code:
/]etc/mplayer/codecs.conf
/etc/mplayer/dvb-menu.conf
/esr/lib/mozilla/components/mplayerplug-in.xpt
/usr/lib/mozilla-firefox/components/mplayerplug-in.xpt
/usr/lib/mozilla-firefox/plugins/mplayerplug-in.so
/usr/lib/mozilla/plugins/mplayerplug-in.so
/usr/lib/transcode/import_mplayer.so
/usr/share/doc/mplayerplug-in/changelog.Debian.gz
/usr/share/doc/mplayerplug-in/changelog.gz
/usr/share/doc/mplayerplug-in/copyright
/usr/share/doc/mplayerplug-in/README
/usr/share/doc/mplayerplug-in/README.Debian
/cache/cnrclient/pool-bin/m/mplayer
/var/cache/cnrclient/pool-bin/m/mplayer-fonts
so I could copy and paste them after 'rm -f' into one long line.

My copy of "Linux in a Nutshell" shows as an example:

Code:
tr ' ' '\012' < file
Which is the exact opposite of what I wanted. Simple swap gave me the desired results.

Be aware that 'tr' requires the numerical input to be of type octal.

So if you've got 3 values to change you might write a script that call 'tr' 3 times. Once for each value.

HtH
Dave
 
Old 09-06-2005, 07:03 PM   #6
happy78
Member
 
Registered: Aug 2003
Posts: 50

Original Poster
Rep: Reputation: 15
thank you matir,

I know exactly which line that has "tag". How do I go to a specific line, Then replace the tag w/ a value using script?
 
Old 09-06-2005, 11:37 PM   #7
carl.waldbieser
Member
 
Registered: Jun 2005
Location: Pennsylvania
Distribution: Kubuntu
Posts: 197

Rep: Reputation: 32
Quote:
Originally posted by happy78
thank you matir,

I know exactly which line that has "tag". How do I go to a specific line, Then replace the tag w/ a value using script?
The more general form of sed is "address command", where address can be line numbers or regular expressions. For example:

Code:
$ cat temp.txt | sed 3s/one/seven/
Replaces the text "one" with "seven", but only for line 3 of the file.
 
Old 09-07-2005, 01:11 PM   #8
happy78
Member
 
Registered: Aug 2003
Posts: 50

Original Poster
Rep: Reputation: 15
Talking

Carl,
It works like a charm... Thank you very much, Carl.
Thanks everyone else for your efforts.
 
Old 09-07-2005, 01:21 PM   #9
happy78
Member
 
Registered: Aug 2003
Posts: 50

Original Poster
Rep: Reputation: 15
Talking

Carl,
It works like a charm... Thank you very much, Carl.
Thanks everyone else for your efforts.
 
Old 09-10-2005, 10:00 AM   #10
eddiebaby1023
Member
 
Registered: May 2005
Posts: 378

Rep: Reputation: 33
Quote:
I needed to change the line feed ( 0x0A ) to a space (0x20 ) in a file
echo is the easiest way to do this
Code:
rm -f $(echo $(< file))
 
Old 09-10-2005, 10:58 AM   #11
Dave Kelly
Member
 
Registered: Aug 2004
Location: Todd Mission Texas
Distribution: Linspire
Posts: 213

Rep: Reputation: 31
Quote:
Originally posted by eddiebaby1023
echo is the easiest way to do this
Code:
rm -f $(echo $(< file))
Dave goes into learning mode to ask questions.
Could you detail the actions here. I don't full understand how this would change a linefeed to a space?

Thanks
Dave
 
Old 09-10-2005, 11:21 AM   #12
jlliagre
Moderator
 
Registered: Feb 2004
Location: Outside Paris
Distribution: Solaris10, Solaris 11, Mint, OL
Posts: 9,523

Rep: Reputation: 365Reputation: 365Reputation: 365Reputation: 365
echo is indeed replacing the linefeeds by spaces, but actually is useless here as you can directly use the simpler:
Code:
rm -f $(< file)
There is no need to put all the arguments on a single line.
 
  


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
Find and replace string Johng Programming 9 01-13-2010 05:50 AM
Help - how to find and replace in Vim stardotstar Linux - Software 7 10-15-2004 12:31 AM
Find & Replace Benr Linux - Newbie 5 05-03-2004 03:37 PM
find with grep and replace it dominant Linux - Newbie 4 03-03-2004 02:11 PM
Find and Replace? duerra Linux - General 9 01-28-2004 05:07 AM


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