LinuxQuestions.org
Review your favorite Linux distribution.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This 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


Reply
  Search this Thread
Old 03-04-2017, 02:23 PM   #16
astrogeek
Moderator
 
Registered: Oct 2008
Distribution: Slackware [64]-X.{0|1|2|37|-current} ::12<=X<=15, FreeBSD_12{.0|.1}
Posts: 6,194
Blog Entries: 24

Rep: Reputation: 4159Reputation: 4159Reputation: 4159Reputation: 4159Reputation: 4159Reputation: 4159Reputation: 4159Reputation: 4159Reputation: 4159Reputation: 4159Reputation: 4159

Here is my complete solution, just for... completeness...

Code:
$ sed -rn '/up.*down/s/up|down/\U&/gp' ud.txt
They were neither UP nor DOWN
This is GNU of course, on Slackware, mileage may vary on the mac.
 
1 members found this post helpful.
Old 03-04-2017, 02:28 PM   #17
vincix
Senior Member
 
Registered: Feb 2011
Distribution: Ubuntu, Centos
Posts: 1,240

Original Poster
Rep: Reputation: 103Reputation: 103
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.

Last edited by vincix; 03-04-2017 at 02:29 PM.
 
Old 03-04-2017, 02:36 PM   #18
astrogeek
Moderator
 
Registered: Oct 2008
Distribution: Slackware [64]-X.{0|1|2|37|-current} ::12<=X<=15, FreeBSD_12{.0|.1}
Posts: 6,194
Blog Entries: 24

Rep: Reputation: 4159Reputation: 4159Reputation: 4159Reputation: 4159Reputation: 4159Reputation: 4159Reputation: 4159Reputation: 4159Reputation: 4159Reputation: 4159Reputation: 4159
Quote:
Originally Posted by vincix View Post
I wanted it to display ALL altered lines only ONCE.
Then the rules seem tohave been changed...

Quote:
Originally Posted by vincix View Post
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
 
1 members found this post helpful.
Old 03-04-2017, 02:45 PM   #19
vincix
Senior Member
 
Registered: Feb 2011
Distribution: Ubuntu, Centos
Posts: 1,240

Original Poster
Rep: Reputation: 103Reputation: 103
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.

What is the role of & after U?
 
Old 03-04-2017, 02:49 PM   #20
astrogeek
Moderator
 
Registered: Oct 2008
Distribution: Slackware [64]-X.{0|1|2|37|-current} ::12<=X<=15, FreeBSD_12{.0|.1}
Posts: 6,194
Blog Entries: 24

Rep: Reputation: 4159Reputation: 4159Reputation: 4159Reputation: 4159Reputation: 4159Reputation: 4159Reputation: 4159Reputation: 4159Reputation: 4159Reputation: 4159Reputation: 4159
Perhaps there is a language barrier, but those sentences are quite different to my understanding.

Quote:
Originally Posted by vincix View Post
What is the role of & after U?
Did you not read post #5?
 
Old 03-04-2017, 02:57 PM   #21
vincix
Senior Member
 
Registered: Feb 2011
Distribution: Ubuntu, Centos
Posts: 1,240

Original Poster
Rep: Reputation: 103Reputation: 103
Oh, yes, I know about &. I guess I thought differently about it in this context because of the \U. So \U alters the matched pattern.

Yeah, I guess you're right about the sentence, I realised now after rereading it.
 
Old 03-04-2017, 03:13 PM   #22
astrogeek
Moderator
 
Registered: Oct 2008
Distribution: Slackware [64]-X.{0|1|2|37|-current} ::12<=X<=15, FreeBSD_12{.0|.1}
Posts: 6,194
Blog Entries: 24

Rep: Reputation: 4159Reputation: 4159Reputation: 4159Reputation: 4159Reputation: 4159Reputation: 4159Reputation: 4159Reputation: 4159Reputation: 4159Reputation: 4159Reputation: 4159
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
 
Old 03-06-2017, 04:28 AM   #23
MadeInGermany
Senior Member
 
Registered: Dec 2011
Location: Simplicity
Posts: 2,562

Rep: Reputation: 1113Reputation: 1113Reputation: 1113Reputation: 1113Reputation: 1113Reputation: 1113Reputation: 1113Reputation: 1113Reputation: 1113
With the OR condition it is nearly impossible with a non-GNU sed.
With awk
Code:
awk '{ p1=sub(/up/,"UP"); p2=sub(/down/,"DOWN") } (p1 || p2)' file.txt
 
1 members found this post helpful.
Old 03-06-2017, 05:54 AM   #24
vincix
Senior Member
 
Registered: Feb 2011
Distribution: Ubuntu, Centos
Posts: 1,240

Original Poster
Rep: Reputation: 103Reputation: 103
@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.
 
  


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



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] Sed print only the line that exists two lines after the match netpumber Linux - Newbie 5 12-03-2014 06:46 PM
[SOLVED] Modifying text/file names and recording changed line with sed longlostjon26 Linux - Newbie 1 06-07-2014 09:49 PM
Sed: Not working as expected. Search out one line, print next line sumncguy Programming 2 10-02-2013 03:21 PM
[SOLVED] how do i add a line with double quotes in sed? Beandip408 Linux - Newbie 1 02-01-2012 03:22 PM
[SOLVED] Sed - print line number along with the line ? ntpntp Linux - Newbie 6 01-30-2011 05:58 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

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