将分隔符 txt 文件中的多行转换为一行

将分隔符 txt 文件中的多行转换为一行

我有如下输入文件

ID~NAME~CREATED_DATE~NOTES~LAST_MODIFIED_DATE
"12345"~"abc"~"9/7/2022 10:05:18 AM"~"new patiant"~"9/7/2022 11:52:18 AM"
"25451"~"bdc"~"11/7/2022 10:05:18 AM"~"next 
month 
visit"~"11/7/2022 10:05:18 AM"
"45522"~"xyz"~"1/8/2022 11:05:18 AM"~"new visiting patient"~"1/8/2022 11:05:18 AM"
"52447"~"pqr"~"5/5/2022 10:05:18 AM"~"transferred
back 
to 
hospital"~"5/5/2022 10:05:18 AM"
"24541"~"rds"~"4/5/2022 05:05:18 AM"~"new patient"~"4/5/2022 05:05:18 AM"

下面是我想要的输出:

ID~NAME~CREATED_DATE~NOTES~LAST_MODIFIED_DATE
"12345"~"abc"~"9/7/2022 10:05:18 AM"~"new patiant"~"9/7/2022 11:52:18 AM"
"25451"~"bdc"~"11/7/2022 10:05:18 AM"~"next month visit"~"11/7/2022 10:05:18 AM"
"45522"~"xyz"~"1/8/2022 11:05:18 AM"~"transferred back to hospital"~"1/8/2022 11:05:18 AM"
"52447"~"pqr"~"5/5/2022 10:05:18 AM"~"new visiting patient"~"5/5/2022 10:05:18 AM"
"24541"~"rds"~"4/5/2022 05:05:18 AM"~"new patient"~"4/5/2022 05:05:18 AM"

请帮忙 !

答案1

使用 GNU awk 进行多字符 RS 和 RT:

$ awk -v RS='([^~]+~){4}[^~]+\n' '{print gensub(/[[:space:]]+/," ","g",RT)}' file
ID~NAME~CREATED_DATE~NOTES~LAST_MODIFIED_DATE
"12345"~"abc"~"9/7/2022 10:05:18 AM"~"new patiant"~"9/7/2022 11:52:18 AM"
"25451"~"bdc"~"11/7/2022 10:05:18 AM"~"next month visit"~"11/7/2022 10:05:18 AM"
"45522"~"xyz"~"1/8/2022 11:05:18 AM"~"new visiting patient"~"1/8/2022 11:05:18 AM"
"52447"~"pqr"~"5/5/2022 10:05:18 AM"~"transferred back to hospital"~"5/5/2022 10:05:18 AM"
"24541"~"rds"~"4/5/2022 05:05:18 AM"~"new patient"~"4/5/2022 05:05:18 AM"

相关内容