Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

I have a list of random memory addresses saved in a text file, and I need to sed them out, decode them, and save them back to my file. However, the decoding operation I'm trying to perform is handled in a C++ program I've created some time ago. The problem I am encountering now is executing the C++ within the sed command; that is, I want to back-reference the part that needs to be decoded and use it as an argument in my C++ program. I have written the following command:

cat $1 | sed -r 's/(0[Xx])([0-9A-Za-z]{1,8})/\1`./convAddrs \2`/g';

before I had the chance to read an answer in this post saying that sed does NOT execute commands. Is it true that sed doesn't allow the execution of commands? If so, assuming the command I've written is a valid sed sequence, how would I write it in perl?

share|improve this question
    
link added. my bad :) –  Fadi Hanna AL-Kass Jul 31 '13 at 17:14
add comment

1 Answer 1

up vote 8 down vote accepted
perl -pe 's|(?<=0x)[0-9a-f]{1,8}|`./convAddrs $&`|gei'
  • perl -pe: like sed: process the input one line at a time in $_, evaluate the perl [e]xpression passed to -e for each line and [p]rint the modified $_ for each.
  • s|X|Y|gei: substitute Y for X in $_ ([g]lobally, case [i]nsensitively, and treating Y as a perl [e]xpression instead of a basic string).
  • (?<=0x): look behind for 0x.
  • [0-9a-f]{1,8}: one to 8 hex digits, as many as possible
  • `./convAddrs $&`: replace by the output of that shell command line where $& is replaced by the matched part.
share|improve this answer
    
I'm getting the following error when executing your command: Having no space between pattern and following word is deprecated at -e line 1. Regexp modifiers "/a" and "/d" are mutually exclusive at -e line 1, at end of line Regexp modifiers "/a" and "/d" are mutually exclusive at -e line 1, at end of line Can't find string terminator "`" anywhere before EOF at -e line 1. –  Fadi Hanna AL-Kass Jul 31 '13 at 17:24
1  
Of course, sorry my bad, you need to change the separator as we can't use / here since it's used inside the replacement part. Corrected. –  Stéphane Chazelas Jul 31 '13 at 17:26
    
Hi Sir, Can please explain... thanks.. –  Rahul Patil Jul 31 '13 at 17:30
    
Works like a charm now. thanks –  Fadi Hanna AL-Kass Jul 31 '13 at 17:32
    
Damn, and I though that I was pretty good at Perl one liners, +1! –  terdon Aug 1 '13 at 15:00
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.