paul sinohin
paul sinohin

Reputation: 23

Delete multiple strings/characters in a file

I have a curl output generated similar below, Im working on a SED/AWK script to eliminate unwanted strings.

File

{id":"54bef907-d17e-4633-88be-49fa738b092d","name":"AA","description","name":"AAxxxxxx","enabled":true}
{id":"20000000000000000000000000000000","name":"BB","description","name":"BBxxxxxx","enabled":true}
{id":"542ndf07-d19e-2233-87gf-49fa738b092d","name":"AA","description","name":"CCxxxxxx","enabled":true}
{id":"20000000000000000000000000000000","name":"BB","description","name":"DDxxxxxx","enabled":true}

......

I like to modify this file and retain similar below,

AA AAxxxxxx
BB BBxxxxxx
AA CCxxxxxx
BB DDxxxxxx
AA n.....
BB n.....

Is there a way I could remove word/commas/semicolons in-between so I can only retain these values?

Upvotes: 1

Views: 872

Answers (3)

Juan Diego Godoy Robles
Juan Diego Godoy Robles

Reputation: 14955

Try this awk

curl your_command | awk -F\" '{print $(NF-9),$(NF-3)}'

Or:

curl your_command | awk -F\" '{print $7,$13}'

A semantic approach ussing perl:

curl your_command | perl -lane '/"name":"(\w+)".*"name":"(\w+)"/;print $1." ".$2'

For any number of name ocurrences:

curl your_command | perl -lane 'printf $_." " for ( $_ =~ /"name":"(\w+)"/g);print ""'

Upvotes: 3

potong
potong

Reputation: 58430

This might work for you (GNU sed):

sed -r 's/.*("name":")([^"]*)".*\1([^"]*)".*/\2 \3/p;d' file

This extracts the fields following the two name keys and prints them if successful.

Alternatively, on simply pattern matching:

sed -r 's/.*:.*:"([^"]*)".*:"([^"]*)".*:.*/\1 \2/p;d' file

Upvotes: 1

martin
martin

Reputation: 3239

In this particular case, you could do

awk -F ":|," '{print $4,$7}' file2 |tr -d '"'

and get

AA AAxxxxxx
BB BBxxxxxx
AA CCxxxxxx
BB DDxxxxxx

Here, the field separator is either : or ,, we print the fourth and seventh field (because all lines have the entries in these two fields) and finally, we use tr to delete the " because you don't want to have it.

Upvotes: 0

Related Questions