- Community Home
- >
- Servers and Operating Systems
- >
- Operating Systems
- >
- Operating System - HP-UX
- >
- Re: extracting a portion from a file
Categories
Company
Local Language
Forums
Discussions
Forums
- Data Protection and Retention
- Entry Storage Systems
- Legacy
- Midrange and Enterprise Storage
- Storage Networking
- HPE Nimble Storage
Discussions
Forums
Discussions
Discussions
Discussions
Forums
Discussions
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
- BladeSystem Infrastructure and Application Solutions
- Appliance Servers
- Alpha Servers
- BackOffice Products
- Internet Products
- HPE 9000 and HPE e3000 Servers
- Networking
- Netservers
- Secure OS Software for Linux
- Server Management (Insight Manager 7)
- Windows Server 2003
- Operating System - Tru64 Unix
- ProLiant Deployment and Provisioning
- Linux-Based Community / Regional
- Microsoft System Center Integration
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Community
Resources
Forums
Blogs
- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Printer Friendly Page
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-14-2003 11:41 AM
11-14-2003 11:41 AM
I have a file of around 500 lines. I want to extract a portion of the file say from the line with the pattern 'abc' to the line with pattern 'efg' which might span for around 100 lines and redirect the output to a file. In case the file has got more occurances of the combination, the script should be able to extract all of them and dump into separate files.
Can anyone please help me in this problem.
Thanks,
Andy
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-14-2003 12:50 PM
11-14-2003 12:50 PM
Re: extracting a portion from a file
EXTRACT=N
NN=1
while read LINE
do
echo $LINE|grep abc
if [ $? -eq 0 ]; then
EXTRACT=Y
OUTFILE=file${NN}
fi
echo $LINE|grep efg
if [ $? -eq 0 ]; then
EXTRACT=N
NN=$(expr $NN + 1)
fi
if [ "X${EXTRACT}" = "XY" ]; then
echo $LINE > $OUTFILE
fi
done < mysourcefile.dat
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-16-2003 03:53 AM
11-16-2003 03:53 AM
Re: extracting a portion from a file
here an awk script:
BEGIN{COUNT=0;DOPRINT=0;}
/abc/ {DOPRINT=1;COUNT=COUNT+1;OUTPUTFILE="text." COUNT;}
{
if (DOPRINT==1)
{
print $0 > OUTPUTFILE;
}
}
/efg/ {DOPRINT=0;}
END{}
greetings,
Michael
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-16-2003 10:59 PM
11-16-2003 10:59 PM
Solutionawk '/abc/,/efg/' yourfile > newfile
will print to a single file, but if you want a new file each time you hit abc it's a bit more involved.
awk '
/abc/ {nf=sprintf("newfile.%d", ++matchcount)}
/abc/,/efg/ {print >>nf}
' yourfile
This will create files called newfile.1, newfile.2 etc.
-- Graham
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-17-2003 12:00 AM
11-17-2003 12:00 AM
Re: extracting a portion from a file
BEGIN {myflag=0;}
/abc/ {myflag=1;}
myflag==1 {print $0}
/def/ {exit;}
Now run this as follows:
awk -f extract.awk < inputfile > outputfile
NOTE: The above will quit after the first occurrence of def following the first occurrence of abc.
If you want to find multiple sections in a file (i.e. the lines from "abc" to "def" and then skipped some lines until we found another "abc" / "def" section. Then just the change "exit" above to "myflag=0".
Best regards,
Kent M. Ostby
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-17-2003 03:06 AM
11-17-2003 03:06 AM
Re: extracting a portion from a file
Rory
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-17-2003 05:00 AM
11-17-2003 05:00 AM
Re: extracting a portion from a file
in case you havent noticed, mine is an awk script. ;-)
But yours is elegant and highly optimzed, very efficient a Borg would say. ;-)
greetings,
Michael
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-17-2003 07:16 AM
11-17-2003 07:16 AM
Re: extracting a portion from a file
Looking forward for some help.
Thanks,
Andy
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-17-2003 08:56 AM
11-17-2003 08:56 AM
Re: extracting a portion from a file
abc{other stuff}def
lines scattered in it and you only want the _lines_ that begin abc and end in edf?
grep -e "^abc.*def$" myfile
Will provide those lines.
However, if your file is like
abc (stuff)
more stuff
...
...
def (stuff)
Then the scripts that have already should work.
If your expect to have
abc stuff
more stuff
def
more stuff
abc stuff
more stuff
def
and you want each block listed,then try modifying the submitted scripts so that a abc match turns on the print flag and a def turns it off.
In perl it would look like
#!/usr/local/bin/perl -w
use IO::File;
$printit=0;
$filename=shift(@ARGV);
$file = new IO::File;
$file->open("<$filename");
while( ($linein=$file->getline) ){
if ($linein =~ /abc/){
printit=1;
}
if (printit == 1){
print "$linein\n";
if($linein=~ /def/){
printit=0;
}
}
}
$file->close;
run it with my_script my_file
Hope this helps
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-17-2003 09:16 AM
11-17-2003 09:16 AM
Re: extracting a portion from a file
abc
some stuff
...
..
def
abc
some stuff
abc
some stuff
abc
some stuff
abc
some stuff
...
..
def
I want the script to extract the portion of the file which starts with the pattern 'abc' and ends with the pattern def. But now the scripts extracts the lines that starts with 'abc' also.
In some files there are only one occurance of 'abc' and 'def'. In those cases the scripts work exactly fine.
-Andy
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-17-2003 09:23 AM
11-17-2003 09:23 AM
Re: extracting a portion from a file
Example-
1 abc one
2 abc two
3 something
4 def one
And you want lines 2 - 4, then maybe this one line perl script can do it.
perl -ne 'if (/abc/) { @a=(); $p=1; } ; { push(@a,$_); }; if (/efg/) { print @a if $p; @a=(); $p=0 }' yourfile
HTH
-- Rod Hills
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-17-2003 09:25 AM
11-17-2003 09:25 AM
Re: extracting a portion from a file
-- Rod Hills
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-17-2003 10:05 AM
11-17-2003 10:05 AM
Re: extracting a portion from a file
This seems to work fine but I want each extract to be put in a separate file. How can I accomplish that.
-Andy
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-17-2003 10:13 AM
11-17-2003 10:13 AM
Re: extracting a portion from a file
perl -ne 'if (/abc/) { @a=(); $p=1; } ; { push(@a,$_); }; if (/efg/) { if ($p) { $n++; open OUT ">outfile.$n"; print @a; close OUT; @a=(); $p=0 }' yourfile
-- Rod Hills
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-17-2003 10:14 AM
11-17-2003 10:14 AM
Re: extracting a portion from a file
perl -ne 'if (/abc/) { @a=(); $p=1; } ; { push(@a,$_); }; if (/efg/) { if ($p) { $n++; open OUT ">outfile.$n"; print @a; close OUT; @a=(); $p=0 }}' yourfile
-- Rod Hills
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-17-2003 10:32 AM
11-17-2003 10:32 AM
Re: extracting a portion from a file
Missing comma after first argument to open function at -e line 1, near "">outfil
e.$n";"
Execution of -e aborted due to compilation errors
Can you please help me out in this
-Andy
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-17-2003 07:27 PM
11-17-2003 07:27 PM
Re: extracting a portion from a file
Mine is an awk script too.
Andy, so you want to
- start capturing when you hit abc
- if you hit another abc, start capturing again.
- when you hit a def, print all lines since the most recent abc
Here goes. I am doing this via a script file rather than in-line:
Create a file, eg andy.awk, containing:
/^abc/ {
split("",list) #empty out the array.
el=0
list [++el] = $0
next }
(el > 0) { list [++el] = $0 }
/^def/ {
nf=sprintf("newfile.%d", ++matchcount)
for (i=1;i<=el;i++)
print list[i] >> nf
}
Invoke this with awk -f andy.awk yourfile, it will create newfile.1, newfile.2 etc.
Hope that closes it.
-- Graham
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-17-2003 09:07 PM
11-17-2003 09:07 PM
Re: extracting a portion from a file
BEGIN{COUNT=0;DOPRINT=0;}
/abc/\
{if (DOPRINT==0)
{
DOPRINT=1;
COUNT=COUNT+1;
OUTPUTFILE="text." COUNT;
}
{
if (DOPRINT==1)
{
print $0 > OUTPUTFILE;
}
}
/efg/ {DOPRINT=0;}
END{}
this should do the trick.
greetings,
Michael
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-17-2003 09:52 PM
11-17-2003 09:52 PM
Re: extracting a portion from a file
# perl -ne '/abc/.../efg/ and print' file>excerpt
What I dont see in your quest is what should hppen when matches overlap
abc
x
abc
efg
efg
that's the diff in perl between /pat/../pat/ and /pat/.../pat/ both not very well know options. Unless you state what your wish is about overlapping matches, it's hard to split into different files.
Basics could be like:
if (/abc/../def/) {
unless ($out) { open $out, ">file$." or die "file$.: $!" }
print $out;
}
else {
$out and close $out;
undef $out;
}
Which is far more easy than the previous posted perl solutions. Rodney's solutions are correct and simple (as usual), but I think I should at least draw they attention to these lesser known, but very powerful features.
Enjoy, have FUN! H.Merijn
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-18-2003 04:47 AM
11-18-2003 04:47 AM
Re: extracting a portion from a file
have you checked on the latest posts?
greetings,
Michael
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-18-2003 05:15 AM
11-18-2003 05:15 AM
Re: extracting a portion from a file
perl -ne 'if (/abc/) { @a=(); $p=1; } ; { push(@a,$_); }; if (/efg/) { if ($p) { $n++; open OUT,">outfile.$n"; print OUT @a; close OUT; @a=(); $p=0 }}' yourfile
I guess I should have tested it before posting. Sorry...
-- Rod Hills
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-18-2003 05:26 AM
11-18-2003 05:26 AM
Re: extracting a portion from a file
Thanks again for all your help.
-Andy.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-18-2003 09:16 AM
11-18-2003 09:16 AM
Re: extracting a portion from a file
I am back again with one more question:
I have used Graham's solution to solve my problem. I am using awk -f andy.awk
Can anyone please help me in this problem.
Thanks,
Andy
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-18-2003 09:53 AM
11-18-2003 09:53 AM
Re: extracting a portion from a file
cat * | awk andy.awk
This way all the files would appear as one stream into the script, thus creating unique filenames.
HTH
-- Rod Hills
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-18-2003 07:26 PM
11-18-2003 07:26 PM
Re: extracting a portion from a file
Change the awk script slightly to have the filename passed into it.
Change the line
nf=sprintf("newfile.%d", ++matchcount)
to
nf=sprintf("%s.%d", fname, ++matchcount)
and invoke with
awk -v fname=$file -f andy.awk $file
it will create $file.1, $file.2, etc...
-- Graham