Please help to write a executable script for extracting some parts of a file


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Please help to write a executable script for extracting some parts of a file
# 8  
Old 05-13-2009
Quote:
Originally Posted by iammitra

Code:
#!/usr/bin/perl -w

$#ARGV==0 or die "Usage: 2ndprocess-script 1st-output-file-as-inputfile\n";

$input=shift;

perl -ne '{$/=""; $i=1;
while (/^Taxonomy:.(.*?)\+{11}/msgi) {
$x = $1; $x =~ s/(^|\n)\s+/$1/g;
open(OUT,">outfile".$i++.".txt"); print OUT $x; close(OUT);
}}' $1; ## it should be $ARGV[0] or $input

and rather than creating perl script wrapper create shell script and put that perl command in it.

here is my perl code ...

Code:
#! env perl
$/ ="" ;
my $i = 1 ; 
my $file = $ARGV[0] ;

open(READ,"<$file") or die "can not open file $! \n" ;
open(WRITE,">${file}.out") or die "can not open file for writing : $!\n" ;
while(<READ>)
{
    chomp ; 
    if ( ^Taxonomy:.(.*?)\+{11}/msgi )
    {
         $x = $1; 
         $x =~ s/(^|\n)\s+/\1/g;
         $i++ ;
         print WRITE "$i\t$x\n" ;
    } 
}
close READ ; 
close WRITE ;

try if it works ...

Last edited by zedex; 05-13-2009 at 09:36 AM.. Reason: changed \1 to $1
# 9  
Old 05-13-2009
Another approach in awk (can write it in one line too of course):
Code:
awk '
     /^Taxonomy:/ {z++}
     /^Taxonomy:/,/\++End/ { if( $0 !~ /^Taxonomy:/ && $0 !~ /\++End/) {sub(/^[[:space:]]*/,""); print > "outfile"z} }
' z=0 infile


$> cat outfile1
Gammaproteobacteria: 2767
Alphaproteobacteria: 4123
Deltaproteobacteria: 1343
Epsilonproteobacteria: 26
Betaproteobacteria: 397
unclassified Proteobacteria: 48
Spirochaetes (class): 15
Nitrospira (class): 1
Bacilli: 25
Not assigned: 1445
No hits: 220253

$> cat outfile2
Gammaproteobacteria: 2809
Alphaproteobacteria: 4001
Deltaproteobacteria: 1208
Epsilonproteobacteria: 15
Not assigned: 299
No hits: 461890

# 10  
Old 05-13-2009
Thank you very much...
It works.. Smilie
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Shell script to encrypt the xls file using executable jar in Linux SUSE 11.4

Dear Experts, I am an ERP consultant and would like to learn shell script. We are working on Linux SUSE 11.4 and I am very new to shell scripting. We can manually encrypt an excel file using "executable jar" through command prompt by placing the jar file & the file to be encrypted on a physical... (1 Reply)
Discussion started by: nithin226
1 Replies

2. Shell Programming and Scripting

Incrementing parts of ten digits number by parts

I have number in file which contains date and serial number: 2013101000. The last two digits are serial number (00). So maximum of serial number is 100. After reaching 100 it becomes 00 with incrementing 10 which is day with max 31. after reaching 31 it becomes 00 and increments 10... (31 Replies)
Discussion started by: Natalie
31 Replies

3. Shell Programming and Scripting

Need to search a particular String form a file a write to another file using perl script

I have file which contains a huge amount of data. I need to search the pattern Message id. When that pattern is matched I need to get abcdeff0-1g6g-91g3-1z2z-2mm605m90000 to another file. Kindly provide your input. File is like below Jan 11 04:05:10 linux100 |NOTICE... (2 Replies)
Discussion started by: Raysf
2 Replies

4. UNIX for Dummies Questions & Answers

Extracting parts from an absolute path

Hi, How can I extract parts from an absolute path? For example : The absolute path is /dir1/dir2/dir3/dir4/dir5.I need the relative path starting with directory given as parameter : for instance if the parameter is dir3 then the result should be dir3/dir4/dir5 I need generic solution... (9 Replies)
Discussion started by: mortanon
9 Replies

5. Shell Programming and Scripting

Write an executable file in Unix

Hi, I want to write an executable file in unix env to go to a particular path instead of always typing the long path cd /app/oracle/product/10.2.0/Db_1/scripts/prejib/sample. I have tried with the below script in but not working . please help me bash-3.00$ cat a.sh #!/bin/sh ... (3 Replies)
Discussion started by: prejib
3 Replies

6. Shell Programming and Scripting

Automatically select records from several files and then run a C executable file inside the script

Dear list its my first post and i would like to greet everyone What i would like to do is select records 7 and 11 from each files in a folder then run an executable inside the script for the selected parameters. The file format is something like this 7 100 200 7 100 250 7 100 300 ... (1 Reply)
Discussion started by: Gtolis
1 Replies

7. Shell Programming and Scripting

Extracting parts of a file.

Hello, I have a XML file as below and i would like to extract all the lines between <JOB & </JOB> for every such occurance. The number of lines between them is not fixed. Anyways to do this awk? ============ <JOB APR="1" AUG="1" DEC="1" FEB="1" JAN="1" JUL="1" JUN="1" MAR="1" MAY="1"... (3 Replies)
Discussion started by: srivat79
3 Replies

8. UNIX for Dummies Questions & Answers

running command prompt executable file in shell script

hi i have file extentioned with test.vbs. i am able to run this file n execute through command promt but i dont know how to run in shell script example: file name is test.vbs which contains strSoundFile = "C:\windows\Media\Notify.wav" Set objShell = CreateObject("Wscript.Shell") strCommand... (5 Replies)
Discussion started by: atl@mav
5 Replies

9. Shell Programming and Scripting

Calling an Executable C file from the script (URGENT HELP PLX ! )

hi i'm trying to use tcl/tk on unix machine to call an executable C file .. i am trying just a simple button like this one button .list -text "LIST" -command filename pack .list -padx 10 -pady 10 but its giving me error message when i save it in a file eg script.tcl the button is... (7 Replies)
Discussion started by: phantom308
7 Replies

10. Shell Programming and Scripting

filter parts of a big file using awk or sed script

I need an assistance in file generation using awk, sed or anything... I have a big file that i need to filter desired parts only. The objective is to select (and print) the report # having the string "apple" on 2 consecutive lines in every report. Please note that the "apple" line has a HEX... (1 Reply)
Discussion started by: apalex
1 Replies
Login or Register to Ask a Question