简体   繁体   English

(Mass)文本处理(bash)

[英](Mass) text processing in place (in bash)

How do I process a file or multiple files in place with bash? 如何使用bash处理文件或多个文件?

So: read from file x , do some processing (eg search-replace) and write to file x . 所以:从文件x读取,进行一些处理(例如搜索替换)并写入文件x

I know that with sed you can do: sed -i "" "s/original/replacement/g" , but sometimes sed doesn't cut it and I need a different tool, which lacks an -i "" -like option. 我知道用sed你可以做: sed -i "" "s/original/replacement/g" ,但有时候sed不会削减它,我需要一个不同的工具,它缺少-i ""式选项。

I recently discovered all by myself that I could do the following: 我最近发现自己可以做到以下几点:

( BUFFER="`cat FILENAME`"; echo "$BUFFER" > FILENAME )

which uses an environment variable to store the contents of the file. 它使用环境变量来存储文件的内容。 For very large files this is probably not efficient and environment variables might be limited in capacity. 对于非常大的文件,这可能效率不高,环境变量的容量可能有限。

Is there a different way? 有不同的方式吗?

I also read this answer, but I'm looking for a bash solution. 我也读过这个答案,但我正在寻找一个bash解决方案。 Open source tools that somehow fit the task perfectly are also welcome. 也非常欢迎完全适合任务的开源工具。

There are many scripting tools around like awk, perl, ruby, python, but with large files in bash it would just be better to store the output first on a temporary file. 有很多脚本工具,比如awk,perl,ruby,python,但是在bash中使用大文件,最好先将输出存储在临时文件中。 Then save it back: 然后保存回来:

while IFS= read -r LINE; do
    case "$LINE" in
    something_to_exclude|another_to_exclude)
        ;;
    yet_another_to_exclude)
        ;;
    *)
        # This is fine to include.
        echo "$LINE"
        ;;
    eac
done < "$FILENAME" > "$FILENAME".temp

cat "$FILENAME".temp > "$FILENAME"
rm "$FILENAME".temp

You can store the output of the command in a temp file, and then move the temp file over the original. 您可以将命令的输出存储在临时文件中,然后将临时文件移动到原始文件上。 Example: 例:

command <file >tmp && mv tmp file

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM