LinuxQuestions.org
Review your favorite Linux distribution.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - General
User Name
Password
Linux - General This Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.

Notices


Reply
  Search this Thread
Old 10-10-2009, 12:16 AM   #1
manwithaplan
Member
 
Registered: Nov 2008
Location: ~/
Distribution: Arch || Sidux
Posts: 393

Rep: Reputation: 45
Using sed to remove text in fgrep string


I would like to use sed to remove text between "( )" in a string pattern... Question is after reading the man pages and looking at examples, I am still slightly confused on how to implement.

Here is a example of what I wanna do:


Code:
  
cd $pkg_edit
    
      for edt in acl attr cloop gcc; 
      do
        cd $edt
        cat PKGBUILD | fgrep depends | sed ..? # This is where I need an example
        cd ..
      done
An example of the fgrep string:
Code:
$  cat PKGBUILD | fgrep depends
depends=('attr>=2.4.41')
I need to remove the text in between "( )"

I could remove the entire line in the file... But I am keeping the variable, just in case of a manual edit. I am not completely helpless ... So, if you show me how i can match a pattern with sed in the string, then remove then text...
 
Old 10-10-2009, 12:42 AM   #2
rikijpn
Member
 
Registered: Jun 2007
Location: Japan
Distribution: Debian lenny, DSL, Solaris 10
Posts: 157

Rep: Reputation: 33
sed

So, according to your example, you want this:
Code:
depends=('attr>=2.4.41')
to be printed like this:
Code:
depends=()
right?

Then this should do it:
Code:
 'sed s/(.*)/()/g'
This tells sed to substitute every character including "(" till ")" with a "()". You don't really need the /g part, but in case you put this same command with sed on a file (not piping) you should use it.
 
Old 10-10-2009, 01:37 AM   #3
manwithaplan
Member
 
Registered: Nov 2008
Location: ~/
Distribution: Arch || Sidux
Posts: 393

Original Poster
Rep: Reputation: 45
Quote:
Originally Posted by rikijpn View Post
So, according to your example, you want this:
Code:
depends=('attr>=2.4.41')
to be printed like this:
Code:
depends=()
right?

Then this should do it:
Code:
 'sed s/(.*)/()/g'
This tells sed to substitute every character including "(" till ")" with a "()". You don't really need the /g part, but in case you put this same command with sed on a file (not piping) you should use it.
Yes that's right, I want to remove the text between "( )".

Thanks for the example... Though it seems the sed isn't matching my fgrep.

So I done away w/ the cat and fgrep... This sed seemed to work with the example you gave me.

After reading the man page. The -i extension was needed to edit the file in place.

Code:
sed -i 's/depends=(.*)/depends=()/g' PKGBUILD >/dev/null
I was having trouble with sed replacing all of my "( )" with empty strings, so with your help on the /s regexp, I was able to fix it.

Though still don't understand the "/g" Here is what the man page says:
Quote:
g G Copy/append hold space to pattern space.
I assume it holds the appended text in the same line..?


Thanks for the help.
 
Old 10-10-2009, 02:04 AM   #4
lutusp
Member
 
Registered: Sep 2009
Distribution: Fedora
Posts: 835

Rep: Reputation: 102Reputation: 102
Quote:
Originally Posted by manwithaplan View Post
I would like to use sed to remove text between "( )" in a string pattern... Question is after reading the man pages and looking at examples, I am still slightly confused on how to implement.

Here is a example of what I wanna do:


Code:
  
cd $pkg_edit
    
      for edt in acl attr cloop gcc; 
      do
        cd $edt
        cat PKGBUILD | fgrep depends | sed ..? # This is where I need an example
        cd ..
      done
An example of the fgrep string:
Code:
$  cat PKGBUILD | fgrep depends
depends=('attr>=2.4.41')
I need to remove the text in between "( )"

I could remove the entire line in the file... But I am keeping the variable, just in case of a manual edit. I am not completely helpless ... So, if you show me how i can match a pattern with sed in the string, then remove then text...
The solutions in this thread are way too complicated. Have you considered the easy way?:

