Hi all, i am new to unix scripting in ksh or any shell for that matter. I have downloaded a xml file from a website and saved on my local harddrive. inside the xml, the same tag is listed multiple times.
<title>Tonight</title>
<title>Thursday</title>
<title>Friday</title>
<title>Saturday</title>
i was able to find the first occurrence of the line with:
ls file.xml | while read line; do
fileTitle=`grep -m 1 "<title>" "$line" | sed s/".*\<title\>"//g | sed s/"\<\/title\>.*"//g`
Which my end result was to output the string "Tonight" all by itself.
However, i am choking on trying to find the 2nd occurrence of <title> and 3rd and so on, so i can print each day of the week as its own variable :wall:
I am in dire help and cannot figure this out, any help will be greatly appreciated.
How about this:
SEQ=3
awk -vN=$SEQ -F'[<>]' '$2=="title" && !--N { print $3 ; exit }' infile.xml
that didn't work
here is the extent of my script so far
#!/bin/sh
basefolder=/s/batchtmp/weather_tmp
cd $basefolder
ls file.xml | while read line; do
fileTitle=`grep -m 1 "<title>" "$line" | sed s/".*\<title\>"//g | sed s/"\<\/title\>.*"//g`
SEQ=3
fileTitle2-`awk -vN=$SEQ -F'[<>]' '$2=="title" && !--N { print $3 ; exit }' file.xml`
echo $fileTitle
echo $fileTitle2
done
This is what i get from the terminal
computer:~ me$ . test.sh
awk: invalid -v option
Tonight
You may need to use nawk if your on Solaris:
ls file.xml | while read line; do
fileTitle=`grep -m 1 "<title>" "$line" | sed s/".*\<title\>"//g | sed s/"\<\/title\>.*"//g`
SEQ=3
fileTitle2=`nawk -vN=$SEQ -F'[<>]' '$2=="title" && !--N { print $3 ; exit }' $line`
echo $fileTitle
echo $fileTitle2
done
Im on Mac OS X
-bash: nawk: command not found
i guess it doesn't like nawk, ugh
OK, the awk on Mac OS seems a little strange, perhaps best to avoid using -v at all try this:
ls file.xml | while read line; do
fileTitle=`grep -m 1 "<title>" "$line" | sed s/".*\<title\>"//g | sed s/"\<\/title\>.*"//g`
SEQ=3
fileTitle2=`awk -F'[<>]' '$2=="title" && !--N { print $3 ; exit }' N=$SEQ $line`
echo $fileTitle
echo $fileTitle2
done
That worked! Thank you very much!