用于迭代html文件并创建csv文件的脚本
我有一个html文件,其中包含我正在处理的项目的依赖项列表。其格式如下: --一些html用于迭代html文件并创建csv文件的脚本,html,bash,csv,unix,scripting,Html,Bash,Csv,Unix,Scripting,我有一个html文件,其中包含我正在处理的项目的依赖项列表。其格式如下: --一些html <p><strong>Module Name:</strong> spring-web</p> <p><strong>Module Group:</strong> org.springframework</p> <p><strong>Module Version:<
<p><strong>Module Name:</strong> spring-web</p>
<p><strong>Module Group:</strong> org.springframework</p>
<p><strong>Module Version:</strong> 4.2.1.RELEASE</p>
模块名称:SpringWeb
模块组:org.springframework
模块版本:4.2.1.0版本
--更多html
<p><strong>Module Name:</strong> google-http-client</p>
<p><strong>Module Group:</strong> com.google.http-client</p>
<p><strong>Module Version:</strong> 1.19.0</p>
模块名称:谷歌http客户端
模块组:com.google.http-client
模块版本:1.19.0
等
我想从这个html文件创建一个csv文件
csv文件的格式为每条记录:
模块名称、模块组、模块版本
e、 g。
谷歌http客户端,com.google.http客户端,1.19.0
你知道如何用脚本实现这一点吗?试试看
#!/bin/bash
inFile=$1
outFile=$2
join () {
local del=$1
shift
IFS="$del"
source <(
cat <<SOURCE
echo "\${$1[*]}"
SOURCE
)
unset IFS
}
declare -a CSV=('"Module Name","Module Group","Module Version"')
declare -a keysAccepted=('Name' 'Group' 'Version')
declare -i nMandatoryKeys=${#keysAccepted[@]}
declare -A KeyFilled
rxKeysAccepted='('$(join '|' keysAccepted)')'
while read line; do
[[ $line =~ \<strong\>Module\ $rxKeysAccepted:\</strong\>[[:space:]]*([^<]+)\</p\> ]] || continue
key=${BASH_REMATCH[1]}
val=${BASH_REMATCH[2]}
KeyFilled[$key]=$val
if (( ${#KeyFilled[@]} == nMandatoryKeys )); then
unset csvLine
for k in ${keysAccepted[@]}; do
csvLine+=${csvLine:+,}${KeyFilled[$k]}
done
KeyFilled=()
CSV+=($csvLine)
fi
done <"$inFile"
(( ${#CSV[@]} > 1 )) || exit 1
join $'\x0a' CSV >"$outFile"
#/bin/bash
填充=1美元
输出文件=2美元
加入(){
本地del=$1
转移
IFS=“$del”
source如果您的源文件是一致的(所有三个字段以相同的顺序存在),您可以尝试以下操作
$ sed -nr 's_\s*<p><strong>Module (Name|Group|Version):</strong> (.*)</p>_\2_p' file\
| awk 'ORS=NR%3?",":RS'
spring-web,org.springframework,4.2.1.RELEASE
google-http-client,com.google.http-client,1.19.0
$sed-nr的\uS*模块(名称|组|版本):(*)\2\u p'文件\
|awk'ORS=NR%3?,“:RS”
SpringWeb,org.springframework,4.2.1.RELEASE
谷歌http客户端,com.google.http客户端,1.19.0
使用XML解析器(xmlstarlet,xmllint,…)。第2行:declare:-A:无效选项declare:用法:declare[-afFirtx][p][name[=value]…]此代码将在BASH 4上运行(4是主要版本号),但这个版本的BASH是我所知道的所有现代*nix的默认版本…谢谢,我如何将文件作为参数传递,而不必在“EOHTML”之后复制它。如果我想在每行中添加另一列,例如license.license:Apache。我应该在哪里添加此模式匹配?修改:“declare-a CSV”(添加新列的标题),“declare-a keysAccepted”-添加新列名,[[$line=~…”-替换正则表达式:\Module\$rxKeysAccepted:\([[:space:]*([^