PDA

View Full Version : shell scripting



.vogue
February 23rd, 2012, 04:13 PM
I am a newbie in shell scripting. I want to know if I have a file which is full of this kind of pattern
file start: [{'name': 'american pie'}, {'name': 'national geographic'}, {'name':'football'},{'name': 'and_so_on'},redundant}] file end

how do I parse the file to get
a)
name: american pie
name: national geographic
name: football
name: and_so_on

AND ALSO

b)
american pie
national geographic
football
and_so_on

ofnuts
February 23rd, 2012, 06:46 PM
You can use sed to remove the punctuation you don't need:



sed -e "s/': *'/:/g" -e "s/'}, *{'/\n/g" -e "s/^\[{'//" -e "s/'},[^:]*}\]/\n/" <file

.vogue
February 23rd, 2012, 07:33 PM
Thank you ofnuts! But, how do I parse the 'name' from it?

Vaphell
February 23rd, 2012, 07:54 PM
you can use another sed


... | sed -r 's/^(.*):(.*)$/col1=\1 col2=\2/'

col1=name col2=american pie
col1=name col2=national geographic
col1=name col2=football
col1=name col2=and_so_on


you can use awk


... | awk -F: 'NF==2 {print "col1 =", $1, "col2 =", $2 }'

col1 = name col2 = american pie
col1 = name col2 = national geographic
col1 = name col2 = football
col1 = name col2 = and_so_on

you can read the output in while loop

... | while IFS=: read c1 c2; do echo "col1=$c1 col2=$c2"; done

col1=name col2=american pie
col1=name col2=national geographic
col1=name col2=football
col1=name col2=and_so_on
col1= col2=

.vogue
February 23rd, 2012, 08:07 PM
Now, I, somehow, got the idea! Thanks Vaphell!