简体   繁体   中英

Add text after end line between N lines in a file with SED

I have this issue trying to add this text 'cd directory2/' to a file between lines 1 to 15. What I tried so far is:

sed -ni '1,15p s/$/\ cd\ available\//g' myfile

But I'm having this error sed: -e expression #1, char 7: extra characters after command . In order to explain a little better the command:

  • -ni —> Are the options of sed
  • 1,15p —> Specified the lines from 1 to 15
  • s/$/\ cd\ available//g' —> The 's' is for substitution, the '$' for search the end of a line, next I have my pattern to add and finally the 'g' option is for substitute without asking.

The files looks like this:

mget -p directory1/file1.csv
mget -p directory1/file2.csv
mget -p directory1/file3.csv
mget -p directory1/file4.txt
mget -p directory4/fileab.csv
mget -p directory4/fileabc.txt

And the expected output is:

mget -p directory1/file1.csv cd directory2/
mget -p directory1/file2.csv cd directory2/
mget -p directory1/file3.csv cd directory2/
mget -p directory1/file4.txt cd directory2/
mget -p directory4/fileab.csv
mget -p directory4/fileabc.txt

It's important to clarify that myfile has more content. So after the modification all the text that are in line 16 and more has to exist and not dissapear.

This might work for you (GNU sed):

sed -i '1,15s/$/ cd directory2\//' file

In the range 1 to 15, append cd directory2/ only.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM