How to add blank line to text file using shell script?

this is my code, the test.txt file contains more empty lines and empty space separately from the tab, I want to delete this empty space and empty lines, but I need empty lines before running st^ , how to do this?

 sed "s/^[ ]*//" -i test.txt cat $2 > /tmp/tt.txt sed '/^$/d' test.txt > /tmp/tt.txt echo " " >> test.txt echo " " >> /tmp/tt.txt mv /tmp/tt.txt test.txt 

iam gets output like

 st^flower p^rose p^jasmine st^animals p^bear p^elephant 

I need a conclusion like

 st^flower p^rose p^jasmine st^animals p^bear p^elephant 
+4
source share
3 answers

I read your comment, "it gives each linear space, but I want only up to st ^". So you can insert a new line before st ^ with this code:

 $cat /tmp/tt.txt | sed 's/^st\^/\n\0/g' 

For instance:

 $ echo ' st^flower p^rose p^jasmine st^animals p^bear p^elephant' | sed 's/^st\^/\n\0/g' 

Works great for me.

0
source

To output an empty string, use this:

 echo -en '\n' 

e - interprets \n

n - does not print an empty string to the end, so there is no confusion with double empty strings

Use single quotes to prevent interpretation by interpreting characters at the end of a line.

+14
source
 echo -e "\n" 

With double quotes it should work

+1
source

Source: https://habr.com/ru/post/1499742/


All Articles