sed question

Hi, :slight_smile:

I tried this to append a line with sed, e.g:

cat -n "$CONFIG_FILE" |sed -n '7,100p'

    read -p "Please choice a line :" INPUT_LINE
    read -p "Please enter the path of file or directory that you want exclude :" INPUT_PATH_OPTS

    sed ''$INPUT_LINE's/$/ - OPTIONS: --exclude='$INPUT_PATH_OPTS'/' "$CONFIG_FILE"

Output :

./backup.sh
     7  IP CLIENT: 10.10.1.22 - PATH: /var/www
     8  IP CLIENT: 10.10.1.23 - PATH: /var/www
Please choice a line :7
Please enter the path of file or directory that you want exclude :/var/www/index.html
sed: -e expression #1, char 29: unknown option to `s'

I understand that the problem is the path.. but how to do?

" != '

Sorry but I don't understand ?

Your problem is that the regex separators / occur in the replacement text as well - char 29 is the "v" from /var/www...
Try different regex separators (e.g. # )

sed ''$INPUT_LINE's#$# - OPTIONS: --exclude='$INPUT_PATH_OPTS'#' "$CONFIG_FILE"
IP CLIENT: 10.10.1.21 - PATH: /var/www
IP CLIENT: 10.10.1.22 - PATH: /var/www
IP CLIENT: 10.10.1.23 - PATH: /var/www
IP CLIENT: 10.10.1.24 - PATH: /var/www
IP CLIENT: 10.10.1.25 - PATH: /var/www
IP CLIENT: 10.10.1.26 - PATH: /var/www
IP CLIENT: 10.10.1.27 - PATH: /var/www - OPTIONS: --exclude=/var/www/index.html
IP CLIENT: 10.10.1.28 - PATH: /var/www
.
.
.
1 Like

The / separator in sed clashes with the / characters in the variable.
Use another separator! Also put "quotes" around each $variable.

sed ''"$INPUT_LINE"'s#$# - OPTIONS: --exclude='"$INPUT_PATH_OPTS"'#' "$CONFIG_FILE"
1 Like

Not connected to your problem but:

This is the classic "useless use of cat":

sed -n '7,100p' "$CONFIG_FILE"

will do the same.

I hope this helps.

bakunin

@bakunin: not in this case, in some cat versions the -n option prints line numbers.

1 Like

Thanks all !:b: