![]() |
|
|
|
|
|||||||
| Forums | Portal | Register | Forum Rules | FAQ | Contribute | Members List | Arcade | Search | Today's Posts | Mark Forums Read |
| Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts here. |
|
|
||||
| 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 |
|
|
Submit Tools | LinkBack | Thread Tools | Display Modes |
|
#1
|
|||
|
|||
|
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 |
| Forum Sponsor | ||
|
|
|
#2
|
||||
|
||||
|
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
|
|||
|
|||
|
Code:
awk '{ if( substr($0, 2, 3) ~ /patter/ ) { print } }' file
|
|
#4
|
|||
|
|||
|
Code:
sed -n "/.\{99\}27/p" file
|
|
#5
|
|||
|
|||
|
Python alternative:
Code:
#!/usr/bin/python
for line in open("file.txt"):
if line[99:101] == "27":
print line
|
|||
| Google The UNIX and Linux Forums |