English 中文(简体)
在UNIX中分析CSV文件,同时处理“”中的数据
原标题:Parsing a CSV file in UNIX , but also handling data within " "
  • 时间:2011-05-29 11:38:01
  •  标签:
  • unix

I am trying to parse a CSV file in UNIX using AWK or shell scripting. But I am facing a issue here. If the data is within quotes(",") then I want to replace the comma(,) with a blank space and remove the quotes. Also , such data might occur multiple times in one single record.

例如:考虑这个输入

20,曼彻斯特,“巴克莱,联赛”,xyz,123,“95,一些,数据”,

输出应该如下

20,曼彻斯特,巴克莱联赛,xyz,123,95一些数据,

How can it be done b basic UNIX commands or scripting. Please help me on this ....

问题回答
<input.csv python -c 
 import csv,sys;f=csv.reader(sys.stdin);print  
 ("
".join(",".join(entry.replace(",", " ") for entry in line) for line in f)) 

以下是在shell中使用sed的方法:

sed -i  .orig  -e  :a  -e  s/^([^"]*)"([^,"]*)"(.*)$/123/g  
-e  s/^([^"]*)"([^,"]*),([^"]*)"(.*)$/1"2 3"4/;ta  file.csv




相关问题
Generate assembler code from C file in linux

I would like to know how to generate assembler code from a C program using Unix. I tried the gcc: gcc -c file.c I also used firstly cpp and then try as but I m getting errors. I m trying to build an ...

Function to create the array by reading the file

I am creating scripts which will store the contents of pipe delimited file. Each column is stored in a separate array. I then read the information from the arrays and process it. There are 20 pipe ...

Compare characters at the end of the string C++

This program supposed to find command line arguments entered on Unix which ends with “.exe”. For some reason it doesn t work. Here is the code: int main( int argc, char* argv[] ) { for ( int ...

Batch Job Dependencies Using Open Source/Free Software

I run a large data warehouse plant where we have a lot of nightly jobs running concerruently however many have dependencies on a extract or data load process before they start. Currently we use an ...

Writing application for both Unix and Windows

I ll write a program for Interactive UNIX (http://en.wikipedia.org/wiki/INTERACTIVE_UNIX). But in a year it will be ported to Windows. I ll write it in ANSI C and/or SH-script. When it runs on Windows ...

Development Environment in Windows [closed]

What are your recommendations for setting up a development environment in Windows, especially when not using an IDE. I am attempting to familiarize myself with Windows, and I feel a bit lost. What ...

热门标签