Combine two Fields if there's a comma between


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Combine two Fields if there's a comma between
# 1  
Old 03-18-2016
Combine two Fields if there's a comma between

Hi All,

Seeking for your assistance on how to combine two fields if there's a comma between them.

What i did was, extract the records from database.

file1.csv(extracted from db)
Code:
82|Supplies Station, Inc.|112012|KARISSA APPAREL, INC. - 112012|NON TRADE F/A AND SUPPLIES|7486|ORAFAS|ORAFAS|2016-03-15|STANDARD|Invoice|60 Days|2016-05-14|CONSO DATED|80400.00|Not Yet Due|0.0010056572830600|Never Validated

As you can see there's a comma on Field 2 and 3(| delimeted), if you will open it in csv the field 2 and will be consist of 2 fields same field 3 because of the csv comma delimeted.

CSV output
Code:
82|<b>Supplies Station</b>|<b>Inc.</b>|112012|<b>KARISSA APPAREL</b>| <b>INC. - 112012</b>|NON TRADE F/A AND SUPPLIES|7486|ORAFAS|ORAFAS|2016-03-15|STANDARD|Invoice|60 Days|2016-05-14|CONSO DATED|80400.00|Not Yet Due|0.0010056572830600|Never Validated

My Expected output in the csv file:
Code:
82    Supplies Station,Inc.   112012    KARISSA APPAREL, INC. - 112012     NON TRADE F/A AND SUPPLIES   7486   ORAFAS   ORAFAS   2016-03-15   STANDARD   Invoice   60 Days   2016-05-14   CONSO DATED   80400.00   Not Yet Due   0.0010056572830600   Never Validated

i used awk but still wrong
Code:
awk '{for (i=1;i<=NF;i++) $i="\""$i"\""}1' FS="|" OFS="|" file1.csv

Please advise,

Thanks,
Moderator's Comments:
Mod Comment When presenting input and output that contains multiple adjacent spaces and input and output lines that do not otherwise fit on a single line; using CODE tags is crucial for people reading your posts to understand what you are trying to do.

You have been placed in read-only mode for a few days for your repeated refusal to use CODE tags when they are required. Read the e-mail that you received explaining why you have you received your 5th infraction for not using CODE tags. Look at the tutorial included in that e-mail. And, please start using CODE tags as required by forum rules instead of expecting moderators and administrators to format your posts for you.

Continued refusal to use CODE tags properly may result in you being permanently barred from posting in the UNIX & Linux Forums.

Last edited by Don Cragun; 03-18-2016 at 02:56 AM.. Reason: Add CODE tags again.
# 2  
Old 03-18-2016
I am totally confused by the description of your problem. You start off with a file of information extracted from a database into a character separated values file with vertical-bar as the separation character. (In which case commas should be absolutely unimportant.)

Then you say you open it in csv??? What is csv? Is it the name of a utility that you have written that assumes that every comma (and sometimes also a space following a comma) should be changed to a vertical-bar? It sounds like your entire problem is that you are using a utility that destroyed the commas in your input file when you didn't want the commas to be changed at all. Why are you using a utility that destroys your input data?

Then you say that the output that you want should randomly replace vertical-bar characters in your input file with three, four, or five space characters and sometimes remove a single space character following a comma. But the description of your problem didn't say anything about changing vertical-bar characters to random numbers of adjacent space characters.

Then you show us an awk script that surrounds each input field with double-quote characters (not affecting commas and not changing vertical-bar field separation characters).

Please give us a MUCH clearer explanation of what you are trying to do (and use CODE tags when displaying sample input, sample output, AND code segments so we have a chance of telling the difference between a single space character and sequences of multiple adjacent space tab characters).
This User Gave Thanks to Don Cragun For This Post:
# 3  
Old 03-18-2016
Actually, I can't see the problem here. ".csv" implies (but doesn't insist) fields to be separated by commas (or semicolons). Spreadsheet programs would show three fields (by default) with your above data. But - when reading (any) text file, you usually can select the field separator; setting it to | will yield exactly what you seem to be after: Supplies Station, Inc. and KARISSA APPAREL, INC. - 112012 in fields 2 and 3, resp.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

How to extract fields from a CSV i.e comma separated where some of the fields having comma as value?

can anyone help me!!!! How to I parse the CSV file file name : abc.csv (csv file) The above file containing data like abv,sfs,,hju,',',jkk wff,fst,,rgr,',',rgr ere,edf,erg,',',rgr,rgr I have a requirement like i have to extract different field and assign them into different... (4 Replies)
Discussion started by: J.Jena
4 Replies

2. Shell Programming and Scripting

awk to format file and combine two fields using comma

I am trying to use awk to format the file below, which is tab-delimited. The desired out is space delimited and is in the order of $9 $13 $2 $10-$11.$10 and $11 are often times multiple values separated by a comma, so the value in $10 is combined with the first value from $11 using the comma.... (5 Replies)
Discussion started by: cmccabe
5 Replies

3. Shell Programming and Scripting

Trying to combine fields with sort/join

I have a file with two fields in it delimited by a comma. Some of the first fields are duplicates. I am trying to eliminate any duplicate records in the first field, and combine the second fields in the output file. For example, if the input is: Jane,group=A Bob,group=A Bob,group=D... (3 Replies)
Discussion started by: DJR
3 Replies

4. Shell Programming and Scripting

Search and combine fields

Hi all, 1. I have a log file 2011/11/14 00:42:50 | 38:guess pid=008499 opened Testing 0, 1, 2, 3 2011/11/14 11:43:42 | 38:guess pid=008499 closed 2011/11/14 11:47:08 | 39:guess pid=017567 opened Testing 0, 1, 2, 3 2011/11/14 11:47:08 | 40:guess pid=012780 opened Testing 0, 1,... (4 Replies)
Discussion started by: sabercats
4 Replies

5. Shell Programming and Scripting

How to combine two files based on fields?

I have two files which are as follows: File 1: 1 abc 250 2 pqr 300 3 xyz 100 File 2: 1 abc 230 2 pqr 700 3 xyz 500 Now I need output File, File 3as: S.No Name Count1 Count2 1 abc 250 230 2 pqr 300 700 3 xyz 100 500 NOTE: (13 Replies)
Discussion started by: karumudi7
13 Replies

6. Shell Programming and Scripting

How to combine first,second and third fields in a file

Hi Guys, I have a file as follows: 1 2 3 There are 5 spaces between 1 and 2 and one space between 2 and 3. I want: 123 How can I do this using awk? I tried using: (8 Replies)
Discussion started by: npatwardhan
8 Replies

7. Shell Programming and Scripting

how to combine fields from different files

linus>cat data1 8119463 | 15000 8136789 | 50000 8152238 | 15000 8167890 | 15000 7155789 | 15000 8123334 | 30000 7166330 | 15000 8146683 | 15000 8152238 | 15000 8167877 | 15000 7145389 | 15000 8178434 | 15000 7166222 | 50000 8167566 | 15000 8115410 | 45000... (2 Replies)
Discussion started by: littleb
2 Replies

8. Shell Programming and Scripting

Trimming fields for comma or pipe seperated file

I have file like this FileA: abc , "helloworld" , america def,asia, japan ghi, africa, ipl Output Needed: abc,"helloworld",america def,asia,japan ghi,africa,ipl I would like to implement using awk. I want to trim each field for its leading and trailing spaces. (7 Replies)
Discussion started by: pinnacle
7 Replies

9. UNIX for Dummies Questions & Answers

Sort the fields in a comma delimited file

Hi, I have a comma delimited file. I want to sort the fields alphabetically and again store them in a comma delimited file. For example, My file looks like this. abc,aaa,xyz,xxx,def pqr,ggg,eee,iii,qqq zyx,lmo,pqr,abc,fff and I want my output to look like this, all fields sorted... (3 Replies)
Discussion started by: swethapatil
3 Replies

10. UNIX for Dummies Questions & Answers

Combine fields and eliminate white space

Good Morning, Newbie here. Could someone help with shell scripting that will enable me to combine 2 fields into one eliminating the white space. The fields are fixed but the data of course varies. For example: First Name: "George " 20 positions" Last Name: "Washington " 30 positions" I need... (2 Replies)
Discussion started by: ski
2 Replies
Login or Register to Ask a Question