I'm trying to create a BASH script to extract fragments of text from a file that has many lines similar to this one:


From each line I only have to keep the:


The context can be deleted. I was trying using sed but I can't find a way to do that. I don't understand how create a command using the regular expression that I already have:


I have been reading and already tried this command and other similar ones but I don't know much about this:

cat file.txt | sed -i -E "s/\w+(\/[\w-]+)+"

That definitely is not working and I can't find a beginners guide on how to use sed and regular expressions.

  • Do you need the second column of each line or will your content be in different columns?
    – jesse_b
    Commented Apr 21, 2018 at 21:18
  • 1
    awk -F, '{print $2}' input
    – jesse_b
    Commented Apr 21, 2018 at 21:23
  • @Jesse_b Thank you, that worked really well but it keeps the " symbol for each one. Is there a way to keep the same result without the "?
    – eera5607
    Commented Apr 21, 2018 at 21:48
  • awk -F, '{print $2}' input | tr -d '"'
    – jesse_b
    Commented Apr 21, 2018 at 21:50
  • @Jesse_b yes that's it, it isn't the approach that I was searching for but it worked great. How do we proceed to mark it as solved?
    – eera5607
    Commented Apr 21, 2018 at 21:53

2 Answers 2


Parsing a file like this is better done with awk:

awk -F, '{ print $2 }' file

or cut,

cut -d, -f 2 file

This would give you "category/subcategory/sub-subcategory/item-1" (including the quotes) from the data that you supplied.

With sed, and utilizing the regular expression that you provided (but with \w replaced by [[:alnum:]]):

sed -E 's@.*,"([[:alnum:]]+(/[[:alnum:]-]+)+)",.*@\1@' file

This replaces the whole line with the bit that matches the regular expression within the first set of parentheses. This returns category/subcategory/sub-subcategory/item-1 for the data that you provided.

For more general CSV file parsing, including correctly handling quoting and fields that may contain commas or double quotes, use csvkit:

csvcut -c 2 file

There's nothing wrong with your regex per se, just that you fell into the trap for the unwary. Inside of the [] the \w are two literals a backslash and a "w" and NOT a "\w" as one might think it to be or as in Perl, from where it's borrowed but not fully.

One way would be to lay out explicitly what is in a \w and then proceed:

sed -Ee '

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .