awk + 更改列值并保留双引号
awk + change column value and keep double quotes
我有这个数据:
$ head test
"Rec Open Date","MSISDN","IMEI"
"2015-01-04",100,200
"2015-01-04",100,200
"2015-01-04",100,200
"2015-01-04",100,200
"2015-01-04",100,200
"2015-01-04",100,200
"2015-01-04",100,200
"2015-01-04",100,200
"2015-01-04",100,200
我可以使用此命令更改第一列的值:
$ awk -F, 'NR>1{="2015-01-05"}1' OFS=, test > tmpfile && mv tmpfile test
用这个命令我松了双引号,我想保留双引号。可以修改此命令以实现此目的吗?
$ head test
"Rec Open Date","MSISDN","IMEI"
2015-01-05,100,200
2015-01-05,100,200
2015-01-05,100,200
2015-01-05,100,200
2015-01-05,100,200
2015-01-05,100,200
2015-01-05,100,200
2015-01-05,100,200
2015-01-05,100,200
来自 GNU Awk 用户指南;
\" A literal double quote (necessary for string constants only). This
sequence is used when you want to write a string constant that
contains a double quote. Because the string is delimited by double
quotes, you need to escape the quote that is part of the string, in
order to tell awk to keep processing the rest of the string.
$ awk -F, 'NR>1{="\"2015-01-05\""}1' OFS=, test > tmpfile && mv tmpfile test
$ head test
"Rec Open Date","MSISDN","IMEI"
"2015-01-05",100,200
"2015-01-05",100,200
"2015-01-05",100,200
"2015-01-05",100,200
"2015-01-05",100,200
"2015-01-05",100,200
"2015-01-05",100,200
"2015-01-05",100,200
"2015-01-05",100,200
并不是你是"losing"他们,而是</code>最初是<code>"something"
,你用another thing
代替了。由于此 another thing
是一个字符串,因此您使用双引号来注释它。因此,要恢复双引号,您需要在替换时注明。
这可以更干净:
awk -v new='"2015-01-05"' 'BEGIN {FS=OFS=","} NR>1{=new}1' file
^^^^^^^^^^^^^^^^^^^^^ ^
|---------------------------------------------|
we provide the var ............... and we replace with it
surrounded by "
我有这个数据:
$ head test
"Rec Open Date","MSISDN","IMEI"
"2015-01-04",100,200
"2015-01-04",100,200
"2015-01-04",100,200
"2015-01-04",100,200
"2015-01-04",100,200
"2015-01-04",100,200
"2015-01-04",100,200
"2015-01-04",100,200
"2015-01-04",100,200
我可以使用此命令更改第一列的值:
$ awk -F, 'NR>1{="2015-01-05"}1' OFS=, test > tmpfile && mv tmpfile test
用这个命令我松了双引号,我想保留双引号。可以修改此命令以实现此目的吗?
$ head test
"Rec Open Date","MSISDN","IMEI"
2015-01-05,100,200
2015-01-05,100,200
2015-01-05,100,200
2015-01-05,100,200
2015-01-05,100,200
2015-01-05,100,200
2015-01-05,100,200
2015-01-05,100,200
2015-01-05,100,200
来自 GNU Awk 用户指南;
\" A literal double quote (necessary for string constants only). This sequence is used when you want to write a string constant that contains a double quote. Because the string is delimited by double quotes, you need to escape the quote that is part of the string, in order to tell awk to keep processing the rest of the string.
$ awk -F, 'NR>1{="\"2015-01-05\""}1' OFS=, test > tmpfile && mv tmpfile test
$ head test
"Rec Open Date","MSISDN","IMEI"
"2015-01-05",100,200
"2015-01-05",100,200
"2015-01-05",100,200
"2015-01-05",100,200
"2015-01-05",100,200
"2015-01-05",100,200
"2015-01-05",100,200
"2015-01-05",100,200
"2015-01-05",100,200
并不是你是"losing"他们,而是</code>最初是<code>"something"
,你用another thing
代替了。由于此 another thing
是一个字符串,因此您使用双引号来注释它。因此,要恢复双引号,您需要在替换时注明。
这可以更干净:
awk -v new='"2015-01-05"' 'BEGIN {FS=OFS=","} NR>1{=new}1' file
^^^^^^^^^^^^^^^^^^^^^ ^
|---------------------------------------------|
we provide the var ............... and we replace with it
surrounded by "