cd \txt
filelist=`ls`
for file in $filelist
do
>temp.txt
echo $file
cat $file | uniq >> temp.txt
>$file
cat temp.txt >> $file
done
如上面代码所示:功能需求是将该目录下所有文件进行cat uniq操作之后保存回该文件,我使用了temp.txt,不知道有没有可以直接写回文件的命令?请指导!
cd \txt
filelist=`ls`
for file in $filelist
do
>temp.txt
echo $file
cat $file | uniq >> temp.txt
>$file
cat temp.txt >> $file
done
如上面代码所示:功能需求是将该目录下所有文件进行cat uniq操作之后保存回该文件,我使用了temp.txt,不知道有没有可以直接写回文件的命令?请指导!
bash
cd \txt filelist=`ls` for file in $filelist do echo $file cat $file | uniq - $file done
覆写文件可以借助 tee 命令,修改后代码如下
shell
cd /txt filelist=`ls` for file in $filelist do cat $file|uniq|tee $file>/dev/null done
1首先准备下环境
{ txt } » pwd ~/txt
/home/Honwhy/txt
{ txt } » ls ~/txt
1.txt 2.txt 3.txt
{ txt } » cat 1.txt ~/txt
abcd
abcd
abc
abcde
abc
a
b
c
三份txt内容都是一样的。
2尝试下这个答案的命令http://askubuntu.com/questions/528658/redirect-pipe-output-to-the-same...
filelist=`ls`
for file in $filelist
do
sort -u $(pwd)/$file | tee $(pwd)/$file >/dev/null
done
sort -u
带有uniq
的意思。
3.还需要调整。
cd \txt
filelist=`ls`
for file in $filelist
do
>temp.txt
echo $file
cat $file | uniq | tee temp.txt
>$file
cat temp.txt >> $file
done
只修改第七行的最后一个重定向即可,tee
可以做 pipe fitting,详见手册
7 回答5.3k 阅读
4 回答4k 阅读
2 回答5.9k 阅读✓ 已解决
2 回答2.5k 阅读✓ 已解决
1 回答2.3k 阅读✓ 已解决
2 回答794 阅读✓ 已解决
2 回答3.2k 阅读
改用python吧