sed command works with e flag but not with E

Prvt_Yadav

I have a string and I need to add #, in the beginning,i.e. convert [ -n "$ID" -a "$ID" -le 200 ] && return to #[ -n "$ID" -a "$ID" -le 200 ] && return. I can use below command:

echo '[ -n "$ID" -a "$ID" -le 200 ] && return'| sed -n -e 's/\[ -n "$ID" -a "$ID" -le 200 \] && return/#&/p'

It works. Now I have two questions, regarding E, flag.

  1. For the string [ -n "$ID" -a "$ID" -le 200 ], if I escape brackets, it does not work; however, it works when I do not escape them i.e.

    sed -n -E 's/[ -n "$ID" -a "$ID" -le 200 ]/#&/p'
    

    works while

     sed -n -E 's/\[ -n "$ID" -a "$ID" -le 200 \]/#&/p'
    

    doesn't work.

  2. For, the full string [ -n "$ID" -a "$ID" -le 200 ] && return, it gives me wrong answer, when I do not escape them:

    echo '[ -n "$ID" -a "$ID" -le 200 ] && return'| sed -n -E 's/[ -n "$ID" -a "$ID" -le 200 ] && return/#&/p'
    

    It gives me output:

     [ -n "$ID" -a "$ID" -le 200 #] && return
    

I want to know how it is working.

Philippos
sed -n -E 's/[ -n "$ID" -a "$ID" -le 200 ]/#&/p'

Does not work the way you expect it to work, it's more of a coincidence: [...] is a collection of characters that match. Inside that collection you have a range -n from whitespace (0x20) to n (0x6E), which can include [ (0x5B), depending on your locale settings. So the collection matches the first char. See what happens when you take #&___ as the replacement... not your intention, I guess?

And that's the reason for your full case to fail: The matching character for the collection is the ] because it is followed by the rest of the pattern, that's why the # is inserted there.

The problem with the extended regular expression is like @steeldriver assumed: The $ anchors the pattern:

A ( '$' ) outside a bracket expression shall anchor the expression or subexpression it ends to the end of a string; such an expression or subexpression can match only a sequence ending at the last character of a string. For example, the EREs "ef$" and "(ef$)" match "ef" in the string "abcdef", but fail to match in the string "cdefab", and the ERE "e$f" is valid, but can never match because the 'f' prevents the expression "e$" from matching ending at the last character.

So in ERE, a literal $ needs to be escaped, while in BRE it only needs to be escaped when being the last character of the pattern.

Also note that -e marks the next argument as a script and is optional if there is only one script, while -E is a switch. Dropping -e for '-E` only works if there is only one script because it was superfluous anyhow.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

sed s command & e flag: `echo '$-' | sed 's/-//e'` gives: `sh: $: command not found`

Sed directory not found when running R with -e flag

sed throws 'bad flag in substitute command' but works fine in package.json

How to combine several option in sed -e command?

Linux sed -i -e command include / in replacement

Need to parse the following sed command: sed -e 's/ /\'$'\n/g'

Using variables in a sed command - sed -e exception: unknown option to `s'

sed command: bad flag in substitute command: '-'

Unknown shorthand flag: 'E' in -E

How does this sed command works?

Getting bad flag in substitute command '/' for sed replacement

Sed command failing with bad flag in script

Sed error: bad flag in substitute command: 'U'

How does sed -e '/./{H;$!d;}' -e 'x;/AAA/!d;' works to print paragraph

sed: -e expression #1, char XX: unterminated `s' command

sed: -e expression #1, char 37: unterminated `s' command

sed: -e expression #1, char 5: unterminated `s' command

bash sed: -e expression #1, char 7: unterminated `s' command

bash - sed: -e expression #1, char 15: unterminated `s' command

sed: -e expression #1, char 35: unterminated `s' command

sed: -e expression #1, char 10: missing command

sed: -e expression #1, char 10: extra characters after command

sed: -e expression #1, char 4: unknown command:

sed command creating unwanted duplicates of file with -e extension

what does the "e" modifier to the s/// command mean in GNU sed?

sed: -e expression #1, char 2: extra characters after command

sed: -e expression #1, char 1: unknown command: '|'

sed: -e expression #1, char 101: unterminated `s' command

sed.exe -e expression #1, unknown command

TOP Ranking

  1. 1

    Failed to listen on localhost:8000 (reason: Cannot assign requested address)

  2. 2

    Loopback Error: connect ECONNREFUSED 127.0.0.1:3306 (MAMP)

  3. 3

    How to import an asset in swift using Bundle.main.path() in a react-native native module

  4. 4

    pump.io port in URL

  5. 5

    Compiler error CS0246 (type or namespace not found) on using Ninject in ASP.NET vNext

  6. 6

    BigQuery - concatenate ignoring NULL

  7. 7

    ngClass error (Can't bind ngClass since it isn't a known property of div) in Angular 11.0.3

  8. 8

    ggplotly no applicable method for 'plotly_build' applied to an object of class "NULL" if statements

  9. 9

    Spring Boot JPA PostgreSQL Web App - Internal Authentication Error

  10. 10

    How to remove the extra space from right in a webview?

  11. 11

    java.lang.NullPointerException: Cannot read the array length because "<local3>" is null

  12. 12

    Jquery different data trapped from direct mousedown event and simulation via $(this).trigger('mousedown');

  13. 13

    flutter: dropdown item programmatically unselect problem

  14. 14

    How to use merge windows unallocated space into Ubuntu using GParted?

  15. 15

    Change dd-mm-yyyy date format of dataframe date column to yyyy-mm-dd

  16. 16

    Nuget add packages gives access denied errors

  17. 17

    Svchost high CPU from Microsoft.BingWeather app errors

  18. 18

    Can't pre-populate phone number and message body in SMS link on iPhones when SMS app is not running in the background

  19. 19

    12.04.3--- Dconf Editor won't show com>canonical>unity option

  20. 20

    Any way to remove trailing whitespace *FOR EDITED* lines in Eclipse [for Java]?

  21. 21

    maven-jaxb2-plugin cannot generate classes due to two declarations cause a collision in ObjectFactory class

HotTag

Archive