sed print flag


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting sed print flag
# 1  
Old 12-29-2015
sed print flag

I have an input file that looks something like this:

Code:
....
key1: ABC
....
key2: DEF
....
key1: GGG
....
key2: HHH
....

The row of dots represents any number of lines that don't contain the strings "key1:" or "key2:" The strings key1: and key2: will always appear alternately as in the above example.

The sed command:

Code:
sed -n -e 's_key1: \(...\)_\1_p' -e 's_key2: \(...\)\1_p'

produces this output:

Code:
ABC
DEF
GGG
HHH

But what I really want is this output:

Code:
ABC DEF
GGG HHH

At one point I thought there was a upper case P flag that would print without
the newline character. That would do it, but at least with my GNU sed version 4.2.1 I don't see such a flag.

Are there any other simple ways to modify my sed command to produce the desired output?

Last edited by Scrutinizer; 12-29-2015 at 09:28 AM.. Reason: code tags
# 2  
Old 12-29-2015
Hello pmennen,

Welcome to forums, please use code tags for commands/codes/Inputs which you are using in your posts as per forum rules. You could try following an awk solution.
Code:
awk '/key1/{A=A?A OFS $2:$2} /key2/{B=B?B OFS $2:$2} END{print A ORS B}'  Input_file

Output will be as follows.
Code:
ABC GGG
DEF HHH

Off course it will work only for keywords key1 and key2 as per your shown input, if you have more conditions please do let us know.

Thanks,
R. Singh
# 3  
Old 12-29-2015
Thanks R. Singh (and sorry for forgetting the code tags).

Yes, I did simplify the problem somewhat for brevity, so I will probably have to learn at least some of that arcane looking awk syntax. (I realize that sed is just as arcane, its just that I am already familiar with it).

It seems like this may be simple enough a sed solution might be possible without resorting to overly cryptic methods. If anyone knows of such a solution, I would still be interested in seeing it.

Thanks
~Paul
# 4  
Old 12-29-2015
GNU sed might handle a newline character like this
Code:
sed -n '
s_key1: \(...\)\n_\1_p
s_key2: \(...\)_\1_p
'

(Untested.)
# 5  
Old 12-29-2015
This at least works in GNU sed

Code:
mute@tiny:~$ sed -n '/key1:/{s/key1: //;h};/^key2:/{H;x;s/\nkey2://p}' input
ABC DEF
GGG HHH

Place it in hold space. Append key2 to hold space, swap hold/pattern space, then remove new line and key2:. Might need to fold spaces.
These 2 Users Gave Thanks to neutronscott For This Post:
# 6  
Old 12-29-2015
Another option:
Code:
sed -n 's/key[12]: //p' file | paste -d" " - -


--
( or sed -n 's/key[12]: \(...\)/\1/p' file | paste -d" " - - if you will )


---
Quote:
Originally Posted by MadeInGermany
GNU sed might handle a newline character like this
Code:
sed -n '
s_key1: \(...\)\n_\1_p
s_key2: \(...\)_\1_p
'

(Untested.)
That will not work since even in GNU sed, the trailing newline will be chopped off before the line gets processed, so the substitute command will never "see" it..
These 3 Users Gave Thanks to Scrutinizer For This Post:
# 7  
Old 12-29-2015
My actual input file was a bit more complicated, and was similar to this:

Code:
....
??? key1: word1, word2, word3, word4
....
????? key2: word5, word6, word7
....
?????? key1: word8, word9
....
??????? key2: word10, word11
....


And the desired output is:

Code:
word1 word5
word8 word10

Before the reply from neutronscott came in, I managed to produce the desired output by piping it to a separate sed command to remove the unwanted newlines (which is somewhat similar to the solution suggested by Scrutinizer using paste):

Code:
sed -n -r -e "s_.* key1: ([^,]*).*_\1_p" -e "s_.*key2: ([^,]*).*_\1_p" input.txt | sed "N;s/\n/ /"

