0

Hey I have a problem with regular subexpresions.

grep -o ".*='.*'" parameters.txt

parameters.txt includes:

name='something'
lastname=' 123123'
something='   somethinggg'

My regular expresion returns everything, but i want it to return only name and value without = and ' '. I also want it to be immune to spaces and tabulators.

name something
lastname 123123
somethign somethinggg
1
  • Is the right hand side guaranteed to always be single quoted? Can the value to the right of = also contain =-characters? What does the "immunity" actually mean? Does it mean you want to allow the line to be indented by spaces or tabs (should the indentation be preserved in that case?), or does it mean you want to maintain any spaces or tabs in the values to the right?
    – Kusalananda
    Commented Jan 3, 2020 at 15:09

4 Answers 4

2

In your case, I would recommend using sed instead:

sed -r "/^[^=]*$/d;t;s/=/ /;s/'//g;s/[\t ]+/ /g" parameters.txt

This command

  • skips any line not containing an = sign
  • replaces the first = by a space (leaving it open that the "value" part may itself contain an assignment)
  • replaces any single quotes ' by nothing
  • replaces any multiple tabs and spaces by one single space
2
  • 1
    Maybe s/=/ /g should not have the g.
    – tripleee
    Commented Jan 3, 2020 at 14:03
  • You are right, it is unnecessary there and would break things if the argument also contained an = sign.
    – AdminBee
    Commented Jan 3, 2020 at 14:04
1
sed -e 's/['\''=]/ /g;s/  */ /g'
0
0
$ tr '=' ' ' < parameters.txt  | sed "s,',,g"  | sed -E "s,\s+, ,g"
name something
lastname 123123
something somethinggg
1
  • You can nearly always combine two sed commands into one. Many sed dialects do not support the Perl escape \s (try [[:space:]] for a portable equivalent). Also, notice that sed has a y command; but replacing all equals slgns will wreck any value which contains a literal equals sign.
    – tripleee
    Commented Jan 3, 2020 at 14:00
0

Using Perl:

perl -pe "tr/=' \t/ /d"   parameters.txt

You must log in to answer this question.

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