Visit Our UNIX and Linux User Community


sed multiline problem


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting sed multiline problem
# 1  
Old 06-25-2018
sed multiline problem

I'm trying to replicate the sed output on p.108 of Sed&Awk,by Doughery & Robbins, 2nd edition.

I'm on a Windows 10 Surface Pro, running Cygwin for 64-bit versions of Windows.

Input text saved in text file called data_p108.txt:

Code:
Consult Section 3.1 in the Owner and Operator
Guide for a description of the tape drives
available on your system.

sed script saved in file called multiline_scr:

Code:
/Operator$/{
N
s/Owner and Operator\nGuide /Installation Guide\
/
}

command I ran:

Code:
sed -f multiline_scr data_p108.txt

output:

Code:
Consult Section 3.1 in the Owner and Operator
Guide for a description of the tape drives
available on your system.

I was expecting the script to produce the output on p.108:

Code:
Consult Section 3.1 in the Installation Guide
for a description of the tape drives
available on your system.

I'm using the Atom editor which shows a carriage return and line feed
characters at the end of each line in the script and input data.

Any ideas why the script seems to just echo back the input?

I can get sed to work on single-line input but not on multi-line input as in
the above.
# 2  
Old 06-25-2018
Without digging deeper: try \r\n (in lieu of \n alone) in the substitute command.
# 3  
Old 06-25-2018
sed multiline problem - reply using \r

interesting suggestion, to use \r\n (carriage return & new line) instead of just \n in the command

Code:
/Operator$/{
N
s/Owner and Operator\r\nGuide /Installation Guide\
/
}

unfortunately, I got the same results where sed just echoes back the input:

Code:
ZR6O@W-018974644353 /cygdrive/c/users/zr6o/Documents/sed_and_awk
$ sed -f multiline_scr data_p108.txt
Consult Section 3.1 in the Owner and Operator
Guide for a description of the tape drives
available on your system.

# 4  
Old 06-26-2018
Quote:
Originally Posted by prooney
interesting suggestion, to use \r\n (carriage return & new line) instead of just \n in the command
The reason is that Unix- and DOS-systems (and their descendants) have different line-ending sequences: UNIX always had a single-character (newline) as line-separator whereas DOS had no real print-processor program. Therefore its inventors made "carriage-return"+"line-feed" the line separator, so that the printer (we are talking typewriter-like printers here) could use that sequence directly.

Quote:
Originally Posted by prooney
unfortunately, I got the same results where sed just echoes back the input:
Maybe you have some (unwanted) whitespace at the end of the line which makes your regexp not matching the text? You could analyse your input data with the od command: use od -ax /your/file to get a hex dump.

I hope this helps.

bakunin
# 5  
Old 06-26-2018
Thank you for the suggestion to check spaces (hex 20) at the end of the lines. The command
Code:
od -ax data_p108.txt

yields
Code:
0000000   C   o   n   s   u   l   t  sp   S   e   c   t   i   o   n  sp
           6f43    736e    6c75    2074    6553    7463    6f69    206e
0000020   3   .   1  sp   i   n  sp   t   h   e  sp   O   w   n   e   r
           2e33    2031    6e69    7420    6568    4f20    6e77    7265
0000040  sp   a   n   d  sp   O   p   e   r   a   t   o   r  cr  nl   G
           6120    646e    4f20    6570    6172    6f74    0d72    470a
0000060   u   i   d   e  sp   f   o   r  sp   a  sp   d   e   s   c   r
           6975    6564    6620    726f    6120    6420    7365    7263
0000100   i   p   t   i   o   n  sp   o   f  sp   t   h   e  sp   t   a
           7069    6974    6e6f    6f20    2066    6874    2065    6174
0000120   p   e  sp   d   r   i   v   e   s  cr  nl   a   v   a   i   l
           6570    6420    6972    6576    0d73    610a    6176    6c69
0000140   a   b   l   e  sp   o   n  sp   y   o   u   r  sp   s   y   s
           6261    656c    6f20    206e    6f79    7275    7320    7379
0000160   t   e   m   .  cr  nl
           6574    2e6d    0a0d
0000166

I don't see any spaces at the end of any of the three lines.

I also tried the single line sed command with the multi-line flag m:
Code:
sed 'N; s/Owner and Operator\nGuide/Installation Guide/m' data_p108.txt

but it still just echoed back the input file:
Code:
Consult Section 3.1 in the Owner and Operator
Guide for a description of the tape drives
available on your system.

