2

I'm running this sed command:

sed -e 's/\/[[:word:]]+//g' *.csv

and in *.csv files I have something like:

name/name, val1, val2
name2/name2, val3, val4
etc

I want delete the one name and / so to obtain name, val1, val2.... But I obtain this error:

sed: -e expression #1, char 18: Invalid character class name

I'm using Ubuntu 16.04

3 Answers 3

5

sed is supporting POSIX regular expressions. POSIX simply doesn't define a character class [:word:] and so doesn't sed.

You can use the following command:

sed 's~/[[:alnum:]]\{1,\}~~' file

It is using the character class [:alnum:] which matches letters and numbers. The + quantifier you were using doesn't exist in POSIX regular expressions. You need to use \{1,\} to express one or more.

2

There is no character class named [:word:], hence the error message regarding invalid character class name.

Presumably, you are trying to remove /name portion from input, if so do:

sed -E 's_/[^,]*__' file.csv

Here we have used _ as the sed's s (substitution) operator delimiter.

Example:

% sed -E 's_/[^,]*__' <<<'name/name, val1, val2'
name, val1, val2
1

You can also use cut or awk for this, assuming you want to delete everything up to and including / from the lines...

$ cat ip.txt 
name/name, val1, val2
name2/name2, val3, val4

$ cut -d/ -f2 ip.txt 
name, val1, val2
name2, val3, val4

$ awk -F/ '{print $2}' ip.txt 
name, val1, val2
name2, val3, val4

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.