blueren blueren - 2 years ago 64
Bash Question

Fastest way to edit text using unix commands rather than dedicated app

I'm looking for ways to quick format the below text using the avalable unix tools and commands rather than downloading a dedicated text editor app. I'm using ubuntu. What's the quickest way to have this done?

shelf-1 : Shelf 1
shelf-2 : Shelf 2


{label: "Shelf 1", value: "shelf-1"},
{label: "Shelf 2", value: "shelf-2"}

At the moment, I've been doing it using text editor that ubuntu has, in combination with regex and find/replace options. It's taking too long since I have to format a lot of data.

Answer Source

You can use awk for this:

awk -F " : " '{print "{label: \""$2"\", value: \""$1"\"}"}' file.txt

Assuming your data is in file.txt, it uses : as the separator, after that you have data before : in $1 and the data after it in $2.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download