ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
Hi
Suppose I have a text file containing some lines like the following:
Code:
text
123 456 789
234 345 456
text
234 456 567
234 456 567
Now I want to find all lines containing "text" and copy the following lines containing numbers into a new file, with "text" as header until the next "text" and so on until the end of the file.
I tried grep but could not manage to extract the interesting lines in to different files.
Since I spend a lot of time farting about with perl, I'd use a small perl program. Maybe there's a better way to do it, but here would be my approach:
Code:
#!/usr/bin/perl -w
my $fno = 65;
my $filename = undef;
my $fd;
open(OUT, ">/dev/null");
while (<>) {
print OUT;
if ( /text/ ) {
$filename = "file" . chr($fno++);
open(OUT, ">$filename") || die "cannot open $filename for writing: $!\n";
print "at line $. of input : writing to new file, $filename\n";
next;
}
}
close(OUT);
Of course, this isn't a very robust implementation - if you have more than 26 files, you'll start to get strange filenames. I'm sure you can write a nice little function to produce better filenames.
It opens the file descriptor, OUT, to the special device node /dev/null, which , on unix-like OSes, is a file which you can write to, and the output disappears into the void.
The purpose is to not output anything until a match for the pattern /text/ has been found. It's just one approach, and not a very portable one. you could also have a flag to prevent the calling of the print line until a match has been found.
data = open("file").read().split("text") #read in the whole file, split on "text"
count = 1 #for incrementing file number
for i in filter(None,data):
outfile = "file-" + str(count)
f = open(outfile, "a")
print >> f , "text"
print >> f, i.strip()
f.close()
count = count + 1
cool! Just a small thing missing yet:
suppose "text" is longer than one line. Now I want to extract the text lines, write them as header into one file, preceded by an "%" and than write the following numbers below this header like:
input file:
Code:
text
bla
bla
123 456 789
234 345 456
text
234 456 567
234 456 567
data = open("file").read().split("text") #read in the whole file, split on "text"
count = 1 #for incrementing file number
for i in filter(None,data):
i = i.strip()
outfile = "file-" + str(count)
f = open(outfile, "a")
print >> f , "%text"
if not i.strip().isdigit():
print >> f, "%" + i
else:
print >> f, i
f.close()
count = count + 1
The python solution also produces a '%' in front of the first line after 'text'. Why's that?
Code:
data = open("file").read().split("text") #read in the whole file, split on "text"
count = 1 #for incrementing file number
for i in filter(None,data):
i = i.strip()
outfile = "file-" + str(count)
f = open(outfile, "a")
print >> f , "%text"
if not i.split()[0].isdigit(): #assume only check the first column for text or number....
print >> f, "%" + i
else:
print >> f, i
f.close()
count = count + 1
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.