Results 1 to 3 of 3
I need to extract the 7th line below C-FM and every third line after that. How can I do it? I've tried using grep but I get all the lines ...
- 02-01-2011 #1Just Joined!
- Join Date
- Jan 2010
- Posts
- 3
Bash Script
I need to extract the 7th line below C-FM and every third line after that. How can I do it? I've tried using grep but I get all the lines in between. An example of the text I am working with is shown below.
C-FM RADIAL POINT-WISE DATA
(P,K-INF,BU)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
1 0.983 1.079 1.082 1.265 1.269 1.049 1.049 1.270 1.273 1.056 1.054 1.229 1.092 0.620 0.339
1.031 1.004 1.007 1.029 1.029 0.979 0.980 1.029 1.029 0.983 1.009 1.099 1.100 0.919 0.899
0 26402 25982 0 0 26098 25919 0 0 25197 21497 0 0 40205 44051
2 1.079 1.241 1.259 1.045 1.046 1.267 1.263 1.041 1.048 1.276 1.288 1.242 1.098 0.666 0.373
1.007 0.989 1.012 0.977 0.977 1.029 1.029 1.008 1.008 1.042 1.060 1.099 1.100 0.972 0.967
25980 24365 21125 26489 26490 0 0 21647 21556 20383 17928 0 0 30899 32017
Thanks,
K
- 02-02-2011 #2
- 02-02-2011 #3
I'm a perl guy (read: I'm insane,) and came up with this solution for you.
this will edit the file in place, while making a backup to XXXfilenameXX.backCode:#!/usr/bin/perl -w $^I = ".back"; $lineCount = 0; while (<>) { if ( $lineCount <= 0 ) { if ( m/^C-FM/ ) { $lineCount++; next; } next; } elsif ( $lineCount < 7 ) { $lineCount++; next; } elsif ( $lineCount == 7 ) { $lineCount++; print; next; } elsif ( $lineCount > 7 && $lineCount % 3 == 1) { # 7 + 3 = 10. 10 % 3 = 1. $lineCount++; print; next; } else { $lineCount++; next; } }
If you don't want to do that, get rid of the
$^I line, and it will not edit in place. It will instead print to the screen (or pipe, or file if you redirected.)
This will take both command line arguements for the file, or accept it from a pipe. So
run `/path/to/script /path/to/data` or `cat /path/to/data | OPTIONAL PIPE STUFF | /path/to/script | OTHER PIPE STUFF > output.file`
use the first if you don't know what I'm talking about.
I assume you know a bit about scripting, but tell me if you need more help.
the script should be self explanatory even without perl knowledge. it keeps looping until it find the C-FM at the beginning of the line, and then increments the linecount control variable. After that, it counts 7 lines, and prints the 7th. From there, every 3rd line will be printed.New to the internet, technical forums, or the hacker / open source community??
Read this to learn good posting habits http://www.catb.org/~esr/faqs/smart-questions.html
RHCE for RHEL version 5
RHCT for RHEL version 4


Reply With Quote
