2

I need to add a space between the md5sum and the file name. So my current output is:

1be9df543bb5fa37c4b53f0401072f98 file.xml

And I need it to be:

1be9df543bb5fa37c4b53f0401072f98  file.xml

I am a mess with regex/sed but this is what I have and I'm kind of stumped:

sed -E 's/([-A-Za-z_0-9]+\.[-A-Za-z_0-9]+).*$/\1  \2/'

Output for this is:

sed: -e expression #1, char 45: invalid reference \2 on `s' command's RHS

Any help is appreciated.

cakes88
  • 1,857
  • 5
  • 24
  • 33

3 Answers3

3

You can use BASH string substitution:

s='1be9df543bb5fa37c4b53f0401072f98 file.xml'
echo "${s/ /  }"
1be9df543bb5fa37c4b53f0401072f98  file.xml

OR sed:

sed 's/ /  /' <<< "$s"
1be9df543bb5fa37c4b53f0401072f98  file.xml
anubhava
  • 761,203
  • 64
  • 569
  • 643
2

You're trying to reference two match-groups, but you've only created one (each parenthesized element in the pattern is a group).

I like anubhava's suggestion for its simplicity, but if you don't want to turn every space into two spaces, change your original regex as follows:

sed -E 's/([-A-Za-z_0-9]+\.[-A-Za-z_0-9]+)/ \1/'

Note that all this actually buys you is that you don't insert a space unless the next word has a period in it.

Kyle Strand
  • 15,941
  • 8
  • 72
  • 167
1

Some awk solution:

echo "1be9df543bb5fa37c4b53f0401072f98 file.xml" | awk '{print $1"  "$2}'
1be9df543bb5fa37c4b53f0401072f98  file.xml

echo "1be9df543bb5fa37c4b53f0401072f98 file.xml" | awk '{print $1,$2}' OFS="  "
1be9df543bb5fa37c4b53f0401072f98  file.xml
Jotne
  • 40,548
  • 12
  • 51
  • 55