我有一个这样的文件:
123456789012345lksdlkssalkd
skdañsldkasñldkas+
slkdñlaskd aslda lkl
012345678901234dkfskadflkdflkasdf
ASLÑsa sLL SLD SdsDA
SLDALDK LÑD
SDFSADF
d
345678901234560 DÑLSAD SDLÑSlkfl fg fg kfglkfgklfd gf
lfdld
我希望得到这个结果,从每个以15个数字开头的行开始,并用空格替换中间行结尾:
123456789012345lksdlkssalkd skdañsldkasñldkas+ slkdñlaskd aslda lkl
012345678901234dkfskadflkdflkasdf ASLÑsa sLL SLD SdsDA SLDALDK LÑD SDFSADF d
345678901234560 DÑLSAD SDLÑSlkfl fg fg kfglkfgklfd gf lfdld
有可能吗?
答案 0 :(得分:2)
你可以这样做:
$ awk '/^[[:digit:]]{15}/{if (buf) {print buf} buf=$0; next}
{buf=buf FS $0}
END {print buf}' file
123456789012345lksdlkssalkd skdañsldkasñldkas+ slkdñlaskd aslda lkl
012345678901234dkfskadflkdflkasdf ASLÑsa sLL SLD SdsDA SLDALDK LÑD SDFSADF d
345678901234560 DÑLSAD SDLÑSlkfl fg fg kfglkfgklfd gf lfdld
答案 1 :(得分:1)
sed
的帮助下,可以简化
$ sed -r 's/[0-9]{15}/\n&/' file |
awk -v RS= -v ORS='\n' '$1=$1'
123456789012345lksdlkssalkd skdañsldkasñldkas+ slkdñlaskd aslda lkl
012345678901234dkfskadflkdflkasdf ASLÑsa sLL SLD SdsDA SLDALDK LÑD SDFSADF d
345678901234560 DÑLSAD SDLÑSlkfl fg fg kfglkfgklfd gf lfdld
答案 2 :(得分:0)
这可能适合你(GNU sed):
sed -r ':a;N;/\n[0-9]{15}/!s/\n/ /;ta;P;D' file
用空格替换换行符,除非该换行符的前缀为15个字符。
答案 3 :(得分:0)
我最喜欢我的解决方案:没有分支,没有sed,
awk '/^[0-9]{15}/ && NR > 1 {sep="\n"} \
{printf "%s%s", sep, $0; sep = " "} END {print ""}' dat
123456789012345lksdlkssalkd skdañsldkasñldkas+ slkdñlaskd aslda lkl
012345678901234dkfskadflkdflkasdf ASLÃsa sLL SLD SdsDA SLDALDK LÃD SDFSADF d
345678901234560 DÃLSAD SDLÃSlkfl fg fg kfglkfgklfd gf lfdld