Question

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.

Was it helpful?

Solution

You can use BASH string substitution:

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

OR sed:

sed 's/ /  /' <<< "$s"
1be9df543bb5fa37c4b53f0401072f98  file.xml

OTHER TIPS

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.

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
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top