HTML 파일이 있는데 단락 요소( ) 내부의 텍스트를 와 <p>
같은 동일한 대문자로 바꿔야 합니다 .<p>hi</p>
<p>HI</p>
x=`cat $1 | grep -o '<p>.*</p>' | tr '[:lower:]' '[:upper:]'`
var2=`echo $x`
headerremove=`grep -o '<p>.*</p>' $1`
var3=`echo $headerremove`
echo $var2
echo $var3
sed 's/$var3/$var2/g' "$1"
Input
<h1>head</h1>
<p>hello</p>
Output
<p>HELLO</p>
예상대로 작동하지 않습니다. 또한 단락 요소를 제외한 모든 태그 및 해당 하위 요소와 같은 다른 모든 세부 정보를 제거해야 합니다.
답변1
xmllint
+sed
해결책:
xmllint --html --xpath "//p" input.html | sed 's/>[^<>]*</\U&/'
산출:
<p>HELLO</p>
답변2
$ cat f.html
<h1>head</h1>
<p>hello</p>
<p>world</p>
$ grep -o '<p>.*</p>' f.html | tr '[:lower:]' '[:upper:]' | sed 's/P>/p>/g'
<p>HELLO</p>
<p>WORLD</p>
# capture other tags: grep multi-pattern e.g 'patt1\|patt2\|pattN'
$ grep -o '<p>.*</p>\|<h1>.*</h1>' f.html | tr '[:lower:]' '[:upper:]' | sed 's/P>/p>/g;s/H1>/h1>/g'
<h1>HEAD</h1>
<p>HELLO</p>
<p>WORLD</p>
# add line after h1 tag : grep+tr+sed
function foo () {
grep -o '<p>.*</p>\|<h1>.*</h1>' "${1}" | tr '[:lower:]' '[:upper:]' | sed 's/P>/p>/g;s/H1>/h1>/g' | while read line; do
case "${line}" in
"<h1>"*)
echo "${line}"
echo "anything that should appear after h1 tags"
;;
"<p>"*)
echo "${line}"
;;
esac
done
}
$ foo f.html
<h1>HEAD</h1>
anything that should appear after h1 tags
<p>HELLO</p>
<p>WORLD</p>
# add line after h1 tag : few [shell parameter expansion] tips + while & case statments
function foo () {
grep -o '<p>.*</p>\|<h1>.*</h1>' "${1}" | while read line; do
case "${line}" in
"<h1>"*)
line="${line^^}"; #capitalize (shell parameter expansion)
echo "${line//H1>/h1>}" # find replace (shell parameter expansion)
echo "anything that should appear after h1 tags"
;;
"<p>"* | "<P>"*) # if html files contain capitalized P tag and you wanna capture them
line="${line^^}"; #capitalize
echo "${line//P>/p>}" # find replace
;;
"<foo>"*)
line="${line^^}"; #capitalize
linopenintag="${line//<foo>/}"; # <foo>hello world</foo> ==> hello world</foo>
innerHTML="${linopenintag//<\/foo>/}"; # hello world</foo> ==> hello world
innerHTMLarr=(${innerHTML}); # in case i want to put each word in a spin or/and style that word differently
for eachword in ${innerHTMLarr[@]}; do
if [[ "${eachword}" == "something" ]]; then # capture special words ...
echo "<bar style='...'> ${eachword} </bar>"
else
echo "<bar> ${eachword} </bar>"
fi
done
;;
esac
done
}
$ foo f.html
<h1>HEAD</h1>
anything that should appear after h1 tags
<p>HELLO</p>
<p>WORLD</p>