Reputation: 30699
I'm having a problem using the sed substitution option that executes a command in the replacement pattern. I believe the option is an extensions to GNU sed but I haven't been able to find any documentation on it. Anyone know of any?
My problem is that I can't extract the matching text properly when using the execute replacement option. I'm using GNU sed 4.2 on Mac OSX 10.7.4
echo "12345678\r" | sed 's/.*/echo "&);"/e'
Result: );345678
Desired Result: 12345678);
I've tried using different regex matches, such as \d+, but that just seems to break the ability to use the /e substitution option. And I've tried using an explicit capture group such as \1 instead of & but that also seems to break the /e option.
Update:
My example was perhaps oversimplified what I'm ultimately trying to do is take a text file with each line holding a unique value and pass that to sed and use the uuidgen command (in the same manner as a previous question I posted). The new problem I've got is because I'm now running the command on a Mac the \r is not being stripped from the matched string.
The full command I want to execute is:
sed 's/.*/echo "blah blah `uuidgen`,&):"/e' file.txt
Update 2:
In my Mac terminal I don't need to use echo -e to get \r interpreted as a line feed and in fact according to "man echo" there is no -e option. It does make life difficult moving between all the terminal environments :(.
Thanks to kev I was able to come up with a solution that worked on Mac.
sed 's/\r//; s/.*/ echo "blah blah (`uuidgen`,&);"/e' file.txt
Upvotes: 1
Views: 1965
Reputation: 28752
);345678
What echo
outputs is 12345678
followed by \r
which takes the cursor back to beginning of line and the );
resulting in the string you see in the window. Put a |od -cx
at the end of command to see.
On my cygwin
install I need echo -e
to get the similar behavior. I am not able to remove \r
in sed
but I am able to do by adding tr
:
$ echo "12345678\r" | sed 's/.*/echo -e "&);"/e'| tr -d "\r"
12345678);
Upvotes: 2
Reputation: 161674
Remove the e
flag:
$ echo "12345678\r" | sed 's/.*/echo "&);"/'
echo "12345678\r);"
Add e
flag is the same as:
$ echo -e "12345678\r);"
);345678
You can remove the \r
:
$ echo "12345678\r" | sed -e 's/\\r//' -e 's/.*/echo "&);"/'
12345678);
Upvotes: 2