Search code examples
sedquotesmsys2

Inserting text with sed at start of line gives error


I am trying to modify a file with sed but I get an error I do not understand:

sed: -e expression #1, char 0: no previous regular expression

Command:

sed -e 's/^/"asset\//' example.txt

Example file:

joe
john
peter

sed is from msys2:

which sed

sed is an external : C:\msys64\usr\bin\sed.exe

When adding text to the end of each line the following works as expected:

sed -e 's/$/",/' y.txt

joe",
john",
peter",

I am at a lost to understand what is wronghere.


Solution

  • EDIT(solution on cygwin): Try following on cygwin sed, just tested on a system and it worked fine from command prompt. While using ' single quotes with sed I am getting same error what OP is getting but while using " in sed I am able to get correct behavior of sed(expected one, to add " before each line)

    sed -E "s/^/\"/"  Input_file
    


    EDIT2(taken reference from tripleee's comments): You could also save your sed command into a file named file with command s/^/\"/ then run the sed code by doing, tested it successfully with cygwin sed.

    sed -f file Input_file
    


    Alternate solution(tested and works on cygwin): I don't have cygwin with me so couldn't test on it, though your mentioned solution works fine for BASH. Could you please try following another approach once? I just tested this solution too on cygwin and it worked fine there (EDIT comment).

    sed 's/\(.*\)/"\1/' Input_file