Issue
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.
Solution
This might work for you (GNU sed):
sed -i '1,15s/$/ cd directory2\//' file
In the range 1 to 15, append cd directory2/
only.
Answered By - potong Answer Checked By - Robin (WPSolving Admin)