[英]bash script replace all null values in a file by lines from another file
I have two files: 我有两个文件:
File1.sql
here is the few first lines: 以下是第一行:
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');
and another file2.txt
: 和另一个file2.txt
:
'this is test'
'another example'
'third one'
they both have the same number of lines. 它们都具有相同的行数。 I want to replace null string in file1.sql
, we have only one null in each line, with the corresponding line from the other file2.txt
我想替换file1.sql
空字符串,我们每行只有一个null ,而另一行是file2.txt
的相应行
How can I do it in sed
or awk
? 我该如何在sed
或awk
?
OLD="null"
NEW="abc"
sed -i 's/$OLD/$NEW/g' file.txt
this is my try, but very basic and cannot go further. 这是我的尝试,但非常基础,无法继续。
In awk, pretty much complete rewrite as &
in a[FNR]
when using sub
causes problems. 在awk中,使用sub
时几乎完全用&
a[FNR]
&
重写会导致问题。
$ 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');
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.