Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to add empty line in text file using shell script? [closed]

Tags:

bash

This is my code; the "test.txt" file contains more empty lines and empty space apart from tab. I want to remove that empty space and empty lines as well but I need one empty line before starting st^. How to do it?

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

I am getting output like:

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

I want output like:

    st^flower
     p^rose
     p^jasmine

    st^animals 
     p^bear
     p^elephant
like image 714
user2653831 Avatar asked Aug 30 '13 07:08

user2653831


People also ask

How do you add a line to a text file in shell script?

This appending task can be done by using 'echo' and 'tee' commands. Using '>>' with 'echo' command appends a line to a file. Another way is to use 'echo,' pipe(|), and 'tee' commands to add content to a file.

How do you add a line break in shell script?

The most used newline character If you don't want to use echo repeatedly to create new lines in your shell script, then you can use the \n character. The \n is a newline character for Unix-based systems; it helps to push the commands that come after it onto a new line.

How do I add a new line to a text file in Linux?

Alternatively, you can use the printf command (do not forget to use \n character to add the next line). You can also use the cat command to concatenate text from one or more files and append it to another file.

How do I insert a blank line in Unix?

The G sed command appends a newline followed by the content of the hold space (here empty as we don't put anything in it) to the pattern space. So it's a quick way to add an empty line below that matched line.


2 Answers

To output empty line use this:

echo -en '\n'

e - interprets \n

n - outputs no empty line on the end, so there is no mess with double empty lines

Use single quotes to prevent shell from interpreting chars as end of line.

like image 65
Grzegorz Żur Avatar answered Oct 03 '22 09:10

Grzegorz Żur


echo -e "\n"

With double quotes, it must work

like image 43
sr01853 Avatar answered Oct 03 '22 07:10

sr01853