我有多个文本如下的文件。
文件1的内容
Begin("tmp1","derived from file \"/disks/setup/ORIG_FILE1.txt\"");
文件2的内容:
Begin("tmp1","derived from file \"/disks/setup/ORIG_FILE2.txt\"");
文件3的内容:
Begin("tmp9","derived from file \"/disks/setup/ORIG_FILE4.txt\"");
以此类推.....
perl或sed中是否有任何命令来查找带有“ Begin”的行,并在该行中搜索“ ...”(即本例中的“ tmp1”)之间的第一个字符串并将其存储在变量中,然后搜索/ .. \“”之间的最后一个字符串(即ORIG_FILE1.txt),并将其存储在变量2中,然后从变量2中删除“ .txt”,然后将变量1替换为变量2并更新文件。
所以输出文件如下:
文件1的内容
Begin("ORIG_FILE1","derived from file \"/disks/setup/ORIG_FILE1.txt\"");
文件2的内容:
Begin("ORIG_FILE2","derived from file \"/disks/setup/ORIG_FILE2.txt\"");
文件3的内容:
Begin("ORIG_FILE4","derived from file \"/disks/setup/ORIG_FILE4.txt\"");
以此类推.....
有人可以在这里帮助我吗?
答案 0 :(得分:0)
尝试一下:
perl -i.bak -pe '
if (/.*\/(.+)\.txt/) {
$file = $1;
s/(?<=").*?(?=")/$file/
}
' file1 file2 file3 ...
这将首先捕获“ orig”文件名(假设它们都具有“ .txt”扩展名),然后替换“ tmp”占位符。
答案 1 :(得分:0)
在任何UNIX盒中的任何外壳中使用任何sed:
$ sed 's/\([^"]*"\)[^"]*\(.*\/\([^.]*\)\)/\1\3\2/' file
Begin("ORIG_FILE1","derived from file \"/disks/setup/ORIG_FILE1.txt\"");
Begin("ORIG_FILE2","derived from file \"/disks/setup/ORIG_FILE2.txt\"");
Begin("ORIG_FILE4","derived from file \"/disks/setup/ORIG_FILE4.txt\"");
答案 2 :(得分:0)
通过gnu sed作为您在“ d”中的数据,
sed -E 's~^(Begin\(")\w+(".+/)(\w+)(\.\w+\\""\);)~\1\3\2\3\4~' d