在文件的开头收集标号 [英] Collect numerals at the beginning of the file
本文介绍了在文件的开头收集标号的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个包含一些数字的文本文件,例如,
I have a text file which contains some numerals, for example,
There are 60 nuts and 35 apples,
but only 24 pears.
我想在一开始收集这些数字(60,35,24)的相同文件,特别是,我想处理后,读取文件
I want to collect these numerals (60, 35, 24) at the beginning of the same file, in particular, I want after processing, the file to read
read "60"
read "35"
read "24"
There are 60 nuts and 35 apples,
but only 24 pears.
我怎么能这样做的文字操作可通行费的* nix这个使用一个?
How could I do this using one of the text manipulating tolls available in *nix?
推荐答案
您可以脚本的编辑会议上编辑一个文件:
You can script an ed session to edit the file in place:
{
echo 0a # insert text at the beginning of the file
grep -o '[0-9]\+' nums.txt | sed 's/.*/read "&"/'
echo ""
echo . # end insert mode
echo w # save
echo q # quit
} | ed nums.txt
更简洁:
printf "%s\n" 0a "$(grep -o '[0-9]\+' nums.txt|sed 's/.*/read "&"/')" "" . w q | ed nums.txt
这篇关于在文件的开头收集标号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文