[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: Help with String Extraction
From: |
Steve Matzura |
Subject: |
Re: Help with String Extraction |
Date: |
Tue, 17 Sep 2024 14:33:39 -0400 |
User-agent: |
Mozilla Thunderbird Beta |
Andreas,
Brilliant! I've never used either xmlstarlet or xmllint before--never
even heard of them--and xmllint solves the problem elegantly.
I want the output of the xmllint command to be put into a string
variable. I've tried braces, single- and double-quoting, parentheses,
nothing works, which
means I'm confused about how to assign the output of a command
containing different quotes to a string variable. This string will be
used in a sed command
further down the script.
On 9/16/2024 5:11 PM, Andreas Kähäri wrote:
block quote
On Mon, Sep 16, 2024 at 02:06:22PM -0400, Steve Matzura wrote:
block quote
I'm trying to write a script that does the following:
In a loop, list certain files in a directory. For each file:
• Assign a variable to the result of grepping for "<svrinfodesc>" which is
always present
• Add 1 to the location of ">" in the string and assign it to a variable
• Assign the location of "</" in the string to a variable
• Assign the difference between the above two values to a variable
• Extract a substring from the original string starting at the first
positional variable for a length equal to the number of characters in the
calculated length variable
• Display the substring
*** Code ***
#!/bin/sh
for f in $(ls *.xml)
do
echo $f # Display the filename
stringZ = $(grep -i svrinfodesc $f) # Find the string I want
echo $stringZ # Let's see the string
pos1 = `expr index "$string" \>` + 1 # Position of first character after ">"
pos2 = `expr index "$string" \<\/` # Position of first character
after the
string
len = (pos2 - pos1) # How long will the substring be?
echo ${stringZ:pos1:len}
done
Test Data:
Create a file (anything.xml will do) with a couple junk lines as long as it
includes a line containing the grep target, like this one:
<svrinfodesc>Ham Radio Study Show</svrinfodesc>
Everything I've read about string extraction says I'm doing it right, but
nevertheless, the script aborts at that very line. What did I miss?
Thanks in advance.
block quote end
Don't do XML parsing in the shell. Use a tool that does it for you:
$ xmlstarlet sel -t -v '//svrinfodesc' -nl file
Ham Radio Study Show
$ xmllint --xpath '//svrinfodesc/text()' file
Ham Radio Study Show
For all file with names matching ./*.xml:
for name in ./*.xml; do
xmllint --xpath '//svrinfodesc/text()' "$name"
done >outfile
block quote end