How to check field formatting of input file?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting How to check field formatting of input file?
# 8  
Old 09-28-2010
That is because the field separator is not whitespace, but a semicolon. Try this:
Code:
while read line
do
   nrfields=$(IFS=\;; set -- $line; echo $#)
   if [ $nrfields -ne 4 ]; then
     printf "Wrong number of fields: \n%s\n" "$line"
   fi
done <  infile


Last edited by Scrutinizer; 09-28-2010 at 03:45 AM..
# 9  
Old 09-28-2010
Yes, Its working.
But my criteria is something different.
My input file data is,
Code:
abcdefghij;20100903040607;1234567891;GLOBAL;
04;ABC123;09;XYZ456;PQR6789;VRAI;;;;;;
04;EFG456;09;STU123;WXYZ786;FAUX;;;;;;
04;MNO101;09;LMN489;PQRS789;VRAI;;;;;;

1] First line should have 4 fields and need to check for last field value.
Solution is,
Code:
               line=`head -1 $FILE`
		echo "Line is : $line"
		nrfields=$(IFS=\;; set -- $line; echo $#)
		if [ $nrfields -ne 4 ]; then
		   echo "Wrong number of fields: $line"
		else
		FileType=`echo $line | cut -d ";" -f 4`
		echo "File Type is $FileType"
		fi

2] From 2nd line onward fields are 11 and all fields have values/not.
3] Need to check 6th fieldof each line. If 6th field value is VRAI, then values of 2nd and 4th fields from that line should be stored in an arraylist.

Please help me out to solve this 2 problems above.

Last edited by Franklin52; 09-28-2010 at 07:03 AM.. Reason: Please use code tags!
# 10  
Old 09-28-2010
I would do something like this.
bash code:
  1. #!/bin/bash
  2. FILE=infile
  3. IFS=\;
  4. {
  5.    read -a L
  6.    if &#91; ${#L[@]} -ne 4 ]
  7.    then
  8.       echo "Wrong number of fields"
  9.    else
  10.       FileType="${L[3]}"
  11.       echo "File Type is $FileType"
  12.       while read -a L
  13.       do
  14.          if &#91; ${L[5]} = VRAI ]
  15.          then
  16.             echo "Elem1='${L[1]}' Elem2='${L[3]}'"
  17.          fi
  18.       done
  19.    fi
  20. } <$FILE
