Results 1 to 5 of 5
  1. #1
    Join Date
    Feb 2004
    Posts
    37

    Unanswered: reading a column value from a text file

    I am trying to compare two values from different sources and store the result into a shell variable. FileA has 4500 records in this example. I want to count the number of records in FileA and compare that with totalcount_fileA column in FileB

    FileB file format is as follows: The file will not contain the column names.

    SourceName source_date totalcount_fileA filedate,
    Hello 12/04/2003 4500 2/04/2003


    Any help is appreciated

  2. #2
    Join Date
    Jan 2004
    Location
    Bordeaux, France
    Posts
    320
    Try and adapt the following code :

    Code:
    CountA=`wc -l FileA | awk '{ print $1}'`
    CountB=`awk 'NR==2 {print $3;next}' FileB`
    if [ $CountA -eq $CountB ]
       then echo "Ok"
       else echo "Counts does'nt match"
    fi
    Jean-Pierre.

  3. #3
    Join Date
    Jun 2002
    Location
    UK
    Posts
    525

    Re: reading a column value from a text file

    FileB file format is as follows: The file will not contain the column names.

    SourceName source_date totalcount_fileA filedate,
    Hello 12/04/2003 4500 2/04/2003
    Are you saying that there may be many FileA, and FileB contains the expected record count for each FileA value?

    If so, this might help you...
    Code:
    awk '
    {
     fname=$1; fcount=$3
     "wc -l "fname | getline # this pipes the system call to 'wc'
                             # the stdout is read in to replace $0
     print fname " - Expected count: "fcount"; Actual Count: "$1
    }' FileB
    Damian

  4. #4
    Join Date
    Jan 2004
    Location
    Bordeaux, France
    Posts
    320
    Sorry, FileB not contain the column names.

    Code:
    CountA=`wc -l FileA | awk '{ print $1}'`
    CountB=`awk '{print $3;exit}' FileB`
    if [ $CountA -eq $CountB ]
       then echo "Ok"
       else echo "Counts does'nt match"
    fi
    Jean-Pierre.

  5. #5
    Join Date
    Feb 2004
    Posts
    37
    Thanks, it worked. I appreciate.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •