How to remove the delimiter from the column value within a file?


 
Thread Tools Search this Thread
Top Forums UNIX for Advanced & Expert Users How to remove the delimiter from the column value within a file?
# 1  
Old 09-27-2016
How to remove the delimiter from the column value within a file?

Hello All,

we have some 10 files wherein we are using the ASCII NULL as separator which is nothing but '^@' and we need to change it to pipe delimited file before loading to database. Most of the data seems to be fine but there are instances where this separator tends to appear in the middle of column value like email address "asbc^@gmail.com

Below is the example of data, where second rows seems to have '^' in email address.

Code:
deptno dname location email
1^@Sales^@NY^@abc@gmail.com
2^@marketing^@WA^@xyz^@gmail.com
3^@Inventory^@NJ^@pqr@gmail.com

we have manage to convert the data into pipe separated file but the problem is how to you tackle this extra '^

Code:
cat -v raw_file.dat | sed 's/\^@/|/g' > parsed_raw_file.dat

Any help would be great.
Thanks
Djha



Moderator's Comments:
Mod Comment Please use CODE tags as required by forum rules!

Last edited by RudiC; 09-27-2016 at 08:23 AM.. Reason: Added CODE tags.
# 2  
Old 09-27-2016
I'm afraid this problem is not that easy to solve as it seems to come from a data/data entry error in the input file. We need to unambiguously identify the address field. Will records always consist of the same number of fields? Will the e-mail-addresses always be in the records' last field? Will they be the only fields containing a dot, or a common domain or set of domains?

Depending on what OS and sed version you use (which you fail to mention in your post), this might work for the conversion (without solving your problem yet):
Code:
sed 's/\x00/|/g' file
deptno dname location email
1|Sales|NY|abc@gmail.com
2|marketing|WA|xyz|gmail.com
3|Inventory|NJ|pqr@gmail.com

Don't you need the header line use the same separators?

Last edited by RudiC; 09-27-2016 at 08:42 AM..
# 3  
Old 09-27-2016
Hi Rudic,

Thanks for your comments. we have got 10 files and all the files will have different number of columns and this ^ issue can appear in any column within the file. I know I am making things a bit complicated but thats how our requirements are.

Thanks
Dharmendra

Tha
# 4  
Old 09-27-2016
Unless you give people something to work upon, I'm afraid we're out of options.
# 5  
Old 09-27-2016
You might be able to do a three pass solution where you change the ^@ in the email addresses first to something temporary, then change everything else, then go back and change the temporary characters to what you want. Are all of the email addresses .com? or are there any .edu or .org. That might help to simplify the problem.
# 6  
Old 09-27-2016
Why not just replace ^@ with @ ? The crucial thing is to identify the address fields.
# 7  
Old 09-27-2016
Is the data shown the actual data where the separator is two characters ^ followed by @, or a screen scrape of the data where it is a single character being displayed as ^@?

Could you do this for us:-
Code:
head -1 raw_file.dat ; head -1 raw_file.dat | od -x

That should give us the actual text line and what it appears to be, otherwise we're left guessing what you have and we may spend ages going the wrong way.



Kind regards,
Robin
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Replace delimiter for a particular column in a pipe delimited file

I have an input file as below Emp1|FirstName|MiddleName|LastName|Address|Pincode|PhoneNumber 1234|FirstName1|MiddleName2|LastName3| Add1 || ADD2|123|000000000 Output : 1234|FirstName1|MiddleName2|LastName3| Add1 ,, ADD2|123|000000000 OR 1234,FirstName1,MiddleName2,LastName3, Add1 ||... (2 Replies)
Discussion started by: styris
2 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

Remove the values from a certain column without deleting the Column name in a .CSV file

(14 Replies)
Discussion started by: dhruuv369
14 Replies

4. Shell Programming and Scripting

Shell script to put delimiter for a no delimiter variable length text file

Hi, I have a No Delimiter variable length text file with following schema - Column Name Data length Firstname 5 Lastname 5 age 3 phoneno1 10 phoneno2 10 phoneno3 10 sample data - ... (16 Replies)
Discussion started by: Gaurav Martha
16 Replies

5. Shell Programming and Scripting

How to avoid Delimiter occuring in column values in .csv file

Hello Gurus, I need to create a file from a .csv file extracting specific columns only. File structure is Column1,Column2,Column3,Column4 abcd,1234,"asdf, tew,123",123456 efgh,234,asdf,654321 My output file should have abcd,123456 efgh,654321 Can you pls help me with the code. ... (10 Replies)
Discussion started by: ritesh.bhawsar
10 Replies

6. Shell Programming and Scripting

How to remove delimiter from specific column?

I have 5 column in sample txt file where in i have to create report based upon 1,3 and 5 th column.. I have : in first and third coulmn. But I want to retain the colon of fifth coulmn and remove the colon of first column.. 5th column contains String message (for example,... (7 Replies)
Discussion started by: Shirisha
7 Replies

7. Shell Programming and Scripting

rearrange the column names with comma as column delimiter

Hi, I am new to shell scripting, i have requirement can any one help me out in this regrads, in directory i have file like invoice1.txt, invoice2.txt in each file i have fixed number of columns, 62 in number but they are randomly arranged.like for first file invoice1.txt can have columns... (5 Replies)
Discussion started by: madhav62
5 Replies

8. Shell Programming and Scripting

Remove first column from file

Hi, This is how data in test.txt file | |abc|zxcv|xy12| | |cvs|zzvc|a23p| How can remove first column. abc|zxcv|xy12| cvs|zzvc|a23p| Thanks srimitta (8 Replies)
Discussion started by: srimitta
8 Replies

9. Shell Programming and Scripting

How to read the first column in a flat file with ~ as delimiter

I have one flat file like below id1~col~batch1 id2~col2~batch2 id3~col3~batch3 I need to read the first column one by one and I need to write one db2 query based on that column1 Like for (i=0;i<=10;i++) do insert into table column (con_id) values (select column from table where... (4 Replies)
Discussion started by: siri_886
4 Replies
Login or Register to Ask a Question