I have a file with lines something like.
......
123_start
......
.......
123_end
....
.....
456_start
......
.....
456_end
....
.....
789_start
....
....
789_end
if i give the input as $a = _start and $b = _end, it should split the contents and save as different files like,
123_start
..
...
123_end
as one file with filename as 123. from the above example it should give 3 different files as 123, 456, 789.
Please let me know the solution.
For now i am executing everytime to get a file individually as given below. i need a replacement to simplify my task.
a=12345_start
b=12345_end
awk '/'"$a"'/,/'"$b"'/' in > out
Try this:
awk -F_ '$2=="start"{f=$1} f{print>f} $2=="end"{close(f);f=x}' infile
Thanks for the reply. It is working well.
But I am extremely sorry for not explaining my requirement clearly.
The example will look like
......test message.....
some junk letters......123_start....to fill this line
......
.......
some junk.....123_end...bla..bla..
....
.....
......test message.....
some junk letters......456_start....to fill this line
......
.......
some junk.....456_end...bla..bla..
....
.....
......test message.....
some junk letters......789_start....to fill this line
......
.......
some junk.....789_end...bla..bla..
....
.....
The output is expected to be in 123 file as follows.
some junk letters......123_start....to fill this line
......
.......
some junk.....123_end...bla..bla..
Are you trying to split apart an email message?
No. this is a text file from which i need to split the data into multiple files.
awk -F'_start|_end' '/_start/{f=$1;sub(/.*[^0-9]/,x,f)} f{print>f} /_end/{close(f);f=x}' infile
In PERL:
while (<>) {
my ($n) = m{(\d+)_start};
next unless defined $n;
open FH, "> $n.file" or die $n;
print FH $_;
while (<>) {
print FH $_;
last if m{_end};
}
close FH;
}