Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What does this mean in Linux sed '$a\' a.txt

Tags:

linux

shell

sed

I have this line in my Linux shell script

sed '$a\' < file_a.txt

Afraid to remove it from the code and cannot find out what it is for.

like image 621
Piyaput Boonpume Avatar asked Dec 27 '16 10:12

Piyaput Boonpume


People also ask

What does sed '/ $/ D do?

It means that sed will read the next line and start processing it. nothing will be printed other than the blank lines, three times each. Show activity on this post. The command d is only applied to empty lines here: '/^$/d;p;p'.

What does sed means in Linux?

The SED command in Linux stands for Stream EDitor and is helpful for a myriad of frequently needed operations in text files and streams. Sed helps in operations like selecting the text, substituting text, modifying an original file, adding lines to text, or deleting lines from the text.

What is sed i in Linux with example?

Though most common use of SED command in UNIX is for substitution or for find and replace. By using SED you can edit files even without opening them, which is much quicker way to find and replace something in file, than first opening that file in VI Editor and then changing it. SED is a powerful text stream editor.

What is a sed script?

To use sed, you write a script that contains a series of editing actions and then you run the script on an input file. Sed allows you to take what would be a hands-on procedure in an editor such as vi and transform it into a look-no-hands procedure that is executed from a script.


1 Answers

It makes sure the output will end with a newline; see:

echo -ne test | sed '$a\'
# same output as:
echo test | sed '$a\'

As you can see with the previous code, a carriage return isn't added in the second example but one is added in the first example. Of course, if you remove the sed part, the output will be different since the first echo statement has no carriage return.

like image 153
Thomas Baruchel Avatar answered Oct 05 '22 16:10

Thomas Baruchel