Print lines with search string at specific position

Hi Folks,

I have a file with all fields defined by byte position, but any field can be empty so I cannot print lines based on a search of specific columns. I need to print all lines of this file where the string of two characters at byte position 100-101 contains the number 27. Any ideas?

Sample file:

...abc def 987lm
...tia fer g270
... gerta27mlonep
...ferssdfsdff22

I would print the second and third lines.

Thanks

1 Like

but you want print lines where the char 100=2 and 101=7

if yes this will help you.

#!/usr/bin/perl
if(@ARGV eq ""){
print "Usage = ./$0 <FILE>\n";
exit 0;
}
$fl=join(" ",@ARGV);
open(FD,"< $fl")|| die "Can't read $fl\n";
@lines=<FD>; #read the lines
close(FD);
foreach(@lines){ #d�h
@chars=split(//,$_); #get chars of a line
if($chars[99] eq "2"){ #cause arrays begin in 0
          if($chars[100] eq "7"){
          print "$_"; #uhh we print the line :D
          }
}

}


:slight_smile:

awk '{ if( substr($0, 2, 3) ~ /patter/ ) { print } }' file
sed -n "/.\{99\}27/p" file

Python alternative:

#!/usr/bin/python
for line in open("file.txt"):
     if line[99:101] == "27":
        print line