LinuxQuestions.org
Help answer threads with 0 replies.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 10-02-2012, 12:21 AM   #1
monsteriname
Member
 
Registered: Sep 2009
Distribution: maggie 4 and 5
Posts: 35

Rep: Reputation: 0
sed works on multiple lines but doesn't work on a single line


Code:
sed -e ':a;N;$!ba;s/\n/*/g' -e s"%01%NATURE OF COMPLAINT;\\n%" -e s"%*%\\n%g" -e s"%01%%g"`
If I feed this two or more lines it does what I want, namely;

changes the newlines into asterisks,
replaces only the first occurrence of "01" with "NATURE OF COMPLAINT" followed by a newline,
changes the asterisks back into newlines,
strips off the remaining "01"s (I haven't been able to get "/01/d" to work)

but if its fed only one line nothing happens, no substitution or deletion of the only "01"
it was fed.

Any help making this work with a single line (must continue to work with multiple lines also)
is greatly appreciated.




Thanks to crts and others for help in the past. We dropped the approach and by the time I
got back to this thread it was too old to drag up;
http://www.linuxquestions.org/questi...acters-834406/
 
Old 10-02-2012, 02:30 AM   #2
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,007

Rep: Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191
You need to consider what the first part of your sed does. As it is already calling for multiple lines it is making changes that then affect further down the sed
hence a single line is now treated abnormally. Unfortunately I am not up on sedfu enough to know how to fix this.
 
1 members found this post helpful.
Old 10-02-2012, 03:24 AM   #3
monsteriname
Member
 
Registered: Sep 2009
Distribution: maggie 4 and 5
Posts: 35

Original Poster
Rep: Reputation: 0
Thanks grail,
I focused on the first part like you said and think I found a work around at least, It seems to be working. I changed;
Code:
':a;N;$!ba;s/\n/*/g'
to
Code:
';$bw;:a;N;$!ba;:w;s/\n/*/g'
It seems this branches to "w" if there is only one line, but not if there are multiple lines. I'm not sure though, it was more of a guess when I tried it. Hopefully I won't discover that it creates some other problem. I'm off to do more testing.

Thanks again,
 
Old 10-02-2012, 06:54 AM   #4
danielbmartin
Senior Member
 
Registered: Apr 2010
Location: Apex, NC, USA
Distribution: Mint 17.3
Posts: 1,881

Rep: Reputation: 660Reputation: 660Reputation: 660Reputation: 660Reputation: 660Reputation: 660
Quote:
Originally Posted by monsteriname View Post
changes the newlines into asterisks,
replaces only the first occurrence of "01" with "NATURE OF COMPLAINT" followed by a newline,
changes the asterisks back into newlines,
strips off the remaining "01"s
Help us to help you. Your original post gave a description of the desired transformation, and that's good. Please provide a sample input file and the corresponding output file. Having words accompanied by "before" and "after" examples gives a fuller picture.

Daniel B. Martin
 
1 members found this post helpful.
Old 10-03-2012, 02:23 AM   #5
monsteriname
Member
 
Registered: Sep 2009
Distribution: maggie 4 and 5
Posts: 35

Original Poster
Rep: Reputation: 0
Thanks, after more testing it seems to continue doing what I want, but I am going to at least try to combine the two separate variable assignations (with all the sed) into one.

I trimmed this way down, I hope enough to be understandable. It is the main portion of this script file. It only takes input from what I think is a "bash built in" dialogs, I use kdialog. This works from a command line also, I do that sometimes for testing.

If I paste this at a bash prompt and make some choices ;
Code:
response=`kdialog  --geometry 300x400+520+20 --separate-output --checklist " 
Choose \"X out\" to just close this window.   
You may re-open a Noise Complaint through the Auto_log 
menu > \"Re-open_Noise_Complaint\"."  "01Loud Voices" "Loud Voices" off "01Loud Music" "Loud Music" off "01Sounds like furniture scraping on the floor above" "Sounds like furniture scraping on the floor above" off "02Spoke to guest for the second time - made them aware of the possibility of being asked to leave the property if we keep getting complaints" "Spoke to offender in person - 2nd time" off  "02Could not find disturbance" "Could not find disturbance" off "02@" "Type an entry" off`

response=`echo "$response" | sed -e ';$bw;:a;N;$!ba;:w;s/\n/*/g' -e s"%01%$timenow NATURE OF COMPLAINT;\\n%" -e s"%*%\\n%g" -e s"%01%%g"`
response=`echo "$response" | sed -e ';$bw;:a;N;$!ba;:w;s/\n/*/g' -e s"%02%\\n$timenow ACTION TAKEN;\\n%" -e s"%*%\\n%g" -e s"%02%%g"`

echo "$response"
I get something like this, which is what I want;

Code:
 NATURE OF COMPLAINT;
Loud Voices
Loud Music

 ACTION TAKEN;
Spoke to guest for the second time - made them aware of the possibility of being asked to leave the property if we keep getting complaints
Could not find disturbance
I direct the output to two different files, one is a running shift log and the other is an specific incident report. They are both plain text files.

Prior to making the change in the code it would not work if I only made one choice. Now it does. In my script file I do not have the final echoed variable (out to a files) in quotes but I did that so it would come out the same at a command line.
 
  


Reply



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
Split single line into multiple lines with 3 column each udiubu Programming 7 11-26-2017 09:41 AM
Make multiple blank lines to a single lines kimhj3715 Programming 5 06-10-2012 10:35 AM
how to create a single line of output from multiple variable lines of input steven.c.banks Linux - General 2 02-03-2010 03:09 PM
SED how to find multiple patterns on a single line yaazz Programming 9 07-31-2009 04:20 AM
merge multiple lines of a single file into one line groverrajiv Linux - Newbie 4 05-26-2004 02:38 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 06:14 AM.

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