Parse file from 2nd line in shell script

Hi,

I need to parse input file from 2nd line.
Input file contents are,

ABC123;20100913115432;2000000;NO;
04;AAA;09;DDD;601020304;AAAA;1;OPTA1;OPTA2;;;
04;BBB;09;BBB;601020304;BBBB;0;OPTB1;OPTB2;OPTB3;OPTB4;OPTB5;
04;CCC;09;DDD;601020304;CCCC;1;;;;;

For each line,
1] I need to check number of fields should be 12 and check 7th field value.

fields=$(IFS=\;; set -- $line; echo $#)
 if [ $fields -ne 12 ]; then
    echo "Incorrect number of fields"
else
   Status=`echo $line | cut -d ";" -f 7`

2] If Status -eq 1, I need to get 2nd and 4th field values of line and need to put in variable array.

 
if [ $Status -eq 1 ]; then
old=`echo $line | cut -d ";" -f 2`
new=`echo $line | cut -d ";" -f 4`
fi

Please tell me how can I do it from 2nd line of file and store values in array variable for a file?
Thanks in advance.

Something like this:

 
awk -F";" 'NR>1 && $7 == 1 { old[NR]=$2 ; new[NR]=$4 }' input_file

You can print/use the old , using END in the awk if you wish to.

Thanks,
But i need code in shell script (ksh).
also let me know the printing of all old and new values using for loop/ if loop.
Thanks in advance.

---------- Post updated at 05:34 AM ---------- Previous update was at 04:58 AM ----------

How can I write loop from 2nd line of input file till end?

simple way.

 
 
$ j=0
$ while read line
> do
> if [ $j -eq 0 ]; then
> echo "hi this is the first line"
> j=1
> else
> echo $line
> fi
> done < input_File
hi this is the first line
04;AAA;09;DDD;601020304;AAAA;1;OPTA1;OPTA2;;;
04;BBB;09;BBB;601020304;BBBB;0;OPTB1;OPTB2;OPTB3;OPTB4;OPTB5;
04;CCC;09;DDD;601020304;CCCC;1;;;;;

I am not satisfied with this solution.
Could you please let me know any other solution?

kindly find below code

while read line
do
fields=$(IFS=\;; set -- $line ; echo $#)
a=( $(IFS=\; ; set -- $line ; echo $@ ) )
Status=${a[6]}
if [ $(($fields+1)) -ne 12 ]
then
echo "Incorrect number of fields"
elif [ "$Status" -eq 1 ]
then
echo -n "${a[1]} "
echo "${a[3]}"
fi
done < infile.txt

Try to work from here
c=0
while read line
do
if [ $c -eq 0 ];then
let c=1
else
IFS=\;; set -- $line
if [ $# -ne 12 ]; then
echo "Incorrect number of fields"
else
if [ $7 -eq 1 ];then
echo Old is $2 and New is $4 # do whatever you want here
fi
fi
fi
done < file

Thanks Ahmad, but always I am getting below message,

"Incorrect number of fields" 

Let me know, what can i do?

Strange, and why?

my output was as below:-

I/P:-

ABC123;20100913115432;2000000;NO;
04;AAA;09;DDD;601020304;AAAA;1;OPTA1;OPTA2;;;
04;BBB;09;BBB;601020304;BBBB;0;OPTB1;OPTB2;OPTB3;OPTB4;OPTB5;
04;CCC;09;DDD;601020304;CCCC;1;;;;;
O/P:-

Incorrect number of fields
AAA DDD
Incorrect number of fields
CCC DDD

My output base on this data sample is

Incorrect number of fields
Status is Null
Incorrect number of fields

If I take the following data sample

ABC123;20100913115432;2000000;NO;
04;AAA;09;DDD;601020304;AAAA;1;OPTA1;OPTA2;;;
04;BBB;09;BBB;601020304;BBBB;0;OPTB1;OPTB2;OPTB3;OPTB4;OPTB5;
04;BCB;09;BCB;601020304;BBBB;1;OPTB1;OPTB2;OPTB3;OPTB4;OPTB5;
04;CCC;09;DDD;601020304;CCCC;1;;;;;

the output is

Incorrect number of fields
Status is Null
Old is BCB and New is BCB
Incorrect number of fields

No , the line you refer in below is 13 field long not 12, you can test it using:

nawk -F";" '{print NF}' infile.txt

there is a null filed in the end of the line after the last ";" .

BR

My code is,

while read line
	do
	fields=$(IFS=\;; set -- $line ; echo $#)
	echo "Fields are $fields"
	a=$(IFS=\; ; set -- $line ; echo $@)
	Status=${a[6]}
	echo "Status is $Status"
	if [ $(($fields+1)) -ne 12 ]
	then
	echo "Incorrect number of fields"
	elif [ "$Status" -eq 1 ]
	then
	echo -n "${a[1]} "
	echo "${a[3]}"
	fi
	done < $file

my output is,

Fields are 4
Status is
Incorrect number of fields
Fields are 11
Status is
Fields are 12
Status is
Incorrect number of fields
Fields are 11
Status is

I am not able to print the values of array and status variable?
Could you please help me out. Thanks in advance.

here is your problem :-

a=(  $(IFS=\; set -- $line ; echo $@)  ) 

in bash to initialize an array you need () :-

example:-

a=( 1 2 3 4 5 )

BR

;);):wink:

tail +2 $yourfile .... before parsing : so this will skip the first line

i you want to handle it as a stream, go for sed or awk

To skip the first line:

{ read line
  while read line; do
    do stuff
  done
} < infile

Hello guys,
I want to write a ksh script.
I am reading 1st line and now want to read from 2nd line onwards to get details.
Here is my code,

for file in "$INPUT_FILE_FOLDER/"AA_BB_CC*.CSV
  do
     echo ${file##*/}
     TYPE=`echo ${file##*/} | cut -d "_" -f 4`
     echo "Type is $TYPE"

            line=`head -1 $file`
	#echo "Line is : $line"
	nrfields=$(IFS=\;; set -- $line; echo $#)
	if [ $nrfields -ne 4 ]; then
	   echo "Incorrect HEADER record fields"
	else
               Date=`echo $line | cut -d ";" -f 2`
	   echo "Date is : $Date"
	   FileType=`echo $line | cut -d ";" -f 4`
	   echo "File Type is $FileType"
	fi
	
	if [ $TYPE == $FileType ]; then
	   echo "Header record is correct"
               // TO DO : read from 2nd line onwards
              // check 7th field of line
              // if its "1" then get 2nd and 4th filed values in array / variables
             // insert array value and Date value into DB table 

Could you please help me with respect to this only. As I am totally confused.

for file in "$INPUT_FILE_FOLDER/"AA_BB_CC*.CSV
do
{ IFS=\; read x date x filetype x
  if [ -z "$filetype" ]; then
    echo "Incorrect HEADER record fields"
    exit 1
  fi
  echo "Date is : $date"
  echo "File Type is $filetype"
  while IFS=\; read x var2 x var4 x x var7 x
  do
    if [ $var7 -eq 1 ]; then
      echo insertintable $var2 $var4
    fi
  done
} < "$file"
done

output:

Date is : 20100913115432
File Type is NO
insertintable AAA DDD
insertintable CCC DDD

I need to check the Field value is 12 or not.
IF field value is 12, then only want to check the 7th field else parse next line
if both are getting (fields = 12 and status = 1) then
get the values of 2nd and 4th field.

Thanks a lot.