祝大家有美好的一天。
请您帮我解决一些文件准备问题:
我有一个文件:
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 ...
Run Code Online (Sandbox Code Playgroud)
我想得到:
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 ...
Run Code Online (Sandbox Code Playgroud)
我试过:
awk '{FS=":"; RS=" "; OFS=":"; ORS=" "}{$1=$1+20000; print $0}'
Run Code Online (Sandbox Code Playgroud)
但它只能部分工作:它不适用于第一行,给出20002:1:3:1:4:2..
,并且不适用于每行的第一个元素,给出4:2 20006:1 20008:1 ...
你可以使用这个(GNU awk 仅适用于 RT)
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
Run Code Online (Sandbox Code Playgroud)
解释
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)
}'
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
348 次 |
最近记录: |