Change the delimiter from Comma to Pipeline


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Change the delimiter from Comma to Pipeline
# 8  
Old 12-17-2012
Slight variation:
Code:
awk 'NR%2{gsub(/,/,"|")}1' RS=\" ORS= file

# 9  
Old 12-17-2012
Quote:
Originally Posted by Arun Mishra
Just a small help if the script can erase the double qoutes after the file has been converted to pipeline.
Code:
awk -F '"' 'NF>1{ for(i=1;i<=NF;i+=2){gsub(",","|",$i)}}
NF==1{gsub(",","|")}1' OFS="" file

Quote:
Originally Posted by Arun Mishra
And if you have time please can you explain how the script works. Smilie
Code:
awk -F '"' 'NF>1{ for(i=1;i<=NF;i+=2){gsub(",","|",$i)}}    # For FS='"' (doble quotes), If NF(number of fields) is more than 1 then use for loop and replace "," with "|" for all odd entries of fields.
Odd entries marked as Red.
Menstrain,A0022699,"Woodstock,IL,2",A0013195,"Rockford,IL"

NF==1{gsub(",","|")}1' OFS='"' file   # When file don't have any FS('"') in it then NF==1. then replace all the entries of "," with "|". and Set OFS='"' to regain all double quotes.

@Scrutinizer - That's cool piece of awk with the help RS and ORS.. Smilie
# 10  
Old 12-17-2012
Thanks very much Pamu.... for taking your time and explaining.
@Scrutinizer... great work.. seems you guys a Einstein of awk.
Thanks very much for the help. I have one more script to be created .. can i raise a different thread or continue in this? Sorry i am pretty new to this very helpful website..
# 11  
Old 12-17-2012
Hi Arun, you would need to create a new thread for a new problem..
# 12  
Old 12-17-2012
Ok Thanks @Scrutinizer.. And can you let me know what's the difference between your code and Pamu's code?
# 13  
Old 12-17-2012
The difference is that Pamu uses a double quote to split fields in a record or line (FS=\"), and a for loop to go to every odd field in a record and remove the commas, whereas my version uses RS=\" to split the input file into small records and only removes the commas in odd records (NR%2), which are the records that lie outside the double quotes...


--
@pamu: thanks Smilie
# 14  
Old 12-17-2012
Code:
urx02:mmadusr:/development/mma/az/data> awk 'NR%2{gsub(/,/,"|")}1' RS=\" ORS= DCIA_C150_R01_PLN_SEG_DATA.TXT > pp1.txt
awk: Input line
0000030075,IPA,4,AE cannot be longer than 3,000 bytes.
 The input line number is 44. The file is DCIA_C150_R01_PLN_SEG_DATA.TXT.
 The source line number is 1.

Sample records
Code:
0000000000,,3,MAIL ORDER,MAIL ORDER DATA,N/A,MAIL ORDER,N/A,
0000010001,CASH,1,CASH,CASH,N/A,CASH,N/A,
0000020002,FFS MED,2,AL MEDICAID,MEDICAID,N/A,AL MEDICAID,N/A,"Tier2,PA,NPF"
0000020003,FFS MED,2,ALASKA MEDICAID,MEDICAID,N/A,ALASKA MEDICAID,N/A,"Tier1,ST,PA"

Please find above the error i got while i used your code @Scrutinizer. may be i have not used it properly Smilie
Please help me out with this

---------- Post updated at 04:27 PM ---------- Previous update was at 04:03 PM ----------

It seems may be because i am using OS HP-UX . If any other reason please specify.

Last edited by Scrutinizer; 12-17-2012 at 08:06 AM.. Reason: icode tags =====>>>>>>>>> code tags
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Insert a new column with sequence number (Delimiter as comma)

Hi All, I have a file which has data like a,b c,d e,f g,h And I need to insert a new column at the begining with sequence no( 1 to n) 1,a,b 2,c,d 3,e,f 4,g,h Please let me know how to acheive this in unix (3 Replies)
Discussion started by: weknowd
3 Replies

2. Shell Programming and Scripting

Perl Code to change file delimiter (passed as argument) to bar delimiter

Hi, Extremely new to Perl scripting, but need a quick fix without using TEXT::CSV I need to read in a file, pass any delimiter as an argument, and convert it to bar delimited on the output. In addition, enclose fields within double quotes in case of any embedded delimiters. Any help would... (2 Replies)
Discussion started by: JPB1977
2 Replies

3. Shell Programming and Scripting

Replace comma delimiter by newline

The input file is as below AR,age,marks,roll,section,evin,25,80,456,A,atch,23,56,789,B,eena,24 ,78H245,C,Ps,ot,ecessary,hat,ame comes first then age and rest AR AZ,kevin,25,80,456,A,Satch,23,56,789,Satch,23,56,789,B,Meena,24,78,H245,C,AZ ................ ................ I am writting... (8 Replies)
Discussion started by: millan
8 Replies

4. Shell Programming and Scripting

To change the delimiter for first two columns

Dear Friends, I have file as below 1|sdf|rere|sert|trt|rtr i want to change the delimeter first three columns two fields expected output 1~sdf~rere|sert|trt|rtr Plz help (2 Replies)
Discussion started by: i150371485
2 Replies

5. Shell Programming and Scripting

Substituting comma "," for dot "." in a specific column when comma"," is a delimiter

Hi, I'm dealing with an issue and losing a lot of hours figuring out how i would solve this. I have an input file which looks like this: ('BLABLA +200-GRS','Serviço ','TarifaçãoServiço','wap.bla.us.0000000121',2985,0,55,' de conversão em escada','Dia','Domingos') ('BLABLA +200-GRR','Serviço... (6 Replies)
Discussion started by: poliver
6 Replies

6. Shell Programming and Scripting

nawk won't accept comma as delimiter

I have a text file delimited by commas, with three fields (no " marks). I want to use awk to create a fourth field which is equal to the line number + field 1 + .txt I've searched this forum and found the following nawk -v OFS=';' '{print $0, FNR}' myFile Which I've amended to change the... (2 Replies)
Discussion started by: b.hamilton
2 Replies

7. UNIX for Dummies Questions & Answers

Making a Tab delimiter file to Comma

How can i make a tab delimiter file to a comma delimiter??? (13 Replies)
Discussion started by: saggiboy10
13 Replies

8. Shell Programming and Scripting

Replacing Comma delimiter coming inside the data.

Hello, I am having flat file (Comma Delimiter) and the data in the file is as given below. EMPNO, ENAME, DESIGNATION, SALARY 10979, Arun Kumar, Cosultant, 35000 13555, Bidhu Shekar, Senior Consultant, 45000 15000, Kiran, Kumar, Senior, Consultant, 40000 If... (9 Replies)
Discussion started by: arunvasu2
9 Replies

9. Shell Programming and Scripting

Exporting data into Comma Delimiter.

Hi, Requirement: Exporting data from Oracle to UNIX into "Comma" delimiter. Help Needed: I was able to connect to Oracle and import the data. But please let me know while importing the data I need to make it into Comma delimiter flat file. For Example: Source Data - 100 ABC TLead... (6 Replies)
Discussion started by: arunvasu2
6 Replies

10. Shell Programming and Scripting

comma delimiter and space

I have a csv file and there is a problem which I need to resolve. Column1,Column2,Colum3,Column4 ,x,y,z ,d,c,v t,l,m,n ,h,s,k ,k,,y z,j, ,p Now if you see column1 for row 1 and row 4 though they are null there is a space but in case of row2 and row 5 there is no space. I want row... (3 Replies)
Discussion started by: RubinPat
3 Replies
Login or Register to Ask a Question