1

This is probably a trivial one:

I have a file (my.file) with these lines:

>h1_c1
>h1_c2
>h1_c3
>h2_c1
>h2_c2
>h2_c3

and I want to change it in place to be:

>c1_h1
>c2_h1
>c3_h1
>c1_h2
>c2_h2
>c3_h3

I thought this ought to do it:

sed -i 's/\(\>\)\(h1\)\(\_\)\(.*\)/\1 \4 \3 \2/g' my.file
sed -i 's/\(\>\)\(h2\)\(\_\)\(.*\)/\1 \4 \3 \2/g' my.file

but it doesn't seem to work. How do I do it?

1
  • pls confirm that the last line of your output should be c3_h2 not h3 Commented Mar 13, 2014 at 14:28

4 Answers 4

2

The obvious sed for your example is:

$ sed -i~ -e 's/^>\(h[0-9]\)_\(c[0-9]\)/>\2_\1/' *.foo

I tested this and it works for your example file.

Sign up to request clarification or add additional context in comments.

Comments

1

Try this awk

awk -F">|_" '{print ">"$3"_"$2}' my.file > tmp && mv tmp my.file

awk -F">|_" '{print ">"$3"_"$2}' my.file
>c1_h1
>c2_h1
>c3_h1
>c1_h2
>c2_h2
>c3_h2

Comments

1

You can try this sed,

sed 's/>\(h[1-2]\)_\(.*\)/>\2_\1/' yourfile

(OR)

sed -r 's/>(h[1-2])_(.*)/>\2_\1/' yourfile

Comments

1
kent$  sed -r 's/>([^_]*)_(.*)/>\2_\1/' f
>c1_h1
>c2_h1
>c3_h1
>c1_h2
>c2_h2
>c3_h2

you add -i if you want it to happen "in-place"

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.