Replace string including newline


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Replace string including newline
# 1  
Old 11-05-2015
Replace string including newline

Hi,

I am trying to do some transformation on a large file and I am getting some troubles trying remove newlines only when the last character of a line is a symbol (in this case is a pipe "|").

I have tried with sed like this:

Code:
sed -i 's/|\n/|/g' my_file

or

Code:
sed -i 's/|$/|/gg' my_file


but as sed is a line operation command does not find newlines. I have also tried:

Code:
sed -i ':a;N;$!ba;s/|\n//g' my_file

This works for small files, but not for my "large" file (actually is only 4Gb).

I could do it using vi and %:s/|\n/|/g' but I need to be able to execute inside a script (also is a slow solution).

Any help is appreciatted.
# 2  
Old 11-05-2015
Hello ngb,

AFSIK only GNU sed will take that \n substitution. Could you please try following and let me know if this helps.
Code:
 awk '{gsub("\n"/,X,$0);print}' Input_file > tmp_Input_file
 mv tmp_Input_file Input_file

You could first test awk command if that works successfully then you can try mv command to change tmp_Input_file to Input_file or if you want to change to a different file then you can change it accordingly. Also remove carriage characters if any in your file by doing tr -d '\r' < Input_file > Output_file.


Thanks,
R. Singh
# 3  
Old 11-05-2015
Thank you RavinderSingh13 for your help.

I am getting this error:

awk: {gsub("\n"/,X,$0);print}
awk: ^ syntax error
awk: fatal: 0 is invalid as number of arguments for gsub


The error is pointing to the comma between / and X.


Quote:
Originally Posted by RavinderSingh13
Hello ngb,

AFSIK only GNU sed will take that \n substitution. Could you please try following and let me know if this helps.
Code:
 awk '{gsub("\n"/,X,$0);print}' Input_file > tmp_Input_file
 mv tmp_Input_file Input_file

You could first test awk command if that works successfully then you can try mv command to change tmp_Input_file to Input_file or if you want to change to a different file then you can change it accordingly. Also remove carriage characters if any in your file by doing tr -d '\r' < Input_file > Output_file.


Thanks,
R. Singh
# 4  
Old 11-05-2015
TrY:
Code:
sed -e :a -e '/|$/{$!N;s/\n//;};ta' file

With GNU sed you could also try
Code:
sed ':a;/|$/{$!N;s/\n//;};ta' file


The difference is that this works on a line-by-line basis, whereas the last example in post #1 first reads the entire file before it attempts to globally substitute...


--
Quote:
Originally Posted by RavinderSingh13
Hello ngb,
AFSIK only GNU sed will take that \n substitution. [..]
Any POSIX compliant sed understands \n in the regex part of the s-function if it is not used within a square bracket expression.

Last edited by Scrutinizer; 11-05-2015 at 06:36 AM..
This User Gave Thanks to Scrutinizer For This Post:
# 5  
Old 11-05-2015
Thank you Scrutinizer!!! That worked for the large file too. I will check further later but your command helped a lot.



Quote:
Originally Posted by Scrutinizer
TrY:
Code:
sed -e :a -e '/|$/{$!N;s/\n//;};ta' file

With GNU sed you could also try
Code:
sed ':a;/|$/{$!N;s/\n//;};ta' file


The difference is that this works on a line-by-line basis, whereas the last example in post #1 first reads the entire file before it attempts to globally substitute...


--

Any POSIX compliant sed understands \n in the regex part of the s-function if it is not used within a square bracket expression.
# 6  
Old 11-30-2015
I got a request to explain the sed statement in post #4:

Code:
-e :a        # define label a
-e '/|$/{    # if the last character is a vertical bar, then
$!N          # if it is not the last line then append the next line              
s/\n//;}     # and replace a newline character. 
ta           # if the last replacement was successful, then go to label a

The loop is necessary, so that it also works for multiple consecutive lines that end with a vertical bar..
These 2 Users Gave Thanks to Scrutinizer For This Post:
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Replace newline in a string

I have a string like below: {\rtf1\fbidis\ansi\deff0{\fonttbl{\f0\fnil\fcharset0 MS Sans Serif;}{\f1\fnil MS Sans Serif;}} \viewkind4\uc1\pard\ltrpar\lang2057\f0\fs16 19/11/2010 SOME DESCRIPTION. \par \lang1033\f1\par } I have to replace the newline character with null in the... (8 Replies)
Discussion started by: Pratik4891
8 Replies

2. Shell Programming and Scripting

Replace String With Newline

Hi, I'm struggling with a string replacement. I have an XML file which is in the following layout <FUNCTION> <PRODUCTS> <PRODUCT CODE="PRODUCE" ACTION="amend" VALIDATE="no"> <SUPPLIER PRODUCT="PRODUCT" ACTION="amend" CODE="SUPPLIER"> <STOCK_QUANTITY DATA="21"/> ... (15 Replies)
Discussion started by: Ste_Moore01
15 Replies

3. Shell Programming and Scripting

Grep a string from input file and delete next three lines including the line contains string in xml

Hi, 1_strings file contains $ cat 1_strings /home/$USER/Src /home/Valid /home/Review$ cat myxml <projected value="some string" path="/home/$USER/Src"> <input 1/> <estimate value/> <somestring/> </projected> <few more lines > <projected value="some string" path="/home/$USER/check">... (4 Replies)
Discussion started by: greet_sed
4 Replies

4. Shell Programming and Scripting

search string in a file and retrieve 10 lines including string line

Hi Guys, I am trying to write a perl script to search a string "Name" in the file "FILE" and also want to create a new file and push the searched string Name line along with 10 lines following the same. can anyone of you please let me know how to go about it ? (8 Replies)
Discussion started by: sukrish
8 Replies

5. Shell Programming and Scripting

replace >< with > newline <

Hi All, I have the command in PERL for performing this, but Can you please suggest me how can i perform this using AWK: My input xml file looks like this: <aaa>hello</aaa><bbb>hai</bbb> I want the output like this ( means need new line after end of each xml tag): <aaa>hello</aaa>... (1 Reply)
Discussion started by: HemaV
1 Replies

6. Shell Programming and Scripting

Help with sed matching <tag1> newline spaces <tag2> and replace the value in the same string format

Hi, I'm very new to shell scripting and have searched google and this forum for quite some time now. I have the following in my xml file: <recipients> <member>value1</member> </recipients> I need to find a string <recipients> that follows with a new-line and bunch of spaces and... (5 Replies)
Discussion started by: mgharios
5 Replies

7. UNIX for Dummies Questions & Answers

replace text string with a newline

I want to replace a text string with a newline. I have a long text file of random characters. I want to replace all the occurences of "pe" with a newline. How can I do that in Unix? There's a thread from 2004 saying that you can do something like this with sed by actually pressing the return... (1 Reply)
Discussion started by: aaronpoley
1 Replies

8. Shell Programming and Scripting

Replace a string with newline

Hi all I have the problem to substitute a string with newline in Perl. Can anybody help me? And also how to replace a string with opening bracket (e.g. (START ) with a whitespace/null character? Thanks in advance. (1 Reply)
Discussion started by: my_Perl
1 Replies

9. UNIX for Dummies Questions & Answers

replace string with a newline string

Hi, I wanted to replace these lines in vi editor: input-- uid=ESVPEME | eriMasterDomain=EAMCS | eriCountry=El | ou=ESV uid=EPYCAR | eriMasterDomain=EAMCS | eriCountry=Argentina | ou=CEA uid=ERCFGA | eriMasterDomain=EAMCS | eriCountry=Costa | ou=ERC uid=EDGLUCU | eriMasterDomain=EAMCS... (5 Replies)
Discussion started by: hegdeshashi
5 Replies

10. Shell Programming and Scripting

replace a newline (\n)

dear all: maybe i have a file like : 12 34 56 78 end how do write can i replace newline into NA : make the file inte : 12 NA 34 NA 56 78 END (3 Replies)
Discussion started by: jeter
3 Replies
Login or Register to Ask a Question