# 6  
Old 06-26-2018
As you can see, you get
cr lf
so
/Operator$/
never match
# 7  
Old 06-26-2018
As mentioned above, your MS Windows style text file is full of carriage returns:

Code:
0000040  sp   a   n   d  sp   O   p   e   r   a   t   o   r  cr  nl   G
           6120    646e    4f20    6570    6172    6f74    0d72    470a

Which are not spaces. You may be able to match them with \r or \x0d
This User Gave Thanks to Corona688 For This Post:

Previous Thread | Next Thread
Test Your Knowledge in Computers #870
Difficulty: Medium
Lisp introduced the concept of automatic garbage collection.
True or False?

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

[sed]: Substitute a string with a multiline value

Dear all, I try to replace a string of characters in a file (MyFile.txt) by a multiline value of the variable "Myvar": $ cat MyFile.txt DESCRIPTION '@TargetTable SCHEMA' ( @InputFlowDef ); $ The content of Myvar: $ echo "$Myvar" col1 , col2 , col3 $ (4 Replies)
Discussion started by: dae
4 Replies

2. Shell Programming and Scripting

Multiline sed

Hi guys, I am fairly comfortable with using the sed command if the string to be replaced is all on a single line. I was wondering is it possible to use sed command in a multiline way ? Say for example I have the below string on 2 different lines: { "key": "brandNameA", ... (3 Replies)
Discussion started by: Junaid Subhani
3 Replies

3. Shell Programming and Scripting

sed to extract a multiline subject from a mail message

I'm trying to extract a subject from a mail message but my subject example has 2 lines. How can I manage to extract it and write a string at the end of it? Consider this example: From: test@domain.com Subject: Re: U =?ISO-8859-1?Q?qu=EA=3F!=3F!=3F!!_wtff_=E7=E3o_=ED=F3?= ... (6 Replies)
Discussion started by: twisterbr
6 Replies

4. UNIX for Dummies Questions & Answers

Need Multiline sed help!!

Hey everyone, I'm new to sed and I need to create a script for inserting one line of code at the beginning of every method in a Xcode project (over 6,000 methods). Each method Structure is (+ or -) (Various declarations-- could span multiple lines) ({) I've tried for days, any guidance would be... (2 Replies)
Discussion started by: jimmyz
2 Replies

5. UNIX for Dummies Questions & Answers

sed multiline pattern match

How can I write a script that takes a cisco config file and outputs every occurrence of two, or more, pattern matches through the whole config file? For example, out of a config file, i want to print out every line with interface, description and ip address through the whole file, and disregard... (3 Replies)
Discussion started by: knownasthatguy
3 Replies

6. Shell Programming and Scripting

sed multiline substitution if a condition holds

Hi. I have the following text file (more precisely a Matlab script): dummy1 = 5; varx = 'false'; dummy2 = 6; % ... some general commands % ... dummy3 = 7; vary = 'option1'; dummy4 = 8; Using sed I want to do the following action: IF varx = 'false' vary='NEW_VALUE_1'... (11 Replies)
Discussion started by: plsrn
11 Replies

7. Shell Programming and Scripting

Multiline pattern search using sed or awk

Hi friends, Could you please help me to resolve the below issue. Input file :- <Node> <username>abc</username> <password>ABC</password> <Node> <Node> <username>xyz</username> <password>XYZ</password> <Node> <Node> <username>mnp</username> ... (3 Replies)
Discussion started by: haiksuresh
3 Replies

8. Shell Programming and Scripting

delete multiline string from file using sed.

Hi, I have file which has the following content... GOOD MORNING **********WARNING********** when it kicks from the kickstart, sshd daemon should start at last. (WHEN KICKING ITSELF, NOT AFTER KICKING). /etc/rc3.d/S55sshd ( run level specification for sshd is 55, now I would want to... (4 Replies)
Discussion started by: skmdu
4 Replies

9. Shell Programming and Scripting

Multiline replace problem

I have a data of the form 0.0117843924 0. 0. 0. 0. 0.011036017 0. 0. 0. 0. 0.0103351669 0. 0. 0. 0. 4839.41211 0. 0. 0. 0. 4532.08203 0. 0. 0. 0. I would like to insert a couple of blank lines before the 4839 line, every time it appears. The numbers in the... (2 Replies)
Discussion started by: mathis
2 Replies

Featured Tech Videos