利用shell脚本文本过滤XML文件-awk工具

源文件

<?xml version="1.0" encoding="UTF-8" ?>
 
<result>
 
<lrc id="123" artist="linkin park" title="In the end"></lrc>
 
<lrc id="456" artist="arvil" title="thing's you never know"></lrc>
 
</result>

——————— 格式化输出文件 ID: 123 artist: linkin park title In the end ID: 456 artist: arvil title thing’s you never know ——————— 文本过滤的shell脚本(写的很实用)

#!/bin/bash
 
XML_FILE_NAME=$1
 
grep &quot;lrc&quot; ${XML_FILE_NAME} | cut -d '&quot;' -f 2,4,6 | awk 'BEGIN {FS=&quot;\&quot;&quot;} {id[NR]=$1;artist[NR]=$2;title[NR]=$3}END {for (i=1;i&lt;NF;i++) print &quot;ID: &quot; id[i]&quot;\tartist: &quot; artist[i]&quot;\ttitle&quot; title[i]}'

Leave a Comment.