How do I replace a string with a newline using a bash script and sed?

awk replace string with newline
sed replace newline
sed remove newline after pattern
sed remove newline between quotes
sed replace newline with null
replace new lines using sed
sed in bash script
bash replace line in file

I have the following input:

Value1|Value2|Value3|Value4@@ Value5|Value6|Value7|Value8@@ Value9|etc...

In my bash script I would like to replace the @@ with a newline. I have tried various things with sed but I'm not having any luck:

line=$(echo ${x} | sed -e $'s/@@ /\\\n/g')

Ultimately I need to parse this whole input into rows and values. Maybe I am going about it wrong. I was planning to replace the @@ with newlines and then loop through the input with setting IFS='|' to split up the values. If there is a better way please tell me, I am still a beginner with shell scripting.

This will work

sed 's/@@ /\n/g' filename

replaces @@ with new line

How to replace a word with new line, awk -F'\\|END\\|' '{$1=$1}1' OFS='\n' file T|somthing|something Another possibly awk command and using its RS option would be: lines :) If you want to have newlines too, replace $1=$1 with $0 in command: awk As mentioned here by Walter Mundt, we can achieve this using an ANSI C quoted string In my bash script I would like to replace the @@ with a newline. I have tried various things with sed but I'm not having any luck: line=$(echo ${x} | sed -e $'s/@@ /\\ /g') Ultimately I need to parse this whole input into rows and values. Maybe I am going about it wrong.

Using pure BASH string manipulation:

eol=$'\n'
line="${line//@@ /$eol}"

echo "$line"
Value1|Value2|Value3|Value4
Value5|Value6|Value7|Value8
Value9|etc...

How to replace any char with newline char., Hi, How to replace any character in a file with a newline character using sed .. Ex: To replace ',' with newline Input: abcd,efgh,ijkl,mnop Output: ab | The UNIX  Use an ANSI-C quoted string $'string' You need a backslash-escaped literal newline to get to sed. In bash at least, $'' strings will replace with a real newline, but then you have to double the backslash that sed will see to escape the newline, e.g.

I recommend using the tr function

echo "$line" | tr '@@' '\n'

For example:

[itzhaki@local ~]$ X="Value1|Value2|Value3|Value4@@ Value5|Value6|Value7|Value8@@"
[itzhaki@local ~]$ X=`echo "$X" | tr '@@' '\n'`
[itzhaki@local ~]$ echo "$X"
Value1|Value2|Value3|Value4

 Value5|Value6|Value7|Value8

Linux / UNIX: Sed Replace Newline, How do I replace newline (\n) with sed under UNIX / Linux operating systems? You can use the following sed command: sysadmin, DevOps engineer, and a trainer for the Linux operating system/Unix shell scripting. Bash Shell: Replace a String With Another String In All Files Using sed and Perl -pie  Extra backslash when storing grep in a value. linux,bash. The output from set -x uses single quotes. So the outer double quotes were replaced with single quotes but you can't escape single quotes inside a single quoted string so when it then replaced the inner double quotes it needed, instead, to replace them with '\'' which ends the single

Finally got it working with:

sed 's/@@ /'\\\n'/g'

Adding the single quotes around \\n seemed to help for whatever reason

How to replace spaces with newlines/enter in a text-file?, Use perl perl -pe 's/ /\n/g' example. Use the shell foo=$(cat example); echo -e RS (Record seperator) is an built-in awk variable. Each line is stripped of its trailing newline, split into words, and then rejoined into one string  How to use sed to find and replace text in files in Linux / Unix shell last updated January 13, 2018 in Categories BASH Shell, CentOS, Debian / Ubuntu, FreeBSD, KSH Shell, Linux, RedHat and Friends, Suse, UNIX

If you don't mind to use perl:

echo $line | perl -pe 's/@@/\n/g'
Value1|Value2|Value3|Value4
 Value5|Value6|Value7|Value8
 Value9|etc

50 `sed` Command Examples – Linux Hint, Replace one of a list of strings with the new string; Replace a matched string with a string that contains newlines; Remove newlines from file and insert a comma at​  Hi, How to replace any character in a file with a newline character using sed .. Ex: To replace ',' with newline Input: abcd,efgh,ijkl,mnop Output: ab | The UNIX and Linux Forums How to replace any char with newline char.

How can I replace a newline with its escape sequence?, Use: sed ':a;N;$!ba;s/\n/\\n/g'. Which uses the answer from How can I replace a newline (\n) using sed? substituting an escaped newline character as the  SEARCH_REGEX - Normal string or a regular expression to search for. REPLACEMENT - The replacement string. g - Global replacement flag. By default, sed reads the file line by line and changes only the first occurrence of the SEARCH_REGEX on a line. When the replacement flag is provided,

Section 4, (a) If typing the sed script from a Bourne shell, use one backslash "\" if the script uses 'single This will embed a newline into the "replace" portion. If the block of lines are strings that never change their order and if the top line never occurs  The `sed` script can be executed without any option. Content: Basic text substitution using ‘sed’. Replace all instances of a text in a particular line of a file by using ‘g’ option. Replace the second occurrence only of a match on each line. Replace the last occurrence only of a match on each line.

sed, 'D' If pattern space contains newlines, delete text in the pattern space up to the first newline, If found, replace matched string with REPLACEMENT. (1) 'e' This command allows one to pipe input from a shell command into pattern space. Replace strings in files with sed, the bash command Filed under: Server — Tags : bash , sed , shell Sed is a stream editor, meaning you can search and replace strings in files and use regex if needed.

Comments
  • a gnu sed of recentish version should work with ...|sed 's/@@ /\n/g'.. . Good luck.
  • awk with RS="@@ " and FS="|"
  • @rpax thanks for editing forgot to format my answer
  • You are welcome. Anyway, this is a good answer. I like it. +1
  • You don't have to escape the first @, and this is missing a space after @@.
  • Sadly this did not work...it did not replace the "@@ " at all line="${x//@@ /$eol}"
  • @odinsride: Make sure you use BASH. See with working demo: ideone.com/ePzOxq
  • This did not work in my script, it still has the '@@ ' in the output: echo $x | tr '@@ ' '\n' >> $OUTPUT
  • I know for a fact it works. I'm using "tr" all the time. Make sure you're using BASH and not SH, and that you're looking at the right output. Here, run this: pastebin.com/gxvKUwS9
  • As far as I know, tr works on individual characters, not strings. As in, tr 'Ab' 'aB' turns Abracadabra into aBracadaBra.
  • You just need to "squeeze": tr -s '@' '\n'
  • The question asks to replace @@ plus a blank, though. tr can't do that.
  • Edited answer to make solution compatible with performing operations on both rows and fields in those rows.