Using the method suggested by neutronscott, I'm now producing the same output using:

Code:
sed -n -r "/key1:/{s/.* key1: ([^,]*).*/\1 /;h};/key2:/{H;x;s/\n.* key2: ([^,]*).*/\1/p}" input.txt

It's actually slightly longer, but I prefer a single call to sed when possible.

Thanks everyone for your replies!

~Paul
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Need help for sed replace and print

Hi I am working with sed to get string replace and print all the lines. Cat f1 <text1> tag123 44412c232place1 text456-text= tag12 44412c232place4 jjaa TAG456 44412c232place1066dfdf erer .. i have used this command - sed -n '/tag/ s#place#SomePlace#gp' f1 It gives me... (2 Replies)
Discussion started by: krsnadasa
2 Replies

2. Shell Programming and Scripting

How to print sed results

Dear all, How can I print results (and of course, send this result to the text file) of sed command. I mean, I want to know which lines of which files sed command has found. For e.g, the result text file should contains: file1.c:line 12 file2.h:line 14 file2.h:line 37 Please help me (10 Replies)
Discussion started by: Hannibal2010
10 Replies

3. Shell Programming and Scripting

How to print this using sed?

Hi Imagine that I have a text file containing the following student's data: student: john group: A sex: male age: 25 student: alice sex: female age: 20 group: B It is guarantee the details will start by "student:", and then followed by the sex, age and group in any order. Is it... (2 Replies)
Discussion started by: hezjing
2 Replies

4. Shell Programming and Scripting

How to Toggle Flag/Switch Value with Sed

I am trying to figure out a one liner to toggle a flag variable. eg. FLAG=0 Is there a way to use sed to toggle above example between 0 and 1. That is if run with flag set to zero it would change it to one if run again it would set it to zero. I thought I had it figured but the... (6 Replies)
Discussion started by: bsquared
6 Replies

5. UNIX for Dummies Questions & Answers

Sed | Awk | Print

Hello, ı want to started shell programing. But first, I want to learn ssh code. How can I use this codes ( sed - awk - print ). How do codes work? Thanks.. Best Wishes.. (1 Reply)
Discussion started by: Nullsix
1 Replies

6. Shell Programming and Scripting

sed - print only matching regex

Hi folks, Lets say I have the following text file: name, lastname, 1234, name.lastname@test.com name1, lastname1, name2.lastname2@test.com, 2345 name, 3456, lastname, name3.lastname3@test.com 4567, name, lastname, name4.lastname4@test.com I now need the following output: 1234... (5 Replies)
Discussion started by: domi55
5 Replies

7. Shell Programming and Scripting

sed/awk print out

guys... i'm currently trying to parse some html code: <tr class="first" name="bc_10934363" class=""><td class="col1"><div class="ctitle">Some text</div><div class="crec" name="pc_10934363"></div><div class="ctext">Some text</div></td><td class="col2"><div class="ctext">SF zwei</div></td><td... (6 Replies)
Discussion started by: domi55
6 Replies

8. Shell Programming and Scripting

Using sed to print from the bottom up?

I have file.txt bob jon jones gary I want to print from the botton, up using sed. gary jones jon bob Whats one command I can use to do this? Or will I have to construct a new file that would hold the original file in reverse and then print the reversed file? (3 Replies)
Discussion started by: Bandit390
3 Replies

9. UNIX for Dummies Questions & Answers

Using sed to print a ruler

How can I use sed to print rulers of x characters in length on my terminal? For example, I want sed to print a 50-character rule. sed '// p' ?? Thanks! (5 Replies)
Discussion started by: doubleminus
5 Replies

10. Shell Programming and Scripting

pattern matching and print with sed

Hi It is possible with sed to print a pattern within a line matching regexp? So, the line looks like : 19:00:00 blablablabla jobid 2345 <2> the regexp is "jobid 2345" and the pattern is 56434. That the code for find... (2 Replies)
Discussion started by: nymus7
2 Replies
Login or Register to Ask a Question