awk无法删除特定列中的新行字符

时间:2017-04-18 06:23:09

标签: awk

我正在尝试使用awk删除csv文件第一列中的换行符,但它似乎无法正常工作

示例文件:

  "This
  is a test
  ","Something","Something"
  "This is
  another
  test","something","something"
  "One
  more
  test","something","something"

我正在使用的命令是

awk -F, '{gsub("\n","",$1); print}' sample

输出不会删除换行符

我正在寻找使用awk not sed或perl的解决方案

有人可以帮忙吗?

所需的输出是,

"This is a test","something","something"
"This is another test","something","something"
"One more test","something","something"

2 个答案:

答案 0 :(得分:1)

假设您拥有的是从Excel或其他一些Windows工具导出的CSV(因为它看起来像是什么),所以它有qpi_partition = [[0 for x in range(len(partition))] for y in range(len(dimension))] for d, r in enumerate(dimension): for p, c in enumerate(partition): result = # Calculate result.... qpi_partition[r][c] = result 行结尾,所有你需要的就是GNU awk对于多字符RS:

\r\n

否则使用GNU awk进行多字符RS,这适用于您发布的样本:

$ awk -v RS='\r\n' -F'\n' '{$1=$1}1' file
"This is a test ","Something","Something"
"This is another test","something","something"
"One more test","something","something"

答案 1 :(得分:0)

尝试:

awk '{printf("%s ",$0)} END{print ""}'   Input_file

只需使用printf打印所有带空格的文件,然后最后打印一个NULL值,最后得到新行。