The UNIX and Linux Forums  
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.

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 and shell scripting languages 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 10:58 PM
check position of end of line for some specific lines senthil_is Shell Programming and Scripting 1 11-09-2007 01:19 AM
How to print specific lines with awk Bugenhagen Shell Programming and Scripting 10 08-16-2007 06:41 AM
How to print at a specific position of the display efernandes Shell Programming and Scripting 1 01-23-2007 08:08 PM
How to add character in specific position of a string? victorlung Shell Programming and Scripting 5 09-01-2006 10:33 AM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 12-04-2006
HealthyGuy HealthyGuy is offline
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
  #2 (permalink)  
Old 12-04-2006
sendai's Avatar
sendai sendai is offline
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
          }
}

}
  #3 (permalink)  
Old 12-04-2006
matrixmadhan matrixmadhan is offline Forum Advisor  
Technorati Master
  
 

Join Date: Mar 2005
Location: leaf node in B+ tree
Posts: 2,944
Code:
awk '{ if( substr($0, 2, 3) ~ /patter/ ) { print } }' file
  #4 (permalink)  
Old 12-04-2006
anbu23 anbu23 is offline Forum Advisor  
Registered User
  
 

Join Date: Mar 2006
Location: Bangalore,India
Posts: 1,398
Code:
sed -n "/.\{99\}27/p" file
  #5 (permalink)  
Old 12-04-2006
ghostdog74 ghostdog74 is offline Forum Advisor  
Registered User
  
 

Join Date: Sep 2006
Posts: 2,509
Python alternative:
Code:
#!/usr/bin/python
for line in open("file.txt"):
     if line[99:101] == "27":
        print line
Closed Thread

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -4. The time now is 10:19 PM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0