![](/img/trans.png)
[英]How to replace a column of a CSV file with lines from another file in BASH?
[英]bash script replace all null values in a file by lines from another file
我有兩個文件:
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 ,而另一行是file2.txt
的相應行
我該如何在sed
或awk
?
OLD="null"
NEW="abc"
sed -i 's/$OLD/$NEW/g' file.txt
這是我的嘗試,但非常基礎,無法繼續。
在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.