0

I have a file that has to be tailored; It contains "variables" that are represented by @variable.name@; There may be more than one such variable on a line; and I want to find all of them.

My thought is that this should be possible with a sed regex, but most linux commands are at my disposal...

The regex I thought of was this one: s#(.*)(@.*@)(.*)#\2#

However, that did not catch all...

Lets assume this file:

Dear @sirmadam@,

Today is @day@-@month@-@year@, a nice @weekday@day.
It is my @pleasureornot@ to tell you youre @hiredorsacked@.

Sincerely,
@nameoftheboss@

The output should be:

@sirmadam@
@day@
@month@
@year@
@weekday@
@pleasureornot@
@hiredorsacked@
@nameoftheboss@

I think my main issue is that the start and end of each variable uses the same sign, but there is little I can do about that... My regex-fu is lacking... I cant wrap my head around how this could be solved, it is a bit of a puzzle for me.

Konrad Rudolph
  • 530,221
  • 131
  • 937
  • 1,214
Michiel
  • 123
  • 11

1 Answers1

2

grep works:

grep -o '@[^@]*@' foo.txt

By contrast, sed fundamentally works in a line-based fashion, and breaking out of this paradigm, while occasionally possible, is hard and usually indicates that another tool is better suited.

Konrad Rudolph
  • 530,221
  • 131
  • 937
  • 1,214