每个人都过得愉快。
请你帮我解决一些文件准备问题:
我有一个文件:
2:1 3:1 4:2 5:1 7:2 34:1 37:3 ...
4:2 6:1 8:1 23:1 25:2 30:1 ...
我想得到:
20002:1 20003:1 20004:2 20005:1 20007:2 20034:1 20037:3 ...
20004:2 20006:1 20008:1 20023:1 20025:2 20030:1 ...
我试过了:
awk '{FS=":"; RS=" "; OFS=":"; ORS=" "}{$1=$1+20000; print $0}'
但它只是部分有效:它不适用于第一行,提供20002:1:3:1:4:2..
,并且不能使用每行的第一个元素,而是4:2 20006:1 20008:1 ...
答案 0 :(得分:4)
您可以使用此功能(仅适用于RT的GNU awk)
awk 'BEGIN{FS=OFS=":";RS="[[:space:]]"}{ORS=RT;$1=$1+20000; print $0}' file
20002:1 20003:1 20004:2 20005:1 20007:2 20034:1 20037:3
20004:2 20006:1 20008:1 20023:1 20025:2 20030:1
解释
BEGIN{
#Only run at start of script
FS=OFS=":"
#Set input and output field separator to :
RS="[[:space:]]"
#Set the record separator to any space character e.g `\n` `\t` or ` `
}
{ORS=RT
#Set the output record separator to whatever was captured by the input one, i.e keep newline space or tab in the right places
$1+=20000; print
#Do your math and print, note that `+=` is shorthand for adding to the current value,
#and also that print can be used on it's own as by default it prints $0(you can also use 1
#at the end of the script as this evaluates to true and the default action if no block
#is defined is to print the current line)
}'
答案 1 :(得分:2)
如果没有@ 123更优雅的解决方案所需的GNU awk:
[Symfony\Component\Debug\Exception\ContextErrorException]
Warning: date_default_timezone_get(): It is not safe to rely on the system'
s timezone settings. You are *required* to use the date.timezone setting or
the date_default_timezone_set() function. In case you used any of those me
thods and you are still getting this warning, you most likely misspelled th
e timezone identifier. We selected the timezone 'UTC' for now, but please s
et date.timezone to select your timezone.