Please tell us how the stored array should look like.
2 arrays with 1 record per matching line ?
# 11  
Old 09-28-2010
I am getting error as,
Code:
read: 0403-010 A specified flag is not valid for this command.
for line,
if [ ${#L[@]} -ne 4 ]


Last edited by Franklin52; 09-28-2010 at 07:03 AM..
# 12  
Old 09-28-2010
bash code:
  1. #!/bin/bash
  2. FILE=infile
  3. L=$(head -1 $FILE)
  4. if &#91; $(IFS=\;; set -- $L; echo $#) -ne 4 ] # As suggested by Scrutinizer
  5. then
  6.    echo "Wrong number of fields : $L"
  7. else
  8.    FileType="$(echo $L | cut -d\; -f4)"
  9.    echo "File Type is $FileType"
  10.    awk -F';' '$6 == "VRAI" { print $2, $4'} infile
  11. fi
But sure a simple awk script woul do the best Smilie
# 13  
Old 09-28-2010
but I need shell script solution for 2 and 3 points mentioned above.
# 14  
Old 09-28-2010
Quote:
Originally Posted by Poonamol
but I need shell script solution for 2 and 3 points mentioned above.
Do you need to check that there are 11 fields ? and what kind of action if not ?
What form of arraylist ? Please, give an example.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Script to check field value from a file records

I need a script to check the records in a file , if any value match transfer the record in error.txt file. 1- If any of the any field value is NULL(nothing in this field) Record1|Record2|Record3|Record4|Record5|DATE1|DATE2 Example: 11111111|22222222|NULL|12|444|27042018|27042018... (8 Replies)
Discussion started by: vivekn
8 Replies

2. Shell Programming and Scripting

Check if 2 input values exists in a file

I have a file number.txt.I need to get 2 inputs from the terminal like a=100 and b=200.If a and b are there in the file,then check if a < b,print "less".If a is not there in the file,print "a is missing" or if b is not there in the file,print "b is missing". number.txt: 100 200 300 (2 Replies)
Discussion started by: aneeta13
2 Replies

3. Shell Programming and Scripting

Check input file with different criteria

HI Input file.txt ABCDE1 JFHFJFJF3 10 ABCDE2 JFHFJFJF5 20 ABCDE3 JFHFJFJF5 30 ABCDE4 JFHFJFJF6 - ABCDE5 JFHFJFJF6 20 ABCDE6 JFHFJFJF6 90 ABCDE7 JFHFJFJF6 9 ABCDE8 JFHFJFJF6 I want to check third column if data missing or wrong data the echo massage and out from script. 1.... (8 Replies)
Discussion started by: pareshkp
8 Replies

4. UNIX for Dummies Questions & Answers

Find and add a field for an input file

Hello Sir/Madam, I'm new to UNIX and wondering if I can process a find & add using AWK command in UNIX? requirements: I have to find *.JPG field for each line in the input file and add it as a separate field for each line.. for ex: 0000684631,E,3088017,7... (10 Replies)
Discussion started by: V1l1h1
10 Replies

5. Shell Programming and Scripting

Input handling and formatting input to shell

i want to get input and depending on it create new commands for input to expect. But problem is that after giving date or month as 01-09 it is interpretation as 1-9 echo -n "ENTER DATE " read d1 echo -n "ENTER MONTH " read m1 echo -n "ENTER YEAR" read y1 o=1 i=1 d2=`expr $d1... (1 Reply)
Discussion started by: sagar_1986
1 Replies

6. Shell Programming and Scripting

Formatting and combining fields of the input file

Hi, I have a file of the following format: AV 103 AV 104 AV 105 AV 308 AV 517 BN 210 BN 211 BN 212 BN 218 and the desired output is : AV 103-105 3 AV 308 1 AV 517 1 BN 210-212 3 (5 Replies)
Discussion started by: rochitsharma
5 Replies

7. Shell Programming and Scripting

Input file check

Hi, I have a script which runs daily. It gets 3 input files test1,test2,test3. I want to do a validation in my script to make sure i have all the 3 files available before running. If any one of the file is missing i want to break the script. Could you please help me with this request. ... (1 Reply)
Discussion started by: Krrishv
1 Replies

8. Shell Programming and Scripting

avoid open file to check field.

Hi Everyone, # cat a.txt 94,aqqc,62345907, 5,aeec,77, # cat 1.pl #!/usr/bin/perl use strict; use warnings; use Date::Manip; open(my $FA, "/root/a.txt") or die "$!"; while(<$FA>) { chomp; my @tmp=split(/\,/, $_); if (index($tmp, "qq") ne -1) { ... (4 Replies)
Discussion started by: jimmy_y
4 Replies

9. Shell Programming and Scripting

check presence of input file

My script is taking a file "input.in" as input for running the script. My worry is that i need to execute the script only if the file is present, if it's not don't perform the next commands. Just to have a check at the beginning of the script : If "input.in" exists, then go on. If it's does not... (4 Replies)
Discussion started by: newpromo
4 Replies

10. Shell Programming and Scripting

check for the value of one particular field and give output in a different file

hi i need to check for the value of one particular field : in the output file. the file may contain many such records as below how to ???? *** Throttled with base name + key params! : : -518594328 : les.alarm.LBS12005 : les.alarm.LBS12005 : les : lessrv1 : les : 2328 : 0... (7 Replies)
Discussion started by: aemunathan
7 Replies
Login or Register to Ask a Question