Code:
$ x="depends=('attr>=2.4.41')"
$ echo "${x//\'*\'}"
depends=()
 
Old 10-10-2009, 02:09 AM   #5
manwithaplan
Member
 
Registered: Nov 2008
Location: ~/
Distribution: Arch || Sidux
Posts: 393

Original Poster
Rep: Reputation: 45
Quote:
Originally Posted by lutusp View Post
The solutions in this thread are way too complicated. Have you considered the easy way?:

Code:
$ x="depends=('attr>=2.4.41')"
$ echo "${x//\'*\'}"
depends=()
Yes... the thing is in your example is, that the variable $x is static. I need to run this in a for loop, that goes through multiple directories to append that one line on the same filename.

Code:
 cd $abs_bd 
   
	    for pkg in acl attr fakeroot pacman libfetch libarchive openssl wget rsync;  
	    do
	     cd $pkg 
		sed -i 's/depends=(.*)/depends=()/g' PKGBUILD >/dev/null  # Removes all dependencies from the build
		makepkg -s $pkg || pkgfail  # Rebuild function
	     cd ..
	    done

Last edited by manwithaplan; 10-10-2009 at 02:10 AM.
 
Old 10-10-2009, 02:25 AM   #6
lutusp
Member
 
Registered: Sep 2009
Distribution: Fedora
Posts: 835

Rep: Reputation: 102Reputation: 102
Quote:
Originally Posted by manwithaplan View Post
Yes... the thing is in your example is, that the variable $x is static. I need to run this in a for loop, that goes through multiple directories to append that one line on the same filename.
So read the file line by line, which you are already doing, and use the simple string method as a filter. Put this inside your present file scanning loop:

Code:
while read line
do
    echo "${line//\'*\'}"
done < filename > tempfile

mv tempfile filename
Be sure to create a temp file as shown above -- don't try to read and write to the same file at the same time. Do this regardless of which method you end up using -- it is vital that you not try to read and write to the same file at once.
 
Old 10-11-2009, 01:00 AM   #7
rikijpn
Member
 
Registered: Jun 2007
Location: Japan
Distribution: Debian lenny, DSL, Solaris 10
Posts: 157

Rep: Reputation: 33
sed options

Quote:
Originally Posted by manwithaplan View Post
Yes that's right, I want to remove the text between "( )".

Thanks for the example... Though it seems the sed isn't matching my fgrep.

So I done away w/ the cat and fgrep... This sed seemed to work with the example you gave me.

After reading the man page. The -i extension was needed to edit the file in place.

Code:
sed -i 's/depends=(.*)/depends=()/g' PKGBUILD >/dev/null
I was having trouble with sed replacing all of my "( )" with empty strings, so with your help on the /s regexp, I was able to fix it.

Though still don't understand the "/g" Here is what the man page says:
I assume it holds the appended text in the same line..?


Thanks for the help.
Well, good thing you got it. You sure can edit the a file with the shell or sed, although sed is probably just shorter and easier to remember on most times.

The /g options is for edit the file globally, meaning replacement in all lines. Otherwise it would replace only the first string match it finds, and then end the program (ignoring the rest of the file). If you would like to read more about sed probably this page is the best you can find: http://www.grymoire.com/Unix/Sed.html#uh-6 (link to the part about the /g option).
 
  


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 On
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
SED - remove last four characters from string 3saul Linux - Software 10 12-04-2018 12:24 PM
Remove everything up to the last numbers of a string w/ sed or awk OutThere Linux - General 4 04-23-2009 07:01 PM
Trying to use sed to remove last line if it contains a certain string. slaxative Linux - Software 1 03-18-2008 02:13 AM
help with sed to remove all text except for some Benanzo Linux - Software 7 01-04-2007 06:21 AM
Remove string in sed twantrd Programming 7 09-13-2006 02:28 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - General

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