avoid open file to check field.

Hi Everyone,

[root@]# cat a.txt
94,aqqc,62345907,
5,aeec,77,

[root@]# 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[1], "qq") ne -1) {
                print "$_\n";
        }
}
close ($FA);

[root@]# perl 1.pl
94,aqqc,62345907,

As we can see, if i want to print out this a.txt, but without the line field 1 value contains "qq", i can use open the file method.

Wonderring if there is any more simple way to do that, more efficient one, image i have 1000lines for the a.txt? :confused:

Thanks

Can you provide a better sample of your input file?
I don't see any "qq" in field 1, assuming the fieldseparator is a comma.

Hi Frank,

the file is:
94,aqqc,62345907,
5,aeec,77,

you can see "qq" is part of line 1, field 1 "aqqc", means when field 1 contains "qq" this word, then print it out.

Thanks

As I can see "aqqc" is the second field if the fieldseparator is a comma, anyway with awk you can do something like:

$ awk -F, '($2 ~ /qq/)' file

:eek: That's amazing, it works, !~ is reverse way. :b:
Thanks Frank, it saves a lot of time compare with my method, to open the file, index grep that value.