Extraction of header columns and comparing it with Header_format


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Extraction of header columns and comparing it with Header_format
# 1  
Old 05-29-2017
Extraction of header columns and comparing it with Header_format

Code:
set -x
for file in /Src/MEDIA_ASSET/*.csv;
do
Header_Format = 'VariantNumber|ERP_SYSTEM_CD|MediaType'
FILESTATUS = GOOD
File_Header = $(cut -d'|' -f1-3 ${file}|head -1)
  do
    if [${Header_Format} = ${File_Header} ];
    then
       ${FILESTATUS} = GOOD
    else
       ${FILESTATUS} = BAD
       break
    fi
  done
  if [${FILESTATUS} = "GOOD"];
  then
     echo "File is validated and ready for processing"
     mv ${file} /Tgtpath/MEDIA_ASSET/
  else
     echo "File is error file and not ready for processing"
     mv ${file} /Tgtpath/MEDIA_ASSET/
  fi
done

I am facing below error for this script
Code:
Header_Columns_Extraction.sh: line 20: syntax error near unexpected token `do'
Header_Columns_Extraction.sh: line 20: `  do'

I Just need to extract first 3 columns of the header and compare it with the defined Header_Format. Please help me here.

Last edited by Scrutinizer; 05-29-2017 at 01:35 PM.. Reason: code tags
# 2  
Old 05-29-2017
Remove the 'do' that error message complains about. 'do' is only used for loops, it shouldn't be there.

Also, if [${Header_Format} = ${File_Header} ]; is wrong, you need spaces between [ ] and whatever's inside them, and should double-quote your variables, like if [ "${Header_Format}" = "${File_Header}" ];

Assignments are the opposite, you must avoid spaces and not wrap the variable name in ${} like
Code:
FILESTATUS="GOOD"

# 3  
Old 05-29-2017
Also, instead of two if-statements, you could do one if-statement and get rid of FILESTATUS completely. The code below a break or continue will not be reached so you know the file is okay if the code gets to that point.

You should use continue instead of break, unless you really intended the loop to completely stop at the first bad file.

Code:
if ! [ "${Header_Format}" = "${File_Header}" ]
then
        echo "File is error file and not ready for processing"
        continue # break will completely stop the loop, continue will restart with next item
fi

echo "File is validated and ready for processing"
echo mv "${file}" /Tgtpath/MEDIA_ASSET/ # Remove echo once tested


Last edited by Corona688; 05-30-2017 at 12:21 PM.. Reason: fix awful spacing
# 4  
Old 05-30-2017
Extraction of header columns and comparing it with Header_format

Thanks Corona688 it's working Smilie..And thanks for your suggestions i will follow those!!
This User Gave Thanks to spidy For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Keep only columns in first two rows based on partial header pattern.

I have this code below that only prints out certain columns from the first two rows (doesn't affect rows 3 and beyond). How can I do the same on a partial header pattern “G_TP” instead of having to know specific column numbers (e.g. 374-479)? I've tried many other commands within this pipe with no... (4 Replies)
Discussion started by: aachave1
4 Replies

2. Emergency UNIX and Linux Support

Average columns based on header name

Hi Friends, I have files with columns like this. This sample input below is partial. Please check below for main file link. Each file will have only two rows. ... (8 Replies)
Discussion started by: jacobs.smith
8 Replies

3. Shell Programming and Scripting

Extract columns based on header

Hi to all, I have two files. File1 has no header, two columns: sample1 A sample2 B sample3 B sample4 C sample5 A sample6 D sample7 D File2 has a header, except for the first 3 columns (chr,start,end). "sample1" is the header for the 4th ,5th ,6th columns, "sample2" is the header... (4 Replies)
Discussion started by: aec
4 Replies

4. UNIX for Dummies Questions & Answers

Printing columns with header

Hi Gurus, I want to extract certain columns from file 2 and combine with file 1. I am using the following script to extract the columns. $ awk 'FNR>1{print $2, $9, FILENAME}' *.lim > out1 However, this script does not print the titles of the columns 2 and 9. Can somebody help me in... (1 Reply)
Discussion started by: Unilearn
1 Replies

5. Shell Programming and Scripting

Need awk help to print specific columns with as string in a header

awk experts, I have a big file of 4000 columns with header. Would like to print the columns with string value of "Commands" in header. File has "," separator. This file is on ESX host with Bash. Thanks, Arv (21 Replies)
Discussion started by: arv_cds
21 Replies

6. Shell Programming and Scripting

Extract columns where header matches a given string

Hi, I'm having trouble pulling out columns where the headers match a file of key ID's I'm interested in and was looking for some help. file1.txt I Name 34 56 84 350 790 1215 1919 7606 9420 file2.txt I Name 1 1 2 2 3 3 ... 34 34... 56 56... 84 84... 350 350... M 1 A A A A... (20 Replies)
Discussion started by: flotsam
20 Replies

7. Shell Programming and Scripting

Averaging Data From Multiple Columns, Using Header if Possible

Hi, I have a file with multiple tab delimited columns and I would like to have the average of each column: Iteration Tree No Lh HMean 1000 1 -78.834717 -78.834717 1100 1 -77.991031 -78.624046 1200 1 -79.416055 -78.761861 1300 1 -79.280494 -78.968099 1400 1 -82.846275 -80.808696 ... (4 Replies)
Discussion started by: mikey11415
4 Replies

8. Shell Programming and Scripting

Comparing one file header with another file header

Hi Experts, In our project we have requirement where in we have to compare header of one file with header in the parameter file. There are 20 files which we ftp from one site. All this files have different header. We are comapring this file with our parameter file(which is having the header... (2 Replies)
Discussion started by: Amey Joshi
2 Replies

9. Shell Programming and Scripting

Comparing two columns

Hi, I want to compare two columns and find out missing entries e:g Column 1 Column 2 1 1 2 2 3 13 4 10 19 234 Results woud be 13. I will appreciate very much if anyone help me :). (12 Replies)
Discussion started by: krabu
12 Replies

10. Shell Programming and Scripting

Partial Column extraction/Process/Repasting changed Columns back to Source file

I have the following requirement. file1.txt (this could contain 5 million rows) ABC 1234 XYZ .... (3000 bytes) QRD 4612 GHT .... (3000 bytes) I need to create file2.txt 1234 4612 I have a EAI process to change file2.txt into file3.txt 4555 3743 Then I would have to use... (0 Replies)
Discussion started by: jostul
0 Replies
Login or Register to Ask a Question