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

sh/bash/dash/ksh/zsh等Shell脚本
回复
hyfans
帖子: 14
注册时间: 2006-08-11 14:01

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

#1

帖子 hyfans » 2006-12-19 15:27

找到个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
aBiNg
帖子: 1331
注册时间: 2006-07-09 12:22
来自: 南京

#2

帖子 aBiNg » 2006-12-19 16:48

代码: 全选

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文件应该在同一个目录下。
上次由 aBiNg 在 2006-12-19 17:12,总共编辑 2 次。
hyfans
帖子: 14
注册时间: 2006-08-11 14:01

#3

帖子 hyfans » 2006-12-19 17:05

多谢楼上,去看看 :)
hyfans
帖子: 14
注册时间: 2006-08-11 14:01

#4

帖子 hyfans » 2006-12-19 17:38

强人呀:D

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

单独用那个sed脚本 “中文文件名”可以。
头像
eexpress
帖子: 58428
注册时间: 2005-08-14 21:55
来自: 长沙

#5

帖子 eexpress » 2006-12-19 17:51

太固执了吧。为什么一定要单独的sed脚本搞定一个问题。和bash组合一下,不是更加方便。
● 鸣学
hyfans
帖子: 14
注册时间: 2006-08-11 14:01

#6

帖子 hyfans » 2006-12-19 18:02

俺是bash菜鸟,能详细说说么?
eexpress 写了:太固执了吧。为什么一定要单独的sed脚本搞定一个问题。和bash组合一下,不是更加方便。
aBiNg
帖子: 1331
注册时间: 2006-07-09 12:22
来自: 南京

#7

帖子 aBiNg » 2006-12-19 21:37

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

单独用那个sed脚本 “中文文件名”可以。
如果*.html名称中有空格,是有问题的;得先将文件名中的空格处理掉!
头像
eexpress
帖子: 58428
注册时间: 2005-08-14 21:55
来自: 长沙

#8

帖子 eexpress » 2006-12-19 21:57

find . -iname "*.html" -exec html2txt {} \;
只是看你的html2txt脚本支持这样传递文件名不。修改下,应该总会可以的。
html2txt如果是纯sed脚本。
find . -iname "*.html" -exec sed -e html2txt {} \;试试。
● 鸣学
aBiNg
帖子: 1331
注册时间: 2006-07-09 12:22
来自: 南京

#9

帖子 aBiNg » 2006-12-19 22:19

eexpress 写了:find . -iname "*.html" -exec html2txt {} \;
只是看你的html2txt脚本支持这样传递文件名不。修改下,应该总会可以的。
html2txt如果是纯sed脚本。
find . -iname "*.html" -exec sed -e html2txt {} \;试试。
ee;你这个语句用得也太离谱了吧。。。 :roll:
:lol:
hyfans
帖子: 14
注册时间: 2006-08-11 14:01

#10

帖子 hyfans » 2006-12-20 1:42

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

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

或者像下面eexpress
说得那么做试试看。
hyfans
帖子: 14
注册时间: 2006-08-11 14:01

#11

帖子 hyfans » 2006-12-20 1:52

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
头像
eexpress
帖子: 58428
注册时间: 2005-08-14 21:55
来自: 长沙

#12

帖子 eexpress » 2006-12-20 9:24

只是看如何把sed -e html2txt包起来而已,用个bash包起来肯定可以。我是随手写的。因为sed调用没用过。
● 鸣学
回复