$ echo "$var" | awk -F\" 'BEGIN{n=split("14 2 10 6 8",v," ")} {for (i=1;i<=n;i++) printf "var%d = \"%s\"\n",i,$(v[i])}' var1 = "Tue Nov 27 16:20:05 CET 2012" var2 = "Finished Number" var3 = "The Corresponding Metric Value is: 0.5" var4 = "Cleared" var5 = "major Over-Flow alert on Finished Number for ['3333']"
Also, perhaps more of what you want here is how to populate a shell array with the result of awk execution:
$ IFS=$'\n' varArr=( $(echo "$var" | awk -F\" 'BEGIN{n=split("14 2 10 6 8",v," ")} {for (i=0;i<=n;i++) printf "\"%s\"\n",$(v[i])}') ) $ echo "${varArr[1]}" "Tue Nov 27 16:20:05 CET 2012" $ echo "${varArr[2]}" "Finished Number" $ echo "${varArr[3]}" "The Corresponding Metric Value is: 0.5" $ echo "${varArr[4]}" "Cleared" $ echo "${varArr[5]}" "major Over-Flow alert on Finished Number for ['3333']"
and if you don't need quotes around your text, just don't add them to awk script:
IFS=$'\n' varArr=( $(echo "$var" | awk -F\" 'BEGIN{n=split("14 2 10 6 8",v," ")} {for (i=0;i<=n;i++) print $(v[i])}') )
Both of the above put the entire input string in $ {varArr [0]}. This is a trivial setting if this is undesirable.
source share