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,不知道有没有可以直接写回文件的命令?请指导!
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号
改用python吧
建议加上sort命令
cat $file | uniq| sort -o $file
配合zsh使用
覆写文件可以借助 tee 命令,修改后代码如下
1首先准备下环境
三份txt内容都是一样的。
2尝试下这个答案的命令http://askubuntu.com/questions/528658/redirect-pipe-output-to-the-same...
sort -u带有uniq的意思。3.还需要调整。
只修改第七行的最后一个重定向即可,
tee可以做 pipe fitting,详见手册