[SOLVED] Need help with sed to modify only lines of text meeting certain criteria
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.
I am trying to remove the trailing 3 sets of spaces and numbers (e.g., " 872 1 872") such that the first one would be renamed >MCAL_43C14_r_00 and the second one would be renamed >MCAL_52K01_f_00. I know sed is at least in part the tool for the job but I'm stuck. I must not have the search pattern formatted correctly but I can't figure out what is wrong with it. Also, how do I ask sed to leave the characters I like untouched?
sed -n 's/>MCAL_[0-9][0-9][A-Z][0-9][0-9]_[fr]_[0-9][0-9] [0-9][0-9][0-9] 1 [0-9][0-9][0-9]/>MCAL_[0-9][0-9][A-Z][0-9][0-9]_[fr]_[0-9][0-9]/g' Mytilus_californianus.txt
Several things; the sed command you have there won't work; you have to supply the "-e" parameter just before the regular expression. Also, your sed command will just print out the changes, but not actually commit them. You will need to supply the "-i" parameter ("edit in-place") and definitely with a filename suffix for the original. Your sed command should look something like this:
Code:
sed -n -i.orig -e 's/>MCAL_[0-9][0-9][A-Z][0-9][0-9]_[fr]_[0-9][0-9] [0-9][0-9][0-9] 1 [0-9][0-9][0-9]/>MCAL_[0-9][0-9][A-Z][0-9][0-9]_[fr]_[0-9][0-9]/g' Mytilus_californianus.txt
Also, you might also want to look into awk. I would highly suggest looking to see what awk will do when you pass it your text file as an argument and see how it splits your file up into fields. Also check out cut.
There's a couple of comments in this thread about the -e flag in SED. Note that this is required only when using multiple commands within the same sed invocation.
There's a couple of comments in this thread about the -e flag in SED. Note that this is required only when using multiple commands within the same sed invocation.
Forrestt, can you explain what the pattern in the search term of the sed command you gave means? I realize that it translates to "everything after the space" but I don't understand how.
The s/// means substitute. The "." means match any character. The "+" means one or more times, but it must be escaped so that is isn't read as an actual "+" sign. You could have also used a "*" instead of the "+". It means zero or more times.
So, you get, substitute a space followed by any character one or more times and replace it with an empty string. This removes the chars after the first space.
If you are confused by the escape (\), you can also turn on extended regular expressions using the -r flag.
Thus, this works:
Code:
sed -e -r 's/ .+//'
With the -r flag, a literal "+" would then require the escape.
So, I guess there really is no way to escape the escape....... NO--WAIT: There is:
If you escape an escape, then it's not an escape......e.g. "\\" means a literal "\"
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.