Script to validate header in a csv file


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Script to validate header in a csv file
# 1  
Old 06-17-2018
Script to validate header in a txt file

Hi All;

I am struggling to write a script that validates file header.

Header file would be like below with TAB separated

Code:
TRX #	TYPE	REF #	Source Piece Code	Destination Piece Code


every time I need to check the txt file if the header was same as above fields if validation success process the file else exit 1

I really appreciate if someone can provide me the script

Thanks

Last edited by heye18; 06-18-2018 at 04:00 AM.. Reason: edit desc; [mod] Added Code tags
# 2  
Old 06-18-2018
Welcome to the forum.


Any attempts / ideas / thoughts from your side?
# 3  
Old 06-18-2018
The question is incomplete:

Do you require that the header is EXACTLY like the one you posted? In this case, there is nothing CSV-specific in your question. Instead, the problem boils down to finding out, whether the first line of a file has a certain content.

Or do you require that the header is STRUCTURALLY like the one you posted? For example, the CSV-file format allows fields to be quoted; or, you might consider trailing spaces after the header names insignificant; or you might allow the fields to occur in any order. In this case, you need to specify, what exactly are the conditions to regard the header valid.
# 4  
Old 06-18-2018
RudiC, I tried to write below code, but not able write the logic how to check below columns with header file columns.

Code:
TRX #    TYPE    REF #    Source Piece Code    Destination Piece Code

Code:
cd $MOUNT_DIR 
for file in $(ls ${MOUNT_DIR}/Test*.txt)
do
    file_name=$(basename $file)
    echo $name
    chmod -R 777 $MOUNT_DIR$name
    match_header=awk NR==1 $file_name
    echo "the header file from the input file is" $match_header
    
    if [ comm $match_header =] ** not able the script how to compare the header columns to mentioned columns **
    then
        echo $name:"Header file mismatch, terminate the flow " >> $JOB_LOG
        exit 1
    else
        echo $name:"Header file match, We can process this file " >> $JOB_LOG
    fi
done

---------- Post updated at 02:09 AM ---------- Previous update was at 01:58 AM ----------

rovf, Sorry for confusion. it was txt file (I have edited the post).

Desired columns (A): TRX # TYPE REF # Source Piece Code

columns we get from header file (B): TRX # TYPE *** # Source Piece Code

if columns from header file doesn't matches to Desired columns then it need to through terminate exit 1. else process the file




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

Last edited by RudiC; 06-18-2018 at 04:51 AM.. Reason: Added CODE tags.
# 5  
Old 06-18-2018
Something like below:

Code:
head -1 fileA | grep "$(cat fileB)" 
if [[ $? -ne 0 ]] ;
then
echo "fileA header does not match with fileB"
exit 1;
fi

Note: even if there is a difference with "spaces" match wont happen.
# 6  
Old 06-18-2018
How about

Code:
if awk '{exit !(/TRX #\tTYPE\tREF #/)}' $file_name
   then	# process file
   else	exit 1
   fi

What's the difference between $name and $file_name?
# 7  
Old 06-18-2018
Quote:
Originally Posted by heye18
if columns from header file doesn't matches to Desired columns....
What do you mean by "matching". Being literally equal? Or are you thinking of some kind of pattern matching?

Also, specify which shell you want the script to run in. This might be an important point if you are looking for some kind of pattern matching instead of just testing equality (which is trivial).
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Bash to verify and validate file header and data type

The below bash is a file validation check executed that will verify the correct header count of 10 and the correct data type in each field of the tab-delimited file. The key has the data type of each field in it. My real data has 58 headers in it but only the header and next row need to be... (6 Replies)
Discussion started by: cmccabe
6 Replies

2. UNIX for Beginners Questions & Answers

Validate csv file

Hi guys, i want to validate the no.of colums in a csv file ,but if there is a comma(,) in any of the data values it should skip and count only valid (,) commas. e.g 1.abc,pqrs,1234,567,hhh result :4 2.abc,pqrs,1234,567,hhh,"in,valid",end12 result:6 here script should skip the comma inside... (10 Replies)
Discussion started by: harry123
10 Replies

3. Shell Programming and Scripting

Script to ingest a csv, validate data and insert into Oracle

Hi all i would appreciate your help... I am looking for a set of unix commands which i can use to 1) ingest a csv file with a known format 2) validate the filename 3) validate the data/datatypes 4) Insert into an oracle db Can you help get me started? yogz888 (1 Reply)
Discussion started by: yogz888
1 Replies

4. Shell Programming and Scripting

Exclude the header row in the file to validate

Hi All, File contains header row.. we need to exclude the header row...no need to validate the first row in the file. Data in the file should take valid data(two columns)..we need to exclude the more than two columns in the file except the first line. email|firstname a|123|100 b|345... (4 Replies)
Discussion started by: bmk
4 Replies

5. Shell Programming and Scripting

Script to validate file header and trailer

Hi, I need a script that validates a file header/detail/trailer. File layout is: Header - Rec_Type|File_name|File_Date Detail - Rec_Type|field1|field2|field3... Trailder - Rec_Type|File_name|File_Date|Record_count Sample Data: HDR|customer_data.dat|20120709... (7 Replies)
Discussion started by: ash_sh
7 Replies

6. UNIX for Dummies Questions & Answers

Merge all csv files in one folder considering only 1 header row and ignoring header of all others

Friends, I need help with the following in UNIX. Merge all csv files in one folder considering only 1 header row and ignoring header of all other files. FYI - All files are in same format and contains same headers. Thank you (4 Replies)
Discussion started by: Shiny_Roy
4 Replies

7. Shell Programming and Scripting

How to: Validate a CSV file using an XSD?

Hi All, I was wondering if there is a utility/perl library/way of validating the contents of a CSV file using an XSD. i.e. Customer csv (including Header & Trailer) Cust_num, Cust_nme, Cust_typ, Cust_act_dte, Cust_loc, 101,Joe's Pizza,Retail,10121979,Detroit, 102,Sony... (6 Replies)
Discussion started by: luinzi
6 Replies

8. Shell Programming and Scripting

How can I add a header to a csv file

I have a csv file which has no header. the file has 15 fields and needs to go out with a header of 8 fields. The header content needs to have some variables and some fixed that i have set up: variable header fields OUTFILE_YEAR=`date '+%y'` DATE=`date '+%d%m%y'` TIME=`date '+%H:%M:%S'`... (6 Replies)
Discussion started by: Pablo_beezo
6 Replies

9. Shell Programming and Scripting

How to validate a CSV file?

Hi. I think some people have already asked this, but the answers/questions seem to be about validating the contents inside a CSV file. I am simply after a simple variable solution (ie 0 = false, 1 = true) that I can use in my script to say that file so-and-so is actually a CSV file, or in some... (4 Replies)
Discussion started by: ElCaito
4 Replies

10. Shell Programming and Scripting

validate csv file load

Hi All, I am using sqlldr to load my csv files into the database. The code in the sh script is as follows. sqlldr ${DBUSER}/${DBPASS}@${ORACLE_SID} \ data=myCSV.data \ bad=myCSV.bad \ control=myCSV.ctl \ ... (0 Replies)
Discussion started by: rahulrathod
0 Replies
Login or Register to Ask a Question