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.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I have text files (*.txt) which I process, I want to find words connected by hyphen such as "drug-associated" or "drug-linked", "drug-related".

I need your help to convert the first one to "associated with drug" and the second one convert it to "linked with drug" instead. In one file I could have many of those, one time it is "associated", "linked", "related" and could have other terms.

share|improve this question
perl -pi.back -e 's/(\w+)-(\w+ed)\b/$2 with $1/g' ./*.txt

That's portable and would work with any system having perl installed.

On recent GNU systems, you can do something similar with GNU sed:

sed -ri.back 's/(\w+)-(\w+ed)\b/\2 with \1/g'


for f in ./*.txt; do
  mv -i "$f" "$f.back" &&
    sed -e :1 -e 's/\([[:alnum:]]\{1,\}\)-\([[:alnum:]]\{1,\}ed\)\(\([^[:alnum:]].*\)\{0,1\}\)$/\2 with \1\3/;t1' < "$f.back" > "$f"
share|improve this answer
can i do it using sed? – user2957777 May 1 '14 at 3:37

Your Answer


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.