[英]Replace lines in text file inside tar file with sed and regular expressions
File b.txt
in a tar file b.tar
contains lines such as these: 文件
b.txt
在tar文件b.tar
包含线,如这些:
dir1 => /dir1A
dir2 => /dir2A
I would like a concise expression for replacing it inside the tar file with a version with these lines instead: 我想要一个简洁的表达式,将其替换为tar文件中的这些版本的版本:
dir1 = /dir1B
dir2 = /dir2B
The expression may rely on the fact that a copy of b.txt
exists in the same directory as a.tar
. 表达可能依赖于副本的事实
b.txt
在同一个目录中存在a.tar
。
My sed
skills are a bit rustly. 我的
sed
技能有些生锈。 I've tried the following but it does not quite work yet. 我已经尝试了以下方法,但是还不能正常工作。 What is my mistake?
我怎么了 Is there an even more concise version?
有没有更简洁的版本?
tar df a.tar b.txt
echo b.txt |
sed \#^dir1[ \t]*=>.*$#dir1 => /dir1B# |
sed \#^dir2[ \t]*=>.*$#dir2 => /dir2B# |
tar af a.tar -
This (is a bit less concise than I had originally hoped for but) apparently does the trick: 这(比我最初希望的要简洁一些,但是)显然可以解决问题:
TMP_DIR=`mktemp -d`
cat b.txt | \
sed 's#^\(dir1A\s*=\s*\)\(.*\)\(\s*\)$#\1/dir1B\3#' | \
sed 's#^\(dir2A\s*=\s*\)\(.*\)\(\s*\)$#\1/dir2B\3#' > \
$TMP_DIR/b.txt
tar fv a.tar --delete ./b.txt
tar fv a.tar -C $TMP_DIR --append ./b.txt
rm -fr $TMP_DIR
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.