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-rprevents interpreting backslashes (\) as escapes and prints them like any other character (which probably is what you want).IFSis 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-ronreadis the only change that's necessary here. SettingIFSto the empty string retains initial and trailing whitespace, which is a good habit to get into, even if it doesn't matter here becausexmllintdoesn't care about these. The double quotes around$linearen'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= readused so often, instead ofIFS=; while read..? – Gilles 'SO- stop being evil' Feb 09 '12 at 09:20