我有两个文件:
File1.sql
这里是少数第一行:
insert into comment (id, field1, field2, field3, date) values (8001, 13, 44, null, '2007-07-06 02:48:15');
insert into comment (id, field1, field2, field3, date) values (8002, 18, 738, null, '2008-12-23 16:30:17');
insert into comment (id, field1, field2, field3, date) values (8003, 3, 150, null, '2007-06-07 06:27:52');
和另一个file2.txt
:
'this is test'
'another example'
'third one'
他们都有相同的行数。
我想替换file1.sql
中的 null 字符串,每行只有一个 null ,其他file2.txt
对应的行
如何在sed
或awk
?
OLD="null"
NEW="abc"
sed -i 's/$OLD/$NEW/g' file.txt
这是我的尝试,但非常基本,不能再进一步。
答案 0 :(得分:1)
在awk中,使用&
导致问题时a[FNR]
中的sub
几乎完全重写。
$ awk '
NR==FNR { a[FNR]=$0; next } # hash the records to a
{
for(i=1;i<=NF;i++) # iterate every field
if(tolower($i)~/^null,?$/) { # if its null (or null,)
$i=a[FNR]($i~/,$/?",":"") # replace it
break # only once
}
}1' file2.txt File1.sql
insert into comment (id, field1, field2, field3, date) values (8001, 13, 44, 'this is test', '2007-07-06 02:48:15');
insert into comment (id, field1, field2, field3, date) values (8002, 18, 738, 'another example', '2008-12-23 16:30:17');
insert into comment (id, field1, field2, field3, date) values (8003, 3, 150, 'third one', '2007-06-07 06:27:52');