linux shell脚本怎么获取目录下所有txt文件名称?

mmerhous 2009-07-09 04:42:27
现在想写一个脚本,把当前目录下所有txt文件的分隔符|替换为|+|,现在我可以把只定文件的字符正确替换了,但是不知道怎么获取文件名称。刚开始学习,还不太明白,还请大家指教。急求急求,谢谢
下面是我写的shell内容:
sed 's/|/|+|/g' $*.txt > 2.txt
mv 2.txt $*.txt
...全文
3693 10 打赏 收藏 转发到动态 举报
写回复
用AI写文章
10 条回复
切换为时间正序
请发表友善的回复…
发表回复
mmerhous 2009-07-16
  • 打赏
  • 举报
回复
非常感谢各位的回答。我实现了替换当前目录下的所有|+|的符号,但是替换后产生了一个问题,靠近|+|的中文出现了乱码,导致整个文件不能导入使用了,还要继续请教~~~
#!/bin/ksh
vi $1 << !!
:1,\$s/\\\//g
:1,\$s/\///g
:1,\$s/ //g
:1,\$s/|+|/|/g
:wq
!!
我尝试用这个方式替换字符但是还是乱码……急求急求
billow_zhang 2009-07-10
  • 打赏
  • 举报
回复

可以在当前目录下,创建一个临时目录, 例如:
mkdir td

然后,执行下面的命令:
find . -name "*.txt" -exec sh -c "sed 's/|/|+|/g' {} >./td/{}" \;

这个时候,所有的输出文件都存放到 ./td目录下了.
你可以使用下面的命令:
mv ./td/* .
将输出文件更换为原来的目录下.

mmerhous 2009-07-10
  • 打赏
  • 举报
回复
billow_zhang 你好:
您的方法我也尝试了,可以实现,谢谢。我正尝试写成脚本
mmerhous 2009-07-10
  • 打赏
  • 举报
回复
cgwu1107 你好:
我就是想写个脚本但是可惜不会。谢谢您给出的脚本。
不过我运行后发现还是不行……报:
bash: ./settxt.sh: bad interpreter: No such file or directory
不明白啊 - - ,再次求教
我的txt文件放在/home/hexin/src/mengtest 这个路径下,脚本中路径写的是:
PATH="/home/hexin/src/mengtest"
又哪里不对吗?
cgwu1107 2009-07-09
  • 打赏
  • 举报
回复
回复楼主:
  可以写个脚本实现,稍描当前路径下所有的.txt 文件,并替换分隔符|替换为|+| 。 假设脚本名为 :sedtxt.sh 内容如下

#!/user/bin/ksh
##########################################
#XXXXX LD.CO
#FUNCTION: 替换文件分隔符
#AUTH: WUCG
#DATE: 2009-07-09
#######################################

#进入指定的目录
PATH="CURR_DIR" #这个路径需要根据需要修改
cd $PATH

for filename in `ls *.txt`
do
sed 's/|/|+|/g' $filename > ${filename}.tmp
mv ${filename}.tmp $filename
done

exit
########### 脚本结束 ############################

脚本结束后,给脚本赋予可执行权限, chmod u+x sedtxt.sh , 然后执行该脚本,就可以了 sh ./sedtxt.sh
祝你好运!!!!
ShowMan 2009-07-09
  • 打赏
  • 举报
回复
哦,升级一下你的sed命令吧,没有-i还是很不方便的。
ShowMan 2009-07-09
  • 打赏
  • 举报
回复
sed -i "s/|/|+|/g" `find . -name *.txt`
mmerhous 2009-07-09
  • 打赏
  • 举报
回复
我这个其实就是批量替换指定目录下的字符串的问题,可是我还真弄不懂了…… 唉
mmerhous 2009-07-09
  • 打赏
  • 举报
回复
谢谢hefuhua :
我用的办法执行后提示:
sed: invalid option -- i
Usage: sed [OPTION]... {script-only-if-no-other-script} [input-file]...

-n, --quiet, --silent
suppress automatic printing of pattern space
-e script, --expression=script
add the script to the commands to be executed
-f script-file, --file=script-file
add the contents of script-file to the commands to be executed
--help display this help and exit
-V, --version output version information and exit

If no -e, --expression, -f, or --file option is given, then the first
non-option argument is taken as the sed script to interpret. All
remaining arguments are names of input files; if no input files are
specified, then the standard input is read.

E-mail bug reports to: bug-gnu-utils@gnu.org .
Be sure to include the word ``sed'' somewhere in the ``Subject:'' field.
hefuhua 2009-07-09
  • 打赏
  • 举报
回复
sed -i 's/|/|+|/g' $*.txt
试试这个-i参数

23,115

社区成员

发帖
与我相关
我的任务
社区描述
Linux/Unix社区 应用程序开发区
社区管理员
  • 应用程序开发区社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