Tim Holum
Tim Holum

Reputation: 737

swap values in sed when they match a regex

I am trying to write a script to take all the php.js functions and create a node.js library for them, Which means I have to figure out how to convert

this

function key (arr) {

to

key: function (arr) {

I know sed can do it, But I can't figure it out

Thanks for any help

Upvotes: 0

Views: 118

Answers (3)

NeronLeVelu
NeronLeVelu

Reputation: 10039

sed "s/^ *function \{1,\}\([^ ]\{1,\}\) \{1,\}(/\1: function (/" file.js

replace the \s by " " assuming there is not tab in white space of the code

Upvotes: 0

Jotne
Jotne

Reputation: 41460

Here is an awk to swap fields

awk '/function/ {t=$2":";$2=$1;$1=t} 1' file.js
key: function (arr) {

Upvotes: 0

Sotiris Kal.
Sotiris Kal.

Reputation: 68

sed  's/function\s\(.*\)(/\1: function(/g' file.js

That should do the trick

Upvotes: 2

Related Questions