[英]Bash script optimization: lines copy
I have a working script but it is quite slow on large files.我有一个工作脚本,但它在处理大文件时速度很慢。 It simply copy a bunch of line from a file to another file at a given position. Here the script:
它只是将一堆行从一个文件复制到给定 position 处的另一个文件。这里是脚本:
#!/bin/bash
source_file="..."
dest_file="..."
first_line_to_copy=...
last_line_to_copy=...
dest_line=...
cat "$source_file" | sed -n "$first_line_to_copy,$last_line_to_copy p" | while IFS= read -r line;
do
sed -i "$dest_line i \\$line" $dest_file
dest_line=$(($dest_line+1))
done
I didn't succeed to manage the copy in one sed command.我没有成功地用一个 sed 命令管理副本。 So it always opens/closes destination file, seeks at dest_line which it really time consumming with a huge number of line.
所以它总是打开/关闭目标文件,在 dest_line 寻找它真正消耗大量行的时间。
Thanks for any help谢谢你的帮助
First copy the lines in the range [$first_line_to_copy,$last_line_to_copy]
to a temporary file and then insert that file to the specified location:首先将
[$first_line_to_copy,$last_line_to_copy]
范围内的行复制到一个临时文件,然后将该文件插入到指定位置:
tempfile=/tmp/tempfile.$$
sed -n "$first_line_to_copy,$last_line_to_copy p" "$source_file" > "$tempfile"
sed -i.backup "$dest_line r $tempfile" "$dest_file"
rm -f "$tempfile"
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.