2014-06-17 16 views
5
#!/bin/bash 

# Let's say now, we are working in my $HOME directory 

# Content of testfile (originally) 
# 123456 
# ABCDEF 
# /home/superman 

string="ABCDEF" 
myfile="$HOME/testfile" 

# test-1, this is okay 
sed -i "/$string/d" $myfile 
echo $string >> $myfile 

# test-2, this fails 
# ERROR (sed: -e expression #1, char 4: extra characters after command) 
sed -i "/$PWD/d" $myfile 
echo $PWD >> $myfile 

# Not working either 
sed -i ":$PWD:d" $myfile 
echo $PWD >> $myfile 

La mia domanda: come gestire la situazione $ PWD?errore sed lamentando "caratteri extra dopo il comando"

risposta

6

Per utilizzare i delimitatori alternativi per gli indirizzi, è necessario utilizzare backslash - \

sed "\:$PWD:d" < $myfile 

dovrebbe funzionare.

Ovviamente per questo esatto esempio, grep -v è probabilmente più semplice.

+1

L'utilizzo di '\:' è molto meglio. +1 – anubhava

Problemi correlati