I have the following bash command which fetches and block XML from a single column in mysql and feeds it into xmllint to prettify it.
mysql --format csv --skip-column-names -e "select xmldata from mytable limit 1;" | xmllint --format -
So far so good, but it only works for one row at a time. I would like to adapt this so each line in the SQL query output is fed in to a separate call to xmlint.
I know this can be done with a loop in a bash script -- but I'd like to do it in a one-liner if possible. Supposedly this can be done with xargs but I can't figure it out.
while read line; do echo $line | xmllint...
– Kyle Smith Feb 08 '12 at 22:18-r
prevents interpreting backslashes (\
) as escapes and prints them like any other character (which probably is what you want).IFS
is the input field separator, setting it there makes sure it's not set to something unexpected, without changing it for later commands. – Kevin Feb 09 '12 at 04:12-r
onread
is the only change that's necessary here. SettingIFS
to the empty string retains initial and trailing whitespace, which is a good habit to get into, even if it doesn't matter here becausexmllint
doesn't care about these. The double quotes around$line
aren't necessary because no shell that supports<<<
performs any expansion on it; again, double quotes around variable substitutions are a good habit to get into. See also Why iswhile IFS= read
used so often, instead ofIFS=; while read..
? – Gilles 'SO- stop being evil' Feb 09 '12 at 09:20