LinuxQuestions.org
Support LQ: Use code LQ3 and save $3 on Domain Registration
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 07-06-2004, 07:47 AM   #1
djgerbavore
Member
 
Registered: Jun 2004
Location: PA
Distribution: Fedora (latest git kernel)
Posts: 458

Rep: Reputation: 30
grep question


is there a way to grep a filename, and replace with what you greped with something else. I read over the manuel, but they are hard to read sometimes.

for example:
cat Makefiel | grep "#export install_path = /boot" and replace that string with somethign else.
 
Old 07-06-2004, 07:55 AM   #2
ppuru
Senior Member
 
Registered: Mar 2003
Location: Beautiful BC
Distribution: RedHat & clones, Slackware, SuSE, OpenBSD
Posts: 1,791

Rep: Reputation: 49
you may be able to achieve that with grep and sed.
 
Old 07-06-2004, 11:23 AM   #3
miss crump
LQ Newbie
 
Registered: May 2004
Location: Oklahoma City, OK
Distribution: Debian
Posts: 17

Rep: Reputation: 0
If I understand you correctly you could just use sed to do what you want. Here's an example (and someone please correct me if I get the syntax a little wrong, I'm at work on a Windoze machine and am thinking off the top of my head):

sed -e 's/^#export\ install_path\ =\ \/boot/new_string/' Makefiel

^ the beginning of a line
\ makes symbols (like /) and spaces be read correctly in the argument
 
Old 07-06-2004, 01:22 PM   #4
djgerbavore
Member
 
Registered: Jun 2004
Location: PA
Distribution: Fedora (latest git kernel)
Posts: 458

Original Poster
Rep: Reputation: 30
i tried your command and it didn't change it, it found the pattern and just printed out the replaced string in the console, but didn't replace the string in the actually file.

thanks anyways, i'm goin to look up some sed examples and see if i figure anything out.

edit: can i use sed to find a string pattern and replace it with something else?


Last edited by djgerbavore; 07-06-2004 at 01:40 PM.
 
Old 07-06-2004, 02:56 PM   #5
miss crump
LQ Newbie
 
Registered: May 2004
Location: Oklahoma City, OK
Distribution: Debian
Posts: 17

Rep: Reputation: 0
You have to output to a new file, like this:

(sed command) > newfile

You can than do whatever you want with the new file.
 
Old 07-06-2004, 03:23 PM   #6
djgerbavore
Member
 
Registered: Jun 2004
Location: PA
Distribution: Fedora (latest git kernel)
Posts: 458

Original Poster
Rep: Reputation: 30
thanks for the help, however, i want to replace a string with another string, and put it in the same file, i don't want to make a new file. is this possible,? if not, i guess i can make a c program that does this.
 
Old 07-06-2004, 11:34 PM   #7
miss crump
LQ Newbie
 
Registered: May 2004
Location: Oklahoma City, OK
Distribution: Debian
Posts: 17

Rep: Reputation: 0
Can you just not add a section to the command to mv the new file to the old file, thus overwriting the old file with the new one? It would be a lot quicker than writing a program.

sed -e 's/^#export\ install_path\ =\ \/boot/new_string/' Makefiel > newfile; mv newfile oldfile
 
Old 07-07-2004, 08:11 AM   #8
djgerbavore
Member
 
Registered: Jun 2004
Location: PA
Distribution: Fedora (latest git kernel)
Posts: 458

Original Poster
Rep: Reputation: 30
that works for some case, but lets say i have a thousand lines of code, and i want to find a particular line, and change it without opening a the file. Will sed do it? or do i need to use grep and then use sed?
 
  


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
grep question djgerbavore Linux - Newbie 3 11-04-2004 11:53 AM
Question about Grep irfanhab Linux - Newbie 6 08-23-2004 03:57 PM
grep question Hockeyfan Linux - General 1 05-15-2004 03:09 AM
grep question vasanthraghavan Programming 3 04-23-2004 01:32 AM
grep question keyops Programming 2 04-09-2004 10:17 PM


All times are GMT -5. The time now is 12:04 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