Validating Separator in unix


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Validating Separator in unix
# 1  
Old 10-13-2009
Validating Separator in unix

Hi
I have an input CSV file which contains some values
Format of file is as follows:
32R01000000003144476;32R01000000029054270;1000;6000;12/12/2007;0382521276;REG;Régulari

I just need to validate it whether it is ';' seperated ie (if any other seperator is used it shld give an error)
How do i do it?
the file contains many values and not just 1

Thanks in Advance.
theeights
# 2  
Old 10-13-2009
Unless you know what data to expect, there's no way to reliably determine what the delimiter in a file is.

Can you elaborate on "the file contains many values and not just 1"? Does this mean multiple rows with the same number of columns, or a variable number of columns?
# 3  
Old 10-13-2009
so according to you ";" should appear in every line..
assuming you have fixed no of fields
you can try this...
Code:
awk -F";" 'NF!=8{print "ERROR"}' file

# 4  
Old 10-14-2009
The file contains many values means that the file contains multiple values eg:
32R01000000003144476;32R01000000029054270;1000;6000;12/12/2007;0382521276;REG;Régulari
32R01000000003144476;32R01000000029054270;1000;6000;12/12/2007;0382521276;REG;Régulari
32R01000000003144476;32R01000000029054270;1000;6000;12/12/2007;0382521276;REG;Régulari
32R01000000003144476;32R01000000029054270;1000;6000;12/12/2007;0382521276;REG;Régulari

I have to check every occurance of Semicolon
theeights
# 5  
Old 10-14-2009
This could be one of the way...

Code:
# cat t.pl
#! /usr/bin/perl

while (<>) {
        die " Delimiter is invalid in Line no $.\n" unless(  split(/#/) > 1 );
}

Execute like
Code:
#perl t.pl < filename

# 6  
Old 10-15-2009
Good suggestions. If I could be bold enough to suggest a couple of modifications, though...

Input data:

Code:
32R01000000003144476;32R01000000029054270;1000;6000;12/12/2007;0382521276;REG;Régulari;
32R01000000003144476;32R01000000029054270;1000;6000;12/12/2007;0382521276;REG;Régulari
32R01000000003144476;32R01000000029054270 1000;6000;12/12/2007;0382521276;REG;Régulari
32R01000000003144476;32R01000000029054270;1000;6000;12/12/2007;0382521276;REG;Régulari

Code:
awk -F";" 'NF!=8{print "ERROR on line " NR}'

Output:

ERROR on line 1
ERROR on line 3

And

Code:
#! /usr/bin/perl

while (<>) {
        print " Delimiter is invalid in Line no ", $. - 1 , "\n" unless(  split(/;/) != 8 );
}

Output:

 Delimiter is invalid in Line no 1
 Delimiter is invalid in Line no 3

And just in case you want a 'line-by-line' validation, rather than just the erroneous lines you could try:

Code:
awk -F";" ' { if(NF!=8) print "ERROR" ; else print $0 } '

Output:

ERROR
32R01000000003144476;32R01000000029054270;1000;6000;12/12/2007;0382521276;REG;Régulari
ERROR
32R01000000003144476;32R01000000029054270;1000;6000;12/12/2007;0382521276;REG;Régulari


Last edited by DeepakS; 10-15-2009 at 03:33 AM..
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Replacing UNIX 1 byte delimiters to the pipe separator

Hi I am new to Unix Shell scripting have a requirement where I have to replace the "unix 1 byte delimiter" with the "pipe" separator and also remove any carriage returns and line feeds if any The Source File 4 QFH Jungle Hill 32-34 City Road London SE23 3UX the output should be ... (3 Replies)
Discussion started by: dJHa
3 Replies

2. Shell Programming and Scripting

Validating XML file using XSD in UNIX

Hi, I have a xml file and a xsd file(xml schema file). Here using unix script i wanted to validate the xml file by referring to xsd file. The validation is in terms of Datatype,Field length and null values. If the data present in the xml file is not matching in terms of datatype,field length... (3 Replies)
Discussion started by: shree11
3 Replies

3. Shell Programming and Scripting

Validating uppercase/lowercase of user input with perl compared to unix folders

Hi, I need to copy files from a source directory to a destination directory in unix. I'm using the file::copy for the actual copy. The problem is that the source and dest directories are supplied by different users, who might type the name of the directories in various combinations of lower... (6 Replies)
Discussion started by: Furou
6 Replies

4. UNIX for Dummies Questions & Answers

Validating a file

Pardon my ignorance but I am lost on how to do this I have a file called "Sample.txt", it is pipe delimited, it should have 13 fields. But some of the records do not, I would like to set up a shell script where I can pass in a parameter "Sample.txt" and it would split the file into records... (2 Replies)
Discussion started by: dgeehot
2 Replies

5. Shell Programming and Scripting

Unix Script for getting date and validating just Hour

Hi, Can someone guide me to write a unix script for getting a hour out of a date command and validating hour to see if its > 7 and < 16. if hours is >7 and <16 then assign a variable value of 0730 and if hour is >16 then assign a variable value of 1630? Help appreciated. Thanks in advance.... (9 Replies)
Discussion started by: zulfikarmd
9 Replies

6. Shell Programming and Scripting

Can any one share a unix script for validating the export of a mapping which is in XM

Hi, Can any one share a unix script for validating the export of a mapping which is in XML format. like my requirment is that when ever we are migrating the mappings or wokflows from development to QA we will take the export of that mapping into an XML file.Insted of reviewing the mapping... (1 Reply)
Discussion started by: perlamohan
1 Replies

7. UNIX for Advanced & Expert Users

Can any one share a unix script for validating the export of a mapping which is in XM

Hi, Can any one share a unix script for validating the export of a mapping which is in XML format. like my requirment is that when ever we are migrating the mappings or wokflows from development to QA we will take the export of that mapping into an XML file.Insted of reviewing the mapping... (1 Reply)
Discussion started by: perlamohan
1 Replies

8. UNIX for Dummies Questions & Answers

Validating XSL sheet data in Unix Data file

Dear All, Need your help. In my day to day activities I have to validate/search Excel Sheet data (eg.say Application No. 0066782345) data into the Unix environment file whether the same data is present in that file or not. There are hundreds of records coming in excel file and I am doing grep... (1 Reply)
Discussion started by: ravijunghare
1 Replies

9. UNIX for Dummies Questions & Answers

Help with unix separator

can some one give me a list of unix separtor(s) if one than just the separator please thank you. (2 Replies)
Discussion started by: Black mage2021
2 Replies

10. Shell Programming and Scripting

Validating $1 and $2 before using

Hi there, I'm trying to validate my $1 and $2 before I use them in the sqlplus update, I think I have the validation rules set correctly??? however when I test my script, it jumps straight into the sql. It doesn't seem to validate the vars....even when I know they are incorrect. if ; then ... (6 Replies)
Discussion started by: nhatch
6 Replies
Login or Register to Ask a Question