Sed linux add line

Adding new line to file with sed

I want to add a new line to the top of a data file with sed, and write something to that line. I tried this as suggested in How to add a blank line before the first line in a text file with awk : sed ‘1i\ \’ ./filename.txt but it printed a backslash at the beginning of the first line of the file instead of creating a new line. The terminal also throws an error if I try to put it all on the same line («1i\»: extra characters after \ at the end of i command). Input :

14 1 2 3 4 1 2 3 4 1 2 3 4 

The awk works; thank you. Any idea why it doesn’t work with sed ? My input is a text file with three numbers on each line, single spaced. I am trying to write a number on an empty new top line. The number instead is appended to the front of a number on the new line.

2 Answers 2

$ sed '1i\14' file 14 1 2 3 4 1 2 3 4 1 2 3 4 

but just use awk for clarity, simplicity, extensibility, robustness, portability, and every other desirable attribute of software:

$ awk 'NR==1 ' file 14 1 2 3 4 1 2 3 4 1 2 3 4 

In this case I would suggest to use sed . What’s the benefit of using awk ? I mean you are editing a stream..

like I said, and I don’t say it glibly — clarity, simplicity, extensibility, robustness, portability, and every other desirable attribute of software. There are many tools that can edit streams, awk included. You couldn’t easily enhance/reuse the sed command to work for multiple lines or add conditions or do anything else but you could the awk command. I’m not even sure the sed command would work in all seds. sed is the best choice for simple substitutions on individual lines but, unlike when sed was invented in the early 1970s to be ed for streams, it’s no longer the best for anything else.

I disagree here. The most robust solution for multiple lines, especially if the content to be concatenated is dynamic, is to use cat , that’s why I’ve added that. Otherwise sed is perfect and meant for that purpose. There is a wide spectrum of tasks perfectly fitted for awk , I’m a big fan, but we don’t need to use awk for everything it can do.

Let me admit that awk ‘1’ file1 file2 is as robust as cat file1 file2 . But hey!, would you replace the cat command for that reason?

I’m well aware gawk has the feature, see compgroups.net/comp.lang.awk/awk-i/2115144 for why it has it ;-). Whatever distros don’t have gawk 4.3 are missing a lot of useful functionality — they’ll catch up eventually.

Источник

Sed Command in Linux – Append and Insert Lines to a File

Before we directly jump to the main content, every learner should know what sed is. Here is the brief introduction of the Super sed :

  • sed stand for Stream EDitor and it being based on the ed editor, it borrows most of the commands from the ed . It was developed by Lee E. McMahon of Bell Labs.
  • sed offers large range of text transformations that include printing lines, deleting lines, editing line in-place, search and replace, appending and inserting lines, etc.
  • sed is useful whenever you need to perform common editing operations on multiple lines without using ‘vi’ editor.
  • Whenever sed is executed on an input file or on the contents from stdin, sed reads the file line-by-line and after removing the trailing newline, places it in the “Pattern space”, where the commands are executed on them after conditions (as in case of regex matching) are verified, and then printed on the stdout.
Читайте также:  Где хранятся ppd linux

Before we start, just remember two points:

  1. sed “a” command lets us append lines to a file, based on the line number or regex provided. So, the lines will be added to the file AFTER the line where condition matches.
  2. sed “i” command lets us insert lines in a file, based on the line number or regex provided. So, the lines will be added to the file AT the location where line number matches or BEFORE the line where pattern matches.
  3. sed with option -i will edit the file in place, i.e. unless you use the option -i , the changes will not be written to the file. (Explained in later section)

sed – Appending Lines to a File

For our better understanding, let us have a file sedtest.txt with contents as follows:

$ cat sedtest.txt This is line #1 This is line #2 This is line #3 This is line #4 This is line #5 This is line #6 This is line #7 This is line #8 This is line #9 This is line #10 

1. Append a line after ‘N’th line

This will add a line after ‘N’th line in the FILE.txt .

Example:
To append a line #This is just a commented line after 1st line,

$ sed '1 a #This is just a commented line' sedtest.txt This is line #1 #This is just a commented line This is line #2 This is line #3 This is line #4 This is line #5 This is line #6 This is line #7 This is line #8 This is line #9 This is line #10

While, to append a line after last line,

$ sed '$ a This is the last line' sedtest.txt This is line #1 This is line #2 This is line #3 This is line #4 This is line #5 This is line #6 This is line #7 This is line #8 This is line #9 This is line #10 This is the last line 

If you run above commands and inspect the file sedtest.txt , you would find that, the original contents of that file would not change. In case you wish to append lines in the file and save the changes (i.e. edit the file in place), you will have to use the option -i .

Lets check it for the latest command we have run to append lines after the last line of the file. Has it made any changes to the file?

$ cat sedtest.txt This is line #1 This is line #2 This is line #3 This is line #4 This is line #5 This is line #6 This is line #7 This is line #8 This is line #9 This is line #10 

No, the original file remains the same. But, I wanted to save the changes to the file. So, I should have used the option -i .

$ sed -i '$ a This is the last line' sedtest.txt $ cat sedtest.txt This is line #1 This is line #2 This is line #3 This is line #4 This is line #5 This is line #6 This is line #7 This is line #8 This is line #9 This is line #10 This is the last line 

Yes, now changes are written to the file. Just remember this.

Читайте также:  Не видит монитор linux

2. Append Line using Regular Expression/Pattern

This will append the line after the line where pattern match is found.

$ sed '/5/ a #Next line is the 6th line, not this' sedtest.txt This is line #1 This is line #2 This is line #3 This is line #4 This is line #5 #Next line is the 6th line, not this This is line #6 This is line #7 This is line #8 This is line #9 This is line #10 

sed – Inserting Lines in a File

1. Insert line using the Line number

This will insert the line before the line at line number ‘N’.

$ sed '4 i #This is the extra line' sedtest.txt This is line #1 This is line #2 This is line #3 #This is the extra line This is line #4 This is line #5 This is line #6 This is line #7 This is line #8 This is line #9 This is line #10 

While, to insert a line before last line,

$ sed '$ i #Next line will be last line' sedtest.txt This is line #1 This is line #2 This is line #3 This is line #4 This is line #5 This is line #6 This is line #7 This is line #8 This is line #9 #Next line will be last line This is line #10 

2. Insert lines using Regular expression

This will insert the line before every line where pattern match is found.

$ sed '/8/ i #This line is inserted using sed' sedtest.txt This is line #1 This is line #2 This is line #3 This is line #5 This is line #6 This is line #7 #This line is inserted using sed This is line #8 This is line #9 This is line #10 

That’s all about the second article on sed command. More articles on sed are coming soon. So, stay tuned. Of course, do not forget to share your feedback in the comment section below.

Источник

Insert line after match using sed

For some reason I can’t seem to find a straightforward answer to this and I’m on a bit of a time crunch at the moment. How would I go about inserting a choice line of text after the first line matching a specific string using the sed command. I have .

CLIENTSCRIPT="foo" CLIENTFILE="bar" 
CLIENTSCRIPT="foo" CLIENTSCRIPT2="hello" CLIENTFILE="bar" 

If one needs capture groups to be used in the inserted line, check this question stackoverflow.com/q/39103787/520567

8 Answers 8

Try doing this using GNU sed:

sed '/CLIENTSCRIPT="foo"/a CLIENTSCRIPT2="hello"' file 

if you want to substitute in-place, use

sed -i '/CLIENTSCRIPT="foo"/a CLIENTSCRIPT2="hello"' file 

Output

CLIENTSCRIPT="foo" CLIENTSCRIPT2="hello" CLIENTFILE="bar" 

Doc

