The UNIX and Linux Forums  

Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
Google UNIX.COM


Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
search in finding position of a string in avariable smr_rashmy Shell Programming and Scripting 2 02-08-2008 07:58 PM
check position of end of line for some specific lines senthil_is Shell Programming and Scripting 1 11-08-2007 10:19 PM
How to print specific lines with awk Bugenhagen Shell Programming and Scripting 10 08-16-2007 03:41 AM
How to print at a specific position of the display efernandes Shell Programming and Scripting 1 01-23-2007 05:08 PM
How to add character in specific position of a string? victorlung Shell Programming and Scripting 5 09-01-2006 07:33 AM

Reply
 
Submit Tools LinkBack Thread Tools Search this Thread Display Modes
  #1  
Old 12-04-2006
Registered User
 

Join Date: Sep 2006
Posts: 8
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
Reply With Quote
Forum Sponsor
  #2  
Old 12-04-2006
sendai's Avatar
Registered User
 

Join Date: Dec 2006
Location: Brasil
Posts: 18
Smile sorry if i'm wrong

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

if yes this will help you.

Code:
#!/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
          }
}

}
Reply With Quote
  #3  
Old 12-04-2006
Technorati Master
 

Join Date: Mar 2005
Location: Large scale systems...
Posts: 2,610
Code:
awk '{ if( substr($0, 2, 3) ~ /patter/ ) { print } }' file
Reply With Quote
  #4  
Old 12-04-2006
Registered User
 

Join Date: Mar 2006
Location: Bangalore,India
Posts: 1,397
Code:
sed -n "/.\{99\}27/p" file
Reply With Quote
  #5  
Old 12-04-2006
Registered User
 

Join Date: Sep 2006
Posts: 1,580
Python alternative:
Code:
#!/usr/bin/python
for line in open("file.txt"):
     if line[99:101] == "27":
        print line
Reply With Quote
Google The UNIX and Linux Forums
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes




All times are GMT -7. The time now is 09:13 PM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited.
The UNIX and Linux Forums Content Copyright ©1993-2008. All Rights Reserved.Ad Management by RedTyger Visit The Complex Event Processing Blog

Content Relevant URLs by vBSEO 3.2.0