gpt4 book ai didi

awk - 如何在awk中手动设置记录分隔符?

转载 作者:行者123 更新时间:2023-12-04 22:56:52 25 4
gpt4 key购买 nike

我有一个像下面这样的文件 -

vipin kumar ........................ kumar ......bangalore
something something .......
;
vipin kumar ........................ kumar ......bangalore
something something .......(testing
)
;
vipin kumar ......................... kumar .....bangalore
something something ;

我想要像下面这样的输出(文件中的名称和数字可以不同,但​​唯一的共同点是 - 当我们有“;”时,行就结束了)
vipin kumar ........................ kumar ......bangalore   something something .......;
vipin kumar ........................ kumar ......bangalore something something .......(testing);
vipin kumar ......................... kumar .....bangaloresomething something ;

I want to set the RS to ";" .

我尝试了以下命令 -
awk '{ORS=(NR%2==0?RS:FS)}1' file.txt

但它没有给出正确的输出,因为
NR%2 or NR%3 不起作用,因为我不确定在多少行后我会得到 ;
然后我尝试将 RS 设置为 ;下面使用
awk '{for(i=1;i<=NF;i++) (ORS=(if($i ~ /;/?RS:FS);break}1' file.txt

但是这个命令不起作用。

最佳答案

输入

$ cat f
vipin kumar ........................ kumar ......bangalore
something something .......
;
vipin kumar ........................ kumar ......bangalore
something something .......(testing
)
;
vipin kumar ......................... kumar .....bangalore
something something ;

输出
$ awk 'ORS=/;/?RS:FS'  f
vipin kumar ........................ kumar ......bangalore something something ....... ;
vipin kumar ........................ kumar ......bangalore something something .......(testing ) ;
vipin kumar ......................... kumar .....bangalore something something ;

关于awk - 如何在awk中手动设置记录分隔符?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41957409/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com