我在以下格式中具有以下文件中的数据
给出"253539","","Company1 Name
","0","1"
"255229","","Ramu ","0","1"
"253548","","Mater Name/Id
","0","1"
"255229","","Ram Lakh","0","1"
"253619","","CHild Name/Ids
","0","1"
每行都附加了 n。
在shell脚本中如何替换 n",",以获取输出为
"253539","","Company1 Name/Id ","0","1"
"255229","","Ramu ","0","1"
"253548","","Mater Name/Id ","0","1"
"255229","","Ram Lakh","0","1"
"253619","","CHild Name/Id ","0","1"
需要自动化一个过程,因此必须在shell脚本中写入。
请帮助。
不确定要在外壳中轻松执行此操作,但是我做了一个抛出的perl脚本来完成:
perl -e 'foreach (<>) { chop; print; print "n" if /"$/ }' < mydata.csv
它仅在 "
中的线结束时输出新线来起作用。
使用awk
您可以做:
$ awk '/"$/ {print a$0; a=""; next} {a=$0}' file
"253539","","Company1 Name ","0","1"
"255229","","Ramu ","0","1"
"253548","","Mater Name/Id ","0","1"
"255229","","Ram Lakh","0","1"
"253619","","CHild Name/Ids ","0","1"
它的作用是将其存储在a
中,以防该行不会以"
结束。如果这样做,它将打印存储的线加上当前的线。
可以做
之类的事情#!/bin/bash
while read -r line; do
[[ $line =~ '"'$ ]] && echo "$line" || echo -n "$line"
done < file
例如。在您的输入文件
上> ./abovescript
"253539","","Company1 Name","0","1"
"255229","","Ramu ","0","1"
"253548","","Mater Name/Id","0","1"
"255229","","Ram Lakh","0","1"
"253619","","CHild Name/Ids","0","1"
如果可以接受 @fedorqui的答案,则是较短的代码。
awk '{printf $0 (/"$/?RS:X)}' file