Assuming I have a file BookDB.txt which stores data in the following format :
Harry Potter - The Half Blood Prince:J.K Rowling:40.30:10:50
The little Red Riding Hood:Dan Lin:40.80:20:10
Harry Potter - The Phoniex:J.K Rowling:50.00:30:20
Harry Potter - The Deathly Hollow:Dan Lin:55.00:33:790
Little Prince:The Prince:15.00:188:9
Lord of The Ring:Johnny Dept:56.80:100:38
Three Little Pig:Andrew Lim:89.10:290:189
All About Linux:Ubuntu Team:76.00:44:144
Catch Me If You Can:Mary Ann:23.60:6:2
Python for dummies:Jared Loo:15.99:1:10
I am trying to replace the (:) delimiter with (, ) in my output. It succeeds, but removes the newline from the output. Here is my code :
TITLE=Potter
OUTPUT=$(cat BookDB.txt | grep $TITLE)
OUTPUT1=$(sed 's/:/, /g' <<< $OUTPUT)
echo $OUTPUT1
I want my output to look like this :
Harry Potter - The Half Blood Prince, J.K Rowling, 40.30, 10, 50
Harry Potter - The Phoniex, J.K Rowling. 50.00. 30. 20
Harry Potter - The Deathly Hollow, Dan Lin, 55.00, 33, 790
However, it looks like this :
Harry Potter - The Half Blood Prince, J.K Rowling, 40.30, 10, 50 Harry Potter - The Phoniex, J.K Rowling, 50.00, 30, 20 Harry Potter - The Deathly Holl
ow, Dan Lin, 55.00, 33, 790
If anyone could share how to preserve the line break in the output, I would be very grateful!
The Proper Way to Preserve Linebreaks The right way to use a variable is simple: quote the variable. This is because if we quote a variable, the shell will treat it as one single argument to the command, no matter if the variable contains linebreaks or not.
sed can be used with other CLI tools through pipelines. Since sed can read an input stream, CLI tools can pipe data to it. Similarly, since sed writes to stdout, it can pipe its output to other commands. The sed and awk examples illustrate this.
There are a couple of different ways we can print a newline character. The most common way is to use the echo command. However, the printf command also works fine. Using the backslash character for newline “\n” is the conventional way.
Just use correct quotation :
TITLE=Potter
OUTPUT=$(cat BookDB.txt | grep $TITLE)
OUTPUT1=$(sed 's/:/, /g' <<< "$OUTPUT")
echo "$OUTPUT1"
As \n
is part of the default value of IFS, it is removed without the double quotes.
More info on quoting here
You can avoid cat
and do all in sed:
sed -n '/Potter/s/:/, /gp' file
Harry Potter - The Half Blood Prince, J.K Rowling, 40.30, 10, 50
Harry Potter - The Phoniex, J.K Rowling, 50.00, 30, 20
Harry Potter - The Deathly Hollow, Dan Lin, 55.00, 33, 790
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With