LinuxQuestions.org
Review your favorite Linux distribution.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 09-23-2011, 01:02 PM   #1
sasanthi
LQ Newbie
 
Registered: Jul 2011
Posts: 11

Rep: Reputation: Disabled
create multiple files from one file


Hi all,

I have a file called file1.txt like:

KEGG_1
Features selected:
V2 1.00
V6 1.00
V9 1.00
V13 2.00
V36 2.00
KEGG_2
Features selected:
V2 1.00
V3 1.00
V8 1.00
V11 1.00
V45 1.00
V55 1.00
KEGG_3
Features selected:
V9 1.00
V10 2.00
V18 1.00
V29 1.00

And I want to create multiple files like:

KEGG_1.txt
Features selected:
V2 1.00
V6 1.00
V9 1.00
V13 2.00
V36 2.00

KEGG_2.txt
Features selected:
V2 1.00
V3 1.00
V8 1.00
V11 1.00
V45 1.00
V55 1.00

and KEGG_3.txt
Features selected:
V9 1.00
V10 2.00
V18 1.00
V29 1.00

I have written the following perl script:
Code:
#!/usr/bin/perl 
use strict; 
use warnings; 

my $file = shift @ARGV; 

                                               
open(IN, $file ) || die "could not open $file\n";

my %answer;
my $regex='KEGG';

#open ouf, ">$regex.txt" or die "Could not open file $regex.txt for output.$!";
foreach my $lines (<IN>) { 

    foreach ($lines =~ m/^$regex/) {

            my $key =$lines;
            open (outp, ">$key")  or  die "Could not open file $key for output.";

        if ($lines =~ m/^$regex/ || $lines =~ m/^V/) {           
            print outp $lines;
        }
            close(outp);
       
        }
}
close(IN);
but when I am running it, I am getting the following error:
"Could not open file KEGG_1
for output. at line 27, <IN> line 2538."

could someone help me? I cannot understand why?

thanks a lot!

Last edited by colucix; 09-23-2011 at 01:52 PM.
 
Old 09-23-2011, 01:17 PM   #2
AlucardZero
Senior Member
 
Registered: May 2006
Location: USA
Distribution: Debian
Posts: 4,817

Rep: Reputation: 610Reputation: 610Reputation: 610Reputation: 610Reputation: 610Reputation: 610
You aren't removing the newline.

Code:
my $key =$lines;
chomp $key;
 
Old 09-23-2011, 01:18 PM   #3
Ghostwheel
Member
 
Registered: Jun 2004
Location: Mid Michigan
Distribution: CentOS
Posts: 30

Rep: Reputation: 0
Try adding this before the output line:

touch $key
 
Old 09-23-2011, 01:54 PM   #4
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978
In addition, may I suggest a simple awk alternative?
Code:
awk '/KEGG/{file=($0 ".txt")}!/KEGG/{print > file}' file1.txt
or a simple bash code:
Code:
#!/bin/bash
while read line
do
  [[ $line =~ "KEGG" ]] && file=$line.txt || echo "$line" >> $file
done < file1.txt

Last edited by colucix; 09-23-2011 at 01:59 PM.
 
Old 09-23-2011, 02:02 PM   #5
schneidz
LQ Guru
 
Registered: May 2005
Location: boston, usa
Distribution: fc-15/ fc-20-live-usb/ aix
Posts: 5,062

Rep: Reputation: 856Reputation: 856Reputation: 856Reputation: 856Reputation: 856Reputation: 856Reputation: 856
also in bash
Code:
cat sasanthi.txt | while read line; do if [ "`echo $line | grep KEGG`" ]; then f=$line; else echo $line >> $f.txt; fi; done
 
Old 09-23-2011, 03:19 PM   #6
sasanthi
LQ Newbie
 
Registered: Jul 2011
Posts: 11

Original Poster
Rep: Reputation: Disabled
thanks a lot!
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
can I create symbolic links for multiple files simutaneously cliffyao Linux - Newbie 3 02-18-2010 03:11 PM
IPTables - How do I create multiple zone files codenjanod Linux - Software 1 10-16-2009 01:22 AM
Create a Multiple files by reading a input file and changing the contents bhargavkr Linux - Newbie 4 09-17-2009 12:37 AM
Create 1 csv file from multiple txt files richmur Programming 10 09-03-2008 01:28 PM
User tar to create multiple backup files Canmetal Linux - General 3 03-24-2006 03:56 PM


All times are GMT -5. The time now is 02:26 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration