How to fix file with more delimiter


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting How to fix file with more delimiter
# 1  
Old 12-03-2010
How to fix file with more delimiter

Hi, Unix experts,
I have a problem about one file. my source file as following:
123, accounting, university street, 23456
124, financing, center street, b suit, 23457
125, information, college street, 23458
above file, 'center street, b suit' should be one field, my file contains more than 50 columns.
target should be
123, accounting, university street, 23456
124, financing, center street b suit, 23457
125, information, college street, 23458

how can I fix this problem? anybody can give me idea.

Thanks in advance.
ken002
# 2  
Old 12-03-2010
This should work, but everything is hard coded:
Code:
awk -F, '{for(i=1;i<=NF;++i) {if($i==" center street") {printf "%s %s", $i, $(i+1); ++i} else {printf $i} if (i<NF) printf ", "} print ""}' infile

or simply sed:
Code:
sed 's/\(center street\),\( b suit\)/\1\2/g' infile

# 3  
Old 12-03-2010
Hi, Kevintse,
Thanks for your replay, but my problem, the column which contains delimiter has different contents. for example.
123, accounting, university street, 23456
124, financing, center street, b suit, 23457
125, information, college street, 23458
126, computer, clars street, apt 12, 2222
target should be
123, accounting, university street, 23456
124, financing, center street b suit, 23457
125, information, college street, 23458
126, computer, clars street apt 12, 2222


Thanks
ken002
# 4  
Old 12-03-2010
What this GNU sed line will do is capture everything EXCEPT the extra comma of the address line 2 field.

Code:
gsed 's/^\([^,]*,[^,]*,[^,]*\),\([^,]*,[^$]*$\)/\1\2/'

# 5  
Old 12-03-2010
An awk solution (you need to adapt it to your field number):
Code:
awk -F\, 'NF==5{$3=$3""$4;$4="";sub(/,,/,",")}1' OFS="," infile

This User Gave Thanks to Klashxx For This Post:
# 6  
Old 12-03-2010
Code:
perl -ne 's/(([^,]+,\s+){2})([^,]+),/$1$3/; print ' input_file

# 7  
Old 12-03-2010
If the total number of comma's in a line are less than or equal to 4 then try..
Code:
sed -e 's/,//3' -e s'/\([a-z]\+\) \([0-9]\+\)/\1,\2/' inputfile


Last edited by michaelrozar17; 12-03-2010 at 06:40 AM..
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. 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

2. UNIX for Dummies Questions & Answers

Getting the folder name and file name after delimiter

Hi, I have a input /dev/cm/test1.txt /qa/tm/hmkr/cc/test2.txt and I need an out like below foldername, filename /dev/cm/,test1.txt /qa/tm/hmkr/cc/,test2.txt I tried with awk $NF, but I'm getting the filenames and not folder names. Please let me know how to achive the above... (5 Replies)
Discussion started by: somu_june
5 Replies

3. 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

4. Shell Programming and Scripting

Scripting to fix the issue in UNIX file having delimiter "|"

hello All, I am new user to shell scripting, kindly advise on the below? I have a file where i have gaps & the delimiter falls in next line and new line is also created , plz see the example :employee.txt Now the issue here is , i wan to write a script , where i can use "|" to get the... (6 Replies)
Discussion started by: sunnyd1
6 Replies

5. UNIX for Advanced & Expert Users

File Delimiter

Hi All, I woul like to know with out opening a file in unix ,how we can find out what is the delemeter in that file... Thanks.. edit by bakunin: changed thread title to "delimiter" so it can be found. (4 Replies)
Discussion started by: raju4u
4 Replies

6. Shell Programming and Scripting

How to cut by delimiter, and delimiter can be anything except numbers?

Hi all, I have a number of strings like below: //mnt/autocor/43°13'(33")W/ and i'm trying to get the numbers in this string, for example 431333 please help thanks ahead (14 Replies)
Discussion started by: sunnydanniel
14 Replies

7. Shell Programming and Scripting

Delimiter in output file

Hello, I am trying to find the record count in a specific folder, Here is the part of the code =========================== STARTDATE=`date +"%y%m%d%H%M"` for i in `ls *.DAT` do wc -l $i >> /XYZ/SrcFiles/"Record_counts"$STARTDATE.csv ... (2 Replies)
Discussion started by: Shanks
2 Replies

8. UNIX for Dummies Questions & Answers

How to change delimiter in my file ?

Hi I have a file in which delimiter is ';' However if the delimiter is within "" it is a part of the string and not delimiter. How to get the fields ? I want to replace the delimiter ';' to '|'. The file contains data like this : 11111; “2222 2222”; “3333; 3333”; “4444 ""44444” The file... (2 Replies)
Discussion started by: dashing201
2 Replies

9. Shell Programming and Scripting

Substring based on delimiter, finding last delimiter

Hi, I have a string like ABC.123.XYZ-A1-B2-P1-C4. I want to delimit the string based on "-" and then get result as only two strings. One with string till last hyphen and other with value after last hyphen... For this case, it would be something like first string as "ABC.123.XYZ-A1-B2-P1" and... (6 Replies)
Discussion started by: gupt_ash
6 Replies

10. Shell Programming and Scripting

splitting file with more than one delimiter

Hi, I just wandering how to split a record which has more than one delimiter, i have a file which contains pattern as group separtor and ~ as field separtor, Ultimately I need consider even the groups as a field, So i need to make this multi-delimited file into ~ delimited file. My record... (4 Replies)
Discussion started by: braindrain
4 Replies
Login or Register to Ask a Question