Issue with Pattern Matching in Unix


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Issue with Pattern Matching in Unix
# 1  
Old 08-26-2009
Issue with Pattern Matching in Unix

Hi,

I am trying to replace a specific column values in a csv file with double quotes.

Example:

Code:
SNO,NAME,ZIPCODE,RANK,CARE_OF
1,Robert,74538,12,RICHARD JOHNSON, P.C
2,Sam,07564,13,% R.S MIKE, V.K.S
3,Kim, Ed,12345,14,@90 KMS, %TK

Desired Output:
Code:
SNO,NAME,ZIPCODE,RANK,CARE_OF
1,Robert Ken,74538,12,"RICHARD JOHNSON, P.C"
2,Sam Mik,"07564",13,"% R.S MIKE, V.K.S"
3,"Kim, Ed",12345,14,"@90 KMS, %TK"

I would like to replace the zipcode value with double quotes when I find a leading zeros for the zipcode. Also, I would like to replace the name in double quotes when I found a comma with in the name.
Also, I would like to replace the CARE_OF value with double quotes when I found a comma with in this column value.

The following command is working fine for Name and Zipcode column values, please advice on CARE_OF column value.

Code:
sed -e 's/,\(0[0-9]*\)/,\"\1\"/g'  -e 's/,\([ 0-9A-Za-z]*, [  0-9A-Za-z]*\),/,\"\1\",/g' -e 's/,\([ 0-9A-Za-z]*, [ 0-9A-Za-z]*\),/,\"\1\",/g' inputFile.csv > outputFile.csv

Please throw your ideas Smilie

Thanks,
moris.

Last edited by Franklin52; 08-26-2009 at 08:38 AM.. Reason: Please use code tags!
# 2  
Old 08-26-2009
How about gawk? :)

Code:
% gawk -F',' ' {

if(NF==5) { if($3 ~ /^0/) { $3 = "\"" $3 "\"" } print $0 }
else {
    sno=$1

    if($3 !~ /^[0-9]*$/)  {

        name="\"" $2 "," $3 "\""; zip=$4;

        if($(NF-1) !~ /^[0-9]*$/)  {

             care_of="\"" $(NF-1) "," $(NF) "\""; rank=$(NF-2);

        } else { care_of=$6;rank=$5; }

    } else {

         if($3 ~ /^0/) { $3 = "\"" $3 "\"" }
         name=$2; zip=$3;
         if($(NF-1) !~ /^[0-9]*$/)  { care_of="\"" $(NF-1) "," $(NF) "\""; rank=$(NF-2); } else { care_of=$5;rank=$4; }
    }
    if(zip ~ /^0/) { zip = "\"" zip "\"" }
    print sno "," name "," zip "," rank "," care_of
}

} ' inputFile.csv > outputFile.csv

% cat *csv
SNO,NAME,ZIPCODE,RANK,CARE_OF
1,Robert Ken,74538,12,RICHARD JOHNSON, P.C
2,Sam Mik,07564,13,% R.S MIKE, V.K.S
3,Kim, Ed,12345,14,@90 KMS, %TK
SNO,NAME,ZIPCODE,RANK,CARE_OF
1,Robert Ken,74538,12,"RICHARD JOHNSON, P.C"
2,Sam Mik,"07564",13,"% R.S MIKE, V.K.S"
3,"Kim, Ed",12345,14,"@90 KMS, %TK"

Cheers,

coding-school
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Binary pattern matching in UNIX

I think what I'm trying to do is pretty straightforward but I just can't find a way to do it. I'm trying to run a double pattern match in a three column file. If the first two columns match, I need to output the third. So in the file AAA BBB 1 BBC CCC 5 CCC DDD 7 DDD EEE 12 If the... (4 Replies)
Discussion started by: matthewndavies1
4 Replies

2. Shell Programming and Scripting

awk script issue redirecting to multiple files after matching pattern

Hi All I am having one awk and sed requirement for the below problem. I tried multiple options in my sed or awk and right output is not coming out. Problem Description ############################################################### I am having a big file say file having repeated... (4 Replies)
Discussion started by: kshitij
4 Replies

3. Shell Programming and Scripting

issue with pattern matching

i have 2 strings with values below which are read from a file 1 ---> end 2 ---> string(1)newline="\n"; which need to be compared inside an if block as below if \\n\"\;" ] then echo "pattern match" fi but the above code is not working (1 Reply)
Discussion started by: cvsanthosh
1 Replies

4. Shell Programming and Scripting

awk pattern matching and shell issue.

Please help me in this issue. I am unable to get the job,seems the awk not browsing the files. Please find my tries below. I have attached two files : 1.tobesearched.txt - a glimpse of a huge log file. 2.searchstring.txt - searching keys. these are the two scripts i tried writing: ... (7 Replies)
Discussion started by: deboprio
7 Replies

5. Shell Programming and Scripting

Pattern matching in Perl issue

Hello all, I have written a perl script which searched for a file in a folder, and return the value found or not found. The file that we are getting is a weekly file and everyweek, the date changes. So for this i have written a file pattern matching script, but i am not getting the... (4 Replies)
Discussion started by: msrahman
4 Replies

6. Shell Programming and Scripting

pattern matching on any special character in Unix

Hi, I have field in a file which would come with any special character, how do i check that field? Eg: @123TYtaasa>>>/ 131dfetr_~2 In the above example, how do I add pattern for any special character on the keyboard. Thanks (3 Replies)
Discussion started by: techmoris
3 Replies

7. Shell Programming and Scripting

Perl Pattern Matching:Unix Vs. Window (Is it OS dependent??)

Hi All, Below I am discussing the problem I am facing while using pattern matching in Unix & Window. Plz hv a look into it. ================ my $s="UPDATE A SET s="klkkk' ;" ; if ( $s =~ m/^*UPDATE+/i ) { print $s; } else { print "no match";} =================== Both should... (6 Replies)
Discussion started by: Niroj
6 Replies

8. UNIX for Advanced & Expert Users

pattern matching in unix

Task is to identify files like code.1 , code.23 and so on ... (the files which are ending with a number) but it should not match files like code.123abc. So the search will normally search for files with "code." and at the end we should extract for the correct match. Now I have to remove these files... (6 Replies)
Discussion started by: diwakar_reddy
6 Replies

9. Shell Programming and Scripting

Pattern Matching problem in UNIX

Hello All, I need help I have a problem in searching the pattern in a file let us say the file contains the below lines line 1 USING *'/FILE/FOLDER/RETURN') ................. ................. line 4 USING *'/FILE/FOLDER/6kdat1') line 5 USING... (2 Replies)
Discussion started by: maxmave
2 Replies

10. Shell Programming and Scripting

Pattern Matching in UNIX

Hello, I have a pattern like "XXXXXX XXXXXX" which i need to make search in a input file and Replace the matched pattern to a another pattern. This is the code i tried .. #!/usr/bin/perl print "Enter a File name :"; chomp ($file = <STDIN>); print "\n Searching file :"; if (system ("ls... (2 Replies)
Discussion started by: maxmave
2 Replies
Login or Register to Ask a Question