[SOLVED] print only changed line with sed after double substition
Linux - NewbieThis 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
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.
That's good to know, but that's not exactly what I wanted to it to display. I wanted it to display ALL altered lines only ONCE. The problem is that with my initial solution, even though it displays lines that contain either 'up' or 'down', lines that contain both 'up' and 'down' are displayed twice (which is quite an obvious behaviour - first it changes up to UP, the it displays the line as it is, and then that line is in turn altered, by changing down to DOWN, so that's the second time). So your solution only solves this part of the problem, but not the whole problem through one command.
I wanted it to display ALL altered lines only ONCE.
Then the rules seem tohave been changed...
Quote:
Originally Posted by vincix
in this case I'm trying to print only the lines that were altered by both substitions. The two substitions are 's/down/DOWN/' and 's/up/UP/'.
...which will be only the one line.
If you wnat to also show lines which only include one of the words, simply remove the address...
Code:
$ sed -rn 's/up|down/\U&/gp' ud.txt
He marched them UP to the top of the hill
And he marched them DOWN again
And when they were UP they were UP
And when they were DOWN they were DOWN
And when they were only half-way UP
They were neither UP nor DOWN
I don't see any difference in meaning between the two quotes, but ok. Come to think about it, though, I understand now why the initial post might have been ambiguous - because you're thinking that I'm talking about both substitutions on the same line, but I was referring on the file as a whole. Anyway, now we're talking about the same thing. It's much easier than I initially thought.
I have to admit that even though I use sed daily, I did not see this case until reading syg00's hint. I think that was the pleasant surprise they mentioned.
Using sed is a lot like learning to ride a bicycle, skill improves only with number of attempts!
If your question is sufficiently answered then please mark the thread as solved.
Good luck!
Last edited by astrogeek; 03-04-2017 at 03:14 PM.
Reason: Bad 'a' key
@MadeInGermany I did see your previous solution - you understood it the same way astrogeek did (which was linguistically correct, even if it wasn't my intention exactly)-, but I wanted to go through each step by myself as much as possible, that's why I tried not to take it into consideration until I reached a later stage.
After I finish (as it were) with sed, I'll go on to learn some awk, too. I had already started it, but I combined it with some bash and so on, and so forth.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.