Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

sed error complaining "extra characters after command" [duplicate]

Tags:

bash

shell

sed

#!/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

My question: How to handle the $PWD situation?

like image 800
Daniel Avatar asked Jun 17 '14 20:06

Daniel


People also ask

What does sed '/ $/ D do?

env | sed '/^#/ d' | sed '/^$/ d' Concatenate FILE(s), or standard input, to standard output. With no FILE, or when FILE is -, read standard input.

What is P option in sed?

In sed, p prints the addressed line(s), while P prints only the first part (up to a newline character \n ) of the addressed line. If you have only one line in the buffer, p and P are the same thing, but logically p should be used.

What is G flag in sed?

unix is a powerful. Replacing all the occurrence of the pattern in a line : The substitute flag /g (global replacement) specifies the sed command to replace all the occurrences of the string in the line.


1 Answers

To use the alternate delimiters for addresses, you need to use backslash - \

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

Should work.

Of course for this exact example, grep -v is probably easier.

like image 124
evil otto Avatar answered Sep 28 '22 06:09

evil otto