0

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

"11","category/subcategory/sub-subcategory/item-1","index.php?option=com_trombinoscopeextended&Itemid=125&lang=es&view=trombinoscope","251","0","0000-00-00","","","","","","","0"

From each line I only have to keep the:

category/subcategory/sub-subcategory/item-1

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:

\w+(\/[\w-]+)+

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 Apr 21 '18 at 21:18
  • 1
    awk -F, '{print $2}' input – Jesse_b Apr 21 '18 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 Apr 21 '18 at 21:48
  • awk -F, '{print $2}' input | tr -d '"' – Jesse_b Apr 21 '18 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 Apr 21 '18 at 21:53
0

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
0

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 '
    /\n/{P;D;}
    s|\w+(/[-_A-Za-z0-9]+)+|\n&\n|;D
'

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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