How to replace a word with new line
Solution 1
Use this:
sed 's/|END|/\n/g' test.txt
What you attempted doesn't work because sed uses basic regular expressions, and your sed implementation has a \| operator meaning “or” (a common extension to BRE), so what you wrote replaces (empty string or END or empty string) by a newline.
Solution 2
The following worked fine for me:
$ sed 's/|END|/\
/g' foobar
T|somthing|something
T|something2|something2
Notice that I just put a backslash followed by the enter key.
Solution 3
You can use awk:
$ awk -F'\\|END\\|' '{$1=$1}1' OFS='\n' file
T|somthing|something
T|something2|something2
-
-F'\\|END\\|'set field separator to|END| -
OFS='\n'set ouput field separator to newline -
$1=$1causeawkreconstruct$0withOFSas field separator -
1is a true value, causeawkprint the whole input line
Solution 4
Another possibly awk command and using its RS option would be:
awk '$1=$1' RS="\|END\|" file
Will print those records (based on awk's Record Separator) which are not empty( has at least one field) to prevent printing empty lines.
Tested on this input:
T|somthing|something|END|T|something2|something2|END|
Test|END|
|END|
Gives this output:
T|somthing|something
T|something2|something2
Test
That cleared all empty lines :) If you want to have newlines too, replace $1=$1 with $0 in command:
awk '$0' RS="\|END\|" file
Solution 5
Another way with sed that doesn't print empty lines:
sed 's/|END|/\
/g;/^$/!P;D' infile
e.g. input:
T|one|two|END|T|three|four|END|
T|five|six|END|T|seven|eight|END|
T|nine|ten|END|T|eleven|twelve|END|
output:
T|one|two
T|three|four
T|five|six
T|seven|eight
T|nine|ten
T|eleven|twelve
same thing with ed:
ed -s infile <<'IN'
1,$j
s/|END|/\
/g
,p
q
IN
Related videos on Youtube
Sas
I am a new grad software engineering student with big dreams. I consider myself an infant in programming, that does not mean I won't grow up. I enjoy every aspect of programming (coding, troubleshooting, bug fix, system design, etc). Quote: "Typing is no substitute for thinking." — Dartmouth Basic manual, 1964.
Updated on September 18, 2022Comments
-
Sas 3 monthsI have a text file with following data and each row ends with
|END|.T|somthing|something|END|T|something2|something2|END|I am tryig to replace
|END|with\nnew line with sed.sed 's/\|END\|/\n/g' test.txtBut it's producing wrong output like below:
T | s o m e ...But what I want is this:
T|somthing|something T|something2|something2I also tried with
tr. It didn't work either.-
Gilles 'SO- stop being evil' over 7 years
-
-
Stéphane Chazelas over 7 yearsThat's the standard syntax. Using\nas in @A.B.'s answer wouldn't work with somesedimplementations. -
RJS over 7 yearsNeed to comment out the \ in \n: sed 's/|END|/\\n/g
-
Stéphane Chazelas over 7 years$1=$1condenses sequences of blanks into one space character and returns false if the first field is 0. Doesn't make sense. You probably wantawk 1 RS='\\|END\\|'orawk NF RS='\\|END\\|'orawk length RS='\\|END\\|'here. Note that a regexp RS requires gawk or mawk -
Gilles 'SO- stop being evil' over 7 years@Baazigar No, what A.B. wrote is correct (for Linux at least, some sed implementations would emit\n). The question asks how to replace|END|by a newline, not by\n. -
Gilles 'SO- stop being evil' over 7 years@StéphaneChazelas What sed implementation supports\|for alternation in a regexp but not\nmeaning newline in ansreplacement? -
RJS over 7 yearsThe characters for newline are '\n'. The \\n is needed because \ also is an escape character, so if you do only \n, you are saying 'escape this n character'. When you do \\n you are saying 'don't treat this next \ as an escape.'.
-
Arlene Mariano about 3 yearsWorking on FreeBSD v10. Actually, only method that worked for me. Thanks you.
-
psmith 11 monthsWorked on macos, while the other answers did not work.