Regexp tip

Hello,

I'm just starting working on it. I'd like to get a tip
For istance if I have a file like:

a      b    c
d     e     f
....


and I wanna get:

1a   &    2b   &  3c
0d   &    8e    &   4f
.....

I would like to use sed and come up with a regular expression that works. I was trying:

s/\s?/\&/g

But I'm wrong. can anyone suggest the right please?

Thx

I'm not sure what you're trying to do with \s. Is that Perl syntax? It's not in POSIX regular expressions.

Remember that the 's/this/that/' syntax replaces the text it matches. It doesn't put any bit of 'this' back into 'that'. If you have GNU sed, and enable backreferences with the -r flag, you can put brackets around parts you want to match and substitute them in the output string with \1, \2, etc.

That won't help you translate 'a' to '1', though. (I'm not sure from where you're pulling these numbers, either.) The replacements you want are complex: Split a line into elements, for each element match something, read it in, translate it to a number, insert in front... A language like awk would probably be more appropriate than a regex tool like sed.

---------- Post updated at 10:17 AM ---------- Previous update was at 10:06 AM ----------

Think I see better what you're trying to do. sed can do part of it.

$ echo a s d f | sed -r 's/[ ]+/ \& /g'
a & s & d & f
$
  • [ ]+ : match one or more spaces
  • \& : replace with space, ampersand, space
  • g : match multiple times per line if possible

You need the -r flag to match "one or more" with + like that. It could be GNU sed only.

1 Like

Indeed -r is GNU only ....

+ is not part of basic regular expressions. You would have to do something like this:

echo "a      b    c" | sed 's/ \{1,\}/ \& /g'

or this:

echo "a      b    c" | sed 's/  */ \& /g'

The square brackets are not required. You can use [ \t] instead of each single space in the first part of the sed examples, if you want to replace both spaces and tabs.

2 Likes

Hello,

Thx a lot both. I really appreciate it together with the explanation.