I have a CSV file that contains the columns a,b,c,d and e and another file that contains column z. While reading line by line from the 1st file I want only fields c and d and at the same time I want to compare d with z from the second file.

z is a variable meaning, for i in catfileb do ....... Now if d is the same as z then display z, but if they are different, then always display "pin". But if z is for example longest or smallest display "lon" or "sma".

File a:

a b c d e
1 2 2 3 3
3 4 6 5 9
4 5 0 9 9

File b:


So c, d, and z are variables, so I was thinking of two loops. While reading line by line file a, get c and d, then compare with z.

  • Please include your desired output, it is very hard to understand what you mean otherwise.
    – terdon
    Jul 31, 2013 at 15:48

2 Answers 2


Assuming you want to compare lines in file b to corresponding lines in file a (i.e. that both files have the same number of lines and you're comparing line-by-line), you can use a Perl script to do this for you:

$file_a = "/path/to/file_a";
$file_b = "/path/to/file_b";
open $fa,'<',$file_a or die "Failed to open file $file_a: $!\n";
open $fb,'<',$file_b or die "Failed to open file $file_b: $!\n";
@file_a = <$fa>;
@file_b = <$fb>;
close $fa;
close $fb;
for (0..$#file_a){
    ($col_c,$col_d) = (split / /,$file_a[$_])[2,3];
    $col_z = $file_b[$_];
    $to_display = "$col_c $col_d";
    if($col_z eq $col_d){
        $to_display .= " $col_z";
        $to_display .= " pin";
        if ($col_z gt $col_c and $col_z gt $col_d ){
            $to_display .= " lon";
        elsif($col_z lt $col_c and $col_z lt $col_d) {
            $to_display .= " sma";
    print "$to_display\n";

Save the above file as /path/to/myscript (after modifying the locations of file_a and file_b to your true locations), then make it executable: chmod +x /path/to/myscript and finally invoke it: $ /path/to/myscript.

  • appreciate it but i want to use loops instead so if i can find a way to get only 2 fields of file a while reading it and then compare one of them to a field of another file Jul 31, 2013 at 11:38
  • @user2613272 Unless I misunderstand you, that's exactly what's happening in the above code with the for (0..$#file_a) loop.
    – Joseph R.
    Jul 31, 2013 at 11:39
  • All perl code should have use strict, and it should be able to compile with strict.
    – jordanm
    Jul 31, 2013 at 13:41
  • 2
    @jordanm This is highly debatable at best. While I agree with the principle in general and use it for all my scripts, I don't use it for one-off, ad-hoc code.
    – Joseph R.
    Jul 31, 2013 at 13:54

As I said in my comment, I am not entirely sure what you are trying to do, you seem to want to always print fields c and d and to compare only d with z. If so, the solution below should work.

$ paste a b | awk '{print $3,$4,$6}' | head -n 1; paste a b | tail -n +2 |
   while read a b c d e z; do 
    echo -n "$c $d"; 
    if [ "$d" -lt "$z" ]; then 
      echo "pin sma"; 
    elif [ "$d" -gt "$z" ]; then 
      echo "pin lon"; 
    else echo $z; 

Run on the example files you have provided, this gives:

c d z
2 33
6 5pin lon
0 9pin lon


Since you want an arithmetic comparison, the headers will break the script. However, I imagine you would like to have them in the final output. So, first I print them:

paste a b | awk '{print $3,$4,$6}' | head -n 1;

So, now we want to process the rest of the fields, skipping the header. So, we paste the files again, skip the headers (tail -n +2) and process the fields.

  • 1.please note that i don't have the same number of lines in both files. Aug 1, 2013 at 7:00
  • 2. second i have the files but i don't know how to assign certain fields into variables , and then use these variables . Basically at the end , what i want is yes to print cd_z Aug 1, 2013 at 7:03
  • each line in d should be compared with every line in z Aug 1, 2013 at 7:06
  • i don't want to use perl , i'm writing in bash and thank you Aug 1, 2013 at 7:11
  • and based on your code how does it know where to get the variables from which files ? Aug 1, 2013 at 7:15

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .