Linux - GeneralThis 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
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
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
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...
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.
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..?
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
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?:
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.
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.
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).
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.