Dhinesh
Dhinesh

Reputation: 67

How to interchange words in shell script?

This is my input line
[[ref:start|Other config files, programs, and scripts]]

and I need to convert above line into
"Other config files, programs, and scripts":ref/start

How can I convert this using the sed command?

Upvotes: 0

Views: 172

Answers (1)

Ilia K.
Ilia K.

Reputation: 4952

It's easier to write than to read :)

$ echo "[[ref:start|Other config files, programs, and scripts]]" |\
  sed 's/\[\[\([^:]*\):\([^|]*\)|\(.*\)\]\]/"\3":\1\/\2/'
"Other config files, programs, and scripts":ref/start

I'd recommend you read man sed as well as GNU sed manual. It's not that big and you actually don't need to read it all at once. In most cases it's sufficient to know s/// command, regular expression syntax and sed's flags (most useful are -n and -i).

Upvotes: 2

Related Questions