ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
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.
You need the '-e' option for every sed command including the first one. Also there isn't an '-a' option in my sed.
For long sed programs, it's best to create a sed script. Especially if you need to use branching, which you will if a pattern can stretch across two or more lines.
I think that you are mixing in the syntax for the find command. The -e option allows you to enter more than one sed command. For example, suppose that you want to change every occurance of the word 'furquan' to 'Furquan' in a document, and also remove all blank lines.
sed -i -e 's/furquan/Furquan/g' -e '/^$/d' document
This is the same as having a sed scripts "changes.sed" with the contents:
s/furquan/Furquan/g
/^$/d
You would call this script like: sed -f changes.sed document
For some types of editing with sed, you have to use branches, and so you will need a sed script. Using a single
sed command with -e is best for simple changes.
I'll give you an example. Suppose that you use k3b to back up files in /home/username/Documents/ and you want to get rid of them. If you unzip the .k3b file you saved there will be a "mimetype" file and a "maindata.xml" file.
Examining the contents, you notice that the full pathname for each file is included in this document, but that the lines look like:
<url>/home/username/Documents/aDocument.pdf</url>
So you would like to A) remove all lines that don't match this pattern and B) remove the <url> and </url> part. So only 2 sed commands are necessary and you can put them in the same sed command and pipe the output to xargs.
The first command deletes all lines that don't start with '<url>'. The second command removes the <url> and </url> tags. The <url> and <\/url> parts in the search command serve as anchors, while the \(.*\) is a wild card that saves the contents in the middle. The \1 recalls the pattern found in the replace section of the sed substitute 's' command. The tr command replaces new-lines with null characters to make it easier to handle filenames that contain spaces.
The -e option is also used by the grep command. Suppose that you use the "locate" command but several screens are displayed. You can remove lines by piping the output through "grep":
locate pattern | grep -v -e pattern1 -e pattern2 -e pattern3
This way, you can press the up arrow, and keep adding patterns until you have the response pruned to how you want it.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.