0

the output of my command is:

scan: DVD has 6 title(s)
...some text...
+ title 1:
...some text...
  + duration: 00:43:12
...some text...
+ title 2:
...some text...
  + duration: 00:43:12
...some text...
+ title 3:
...some text...
  + duration: 00:41:15

i want to have var for:

6 from scan: DVD has 6 title(s)

1 from + title 1:

00:43:12 from + duration: 00:43:12

not that the two last one are linked, maybe use an array For title and duration title[1]="00:43:12"

actually i'm only able to extract one information at a time but i don't want to run the command multiple time if possible. for example:

[command] |& grep -Po '(?<=DVD has )([0-9]+)')

maybe i should store the command output to a file first? what is the best way to start please ?

2
  • 1
    For just the parsing alone, let alone what you intend to do with the parsed data, I would recommend using a different language. bash (and shell in general) is not really meant for data processing; it's meant for process control and file handling. Commented Feb 12, 2014 at 13:58
  • Ok, so maybe i should store the command output to file (+/- 600 lines) and parse the file for different var? Commented Feb 12, 2014 at 14:00

2 Answers 2

0

You didn't specify what you wanted to do with the values, but here's an awk script that gathers them and outputs them for every line of input:

yourcommand | awk '
  NR==1{ titlecount=$4; }
  /\+ title/{ curtitle=$3; }
  /\+ duration/{ dur=$3; }
  1{ print titlecount, curtitle, dur; }
'
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks, want to store values in array: title[number]="associated duration". Your awk script print lines that match + title or + duration several times, it also print a lot of empty lines, lines that don't match ?
@Eelisland I can't really tell exactly what output you want. Why do you want the values in an array? What will you do with the array?
0

sorry my question is not clear. Based on your answer

cat DVDscan.txt | awk '
  /+ title/{gsub(":","",$3); print $3 }
  /+ duration:/{ print $3 }'

Output:

1
00:43:12
2
00:43:12
3
00:41:15
4
00:41:15
5
00:41:17
6
00:41:17
7
00:41:15
8
00:41:15

want to store the output to multiples array:

title[1]="00:43:12"

so i will know the total number of title and can access duration for specified title.

Thanks for your time and help.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.