分页: 1 / 1

[问题]如何遍历子目录并把列出所有的文件名传递给一个sed脚本

发表于 : 2006-12-19 15:27
hyfans
找到个sed的html2txt的脚本,因为要转换的html文件分散放在嵌套的子目录下面,
想如何能遍历目录的同时把遇到的每个文件转换成txt?

sed脚本的代码

代码: 全选

#!/usr/bin/sed -nf
#
# Usage:  ./html2txt.sed file.html > file.txt
#                  OR
#         cat file.html | ./html2txt.sed > file.txt
#
# Bugs: Anand Avati <avati@hardcodecafe.com>
#


#/<[^>]*$/b SearchTagEnd
#b ParseHTML
#:SearchTagEnd
#N
#/<[^>]*$/b SearchTagEnd


# convert entire file/stdin into single pattern space
:GetLine
/^[ \t\n]*$/d
N
$b ParseHTML
b GetLine


# start parsing tags
:ParseHTML

# convert whitespace groups into single ' '
s/[ \t\n][ \t\n]*/ /g

# explicit space
s/&nbsp;/ /g

# fancy stuph
s/&copy;/(c)/g
s/&raquo;/>>/g
s/&middot;/*/g

# standard tags
s/< *[pP] *>/\n/g
s/< *\/[pP] *>/\n/g
s/< *[bB][rR] *>/\n/g

# parse table related tags
# <table>   =>  \n
s/< *[tT][aA][bB][lL][eE] *>/\n/g
s/< *[tT][rR] *>//g
s/< *\/[tT][rR] *>/\n/g
s/< *[tT][dD] *>//g
s/< *\/[tT][dD] *>//g

# <input type="radio" ..>    =>   ( )
s/< *[iI][nN][pP][uU][tT] [^>]*[tT][yY][pP][eE] *= *["]*[rR][aA][dD][iI][oO][^>]*>/( )/g

# <img src="/imag.jpg">   =>  [IMG:/imag.jpg]
#s/< *[iI][mM][gG] *[^>]*[sS][rR][cC] *= *["]*\([^" ]*\)["]*[^>]*>/[IMG:\1]\n/g
s/< *[iI][mM][gG] *[^>]*[sS][rR][cC] *= *["]*\([^" ]*\)["]*[^>]*>//g


#  <script> ... </script> => *nothing*
:ScriptTagUp
/< *[sS][cC][rR][iI][pP][tT].*\/ *[sS][cC][rR][iI][pP][tT] *>.*< *[sS][cC][rR][iI][pP][tT].*\/ *[sS][cC][rR][iI][pP][tT] *>/!b ScriptTagClean
s#\(< *[sS][cC][rR][iI][pP][tT].*/ *[sS][cC][rR][iI][pP][tT] *>.*\)\(< *[sS][cC][rR][iI][pP][tT].*/ *[sS][cC][rR][iI][pP][tT] *>\)#\1#g
b ScriptTagUp
:ScriptTagClean
s#< *[sS][cC][rR][iI][pP][tT].*/ *[sS][cC][rR][iI][pP][tT] *>##g

#  <form ..> ... </form>  => *nothing*
#:FormTagUp
#/< *[fF][oO][rR][mM].*\/ *[fF][oO][rR][mM] *>.*< *[fF][oO][rR][mM].*\/ *[fF][oO][rR][mM] *>/!b FormTagClean
#s#\(< *[fF][oO][rR][mM].*/[fF][oO][rR][mM] *>.*\)\(< *[fF][oO][rR][mM].*/[fF][oO][rR][mM] *>\)#\1#g
#b FormTagUp
#:FormTagClean
#s#< *[fF][oO][rR][mM].*/ *[fF][oO][rR][mM] *>##g

# for now
s/<[^>]*>//g

# we do this in the end to avoid adding confusion to the above
# because > and < can be interpreted as tags

# explicit > 
s/>/>/g
# explicit <
s/</</g

:Poo
p

发表于 : 2006-12-19 16:48
aBiNg

代码: 全选

array_html=(`ls -R directory | grep ".*\.html"`); \
for i in ${array_html[*]}; \
do path_html=`find directory -name $i`; \
/path/to/html2txt $i > ${path_html%/*}/${i%.*}.txt; \
done
又改了一下,生成的*.txt文件与*.html文件应该在同一个目录下。

发表于 : 2006-12-19 17:05
hyfans
多谢楼上,去看看 :)

发表于 : 2006-12-19 17:38
hyfans
强人呀:D

但是有点小问题,在cygwin下,遇到中文文件名说找不到文件。

单独用那个sed脚本 “中文文件名”可以。

发表于 : 2006-12-19 17:51
eexpress
太固执了吧。为什么一定要单独的sed脚本搞定一个问题。和bash组合一下,不是更加方便。

发表于 : 2006-12-19 18:02
hyfans
俺是bash菜鸟,能详细说说么?
eexpress 写了:太固执了吧。为什么一定要单独的sed脚本搞定一个问题。和bash组合一下,不是更加方便。

发表于 : 2006-12-19 21:37
aBiNg
hyfans 写了:但是有点小问题,在cygwin下,遇到中文文件名说找不到文件。

单独用那个sed脚本 “中文文件名”可以。
如果*.html名称中有空格,是有问题的;得先将文件名中的空格处理掉!

发表于 : 2006-12-19 21:57
eexpress
find . -iname "*.html" -exec html2txt {} \;
只是看你的html2txt脚本支持这样传递文件名不。修改下,应该总会可以的。
html2txt如果是纯sed脚本。
find . -iname "*.html" -exec sed -e html2txt {} \;试试。

发表于 : 2006-12-19 22:19
aBiNg
eexpress 写了:find . -iname "*.html" -exec html2txt {} \;
只是看你的html2txt脚本支持这样传递文件名不。修改下,应该总会可以的。
html2txt如果是纯sed脚本。
find . -iname "*.html" -exec sed -e html2txt {} \;试试。
ee;你这个语句用得也太离谱了吧。。。 :roll:
:lol:

发表于 : 2006-12-20 1:42
hyfans
helo_aBiNg 写了:
hyfans 写了:但是有点小问题,在cygwin下,遇到中文文件名说找不到文件。

单独用那个sed脚本 “中文文件名”可以。
如果*.html名称中有空格,是有问题的;得先将文件名中的空格处理掉!
我在想有没有办法让脚本执行的时候自动给每个文件名两边加上""呢?

或者像下面eexpress
说得那么做试试看。

发表于 : 2006-12-20 1:52
hyfans
eexpress 写了:find . -iname "*.html" -exec html2txt {} \;
只是看你的html2txt脚本支持这样传递文件名不。修改下,应该总会可以的。
html2txt如果是纯sed脚本。
find . -iname "*.html" -exec sed -e html2txt {} \;试试。
有效果:

第一个,不过只是列出html文件的内容,没有进行转换。

第2个,sed: -e expression #1, char 2: extra characters after command

发表于 : 2006-12-20 9:24
eexpress
只是看如何把sed -e html2txt包起来而已,用个bash包起来肯定可以。我是随手写的。因为sed调用没用过。