Note that both assume GNU sed . That’s not standard sed syntax and won’t work with any other sed implementation.

I had trouble getting this to work for me because I was using a different delimiter. After RTFM’ing, I realized I had to start the regex with a \, then the delimiter.

How does it apply ONLY to the first match? it is clear that it append text after the match, but how does it know only to the first match?

Note the standard sed syntax (as in POSIX, so supported by all conforming sed implementations around (GNU, OS/X, BSD, Solaris. )):

sed '/CLIENTSCRIPT=/a\ CLIENTSCRIPT2="hello"' file 
sed -e '/CLIENTSCRIPT=/a\' -e 'CLIENTSCRIPT2="hello"' file 

( -e xpressions (and the contents of -f iles) are joined with newlines to make up the sed script sed interprets).

Читайте также:  Linux как удалить устройства

The -i option for in-place editing is also a GNU extension, some other implementations (like FreeBSD’s) support -i » for that.

Alternatively, for portability, you can use perl instead:

perl -pi -e '$_ .= qq(CLIENTSCRIPT2="hello"\n) if /CLIENTSCRIPT=/' file 

Or you could use ed or ex :

printf '%s\n' /CLIENTSCRIPT=/a 'CLIENTSCRIPT2="hello"' . w q | ex -s file 

I may be wrong, but the current sed -e ‘/CLIENTSCRIPT=/a\’ -e ‘CLIENTSCRIPT2=»hello»‘ file escapes the quote at the end of the first parameter and breaks the command.

@AbandonedCart, in shells of the Bourne, csh or rc family, ‘. ‘ are strong quotes inside which backslash is not special. The only exception that I know is the fish shell.

Terminal on MacOS (and subsequently a script run in terminal) is also an exception, apparently. I found alternate syntax, but thanks anyway.

@AbandonedCart, that’s something else. That’s macOS sed not being POSIX compliant here. That makes my statement about it being portable incorrect (for the one line variant). I’ll ask the opengroup for confirmation if it’s indeed a non-conformance or a misinterpretation of the standard on my part.

I had so many problems with different versions of sed that I tried the printf. ex version. This works like charm!

Sed command that works on MacOS (at least, OS 10) and Unix alike (ie. doesn’t require gnu sed like Gilles’ (currently accepted) one does):

sed -e '/CLIENTSCRIPT="foo"/a\'$'\n''CLIENTSCRIPT2="hello"' file 

This works in bash and maybe other shells too that know the $’\n’ evaluation quote style. Everything can be on one line and work in older/POSIX sed commands. If there might be multiple lines matching the CLIENTSCRIPT=»foo» (or your equivalent) and you wish to only add the extra line the first time, you can rework it as follows:

sed -e '/^ *CLIENTSCRIPT="foo"/b ins' -e b -e ':ins' -e 'a\'$'\n''CLIENTSCRIPT2="hello"' -e ': done' -e 'n;b done' file 

(this creates a loop after the line insertion code that just cycles through the rest of the file, never getting back to the first sed command again).

You might notice I added a ‘^ *’ to the matching pattern in case that line shows up in a comment, say, or is indented. Its not 100% perfect but covers some other situations likely to be common. Adjust as required.

These two solutions also get round the problem (for the generic solution to adding a line) that if your new inserted line contains unescaped backslashes or ampersands they will be interpreted by sed and likely not come out the same, just like the \n is — eg. \0 would be the first line matched. Especially handy if you’re adding a line that comes from a variable where you’d otherwise have to escape everything first using $ before, or another sed statement etc.

This solution is a little less messy in scripts (that quoting and \n is not easy to read though), when you don’t want to put the replacement text for the a command at the start of a line if say, in a function with indented lines. I’ve taken advantage that $’\n’ is evaluated to a newline by the shell, its not in regular ‘\n’ single-quoted values.

Its getting long enough though that I think perl/even awk might win due to being more readable.

Источник

Оцените статью
Adblock
detector