UNIX Help
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me
Go Back   Dev Shed ForumsOperating SystemsUNIX Help

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old May 10th, 2005, 01:03 PM
siva111111 siva111111 is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: May 2005
Posts: 1 siva111111 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 28 m 34 sec
Reputation Power: 0
Splitting files based on condition

I have a ~ delimited file with 8 or 9 columns i.e. in some cases, the 9th column is populated. If the 9th column is not null, then data needs to go to file1, if there are only 8 columns, then file2.

The if statement below does not work - all the data goes to file1.txt. Any ideas will be appreciated. Thank you.

while read line
do
v9=`cut -f9 -d~ $filename`
if [[ -n $v9 ]];then
echo "$line" >> file1.txt
else
echo "$line" >> file2.txt
fi
done < $filename

Reply With Quote
  #2  
Old May 16th, 2005, 05:09 AM
christo's Avatar
christo christo is offline
Introspective
Dev Shed Loyal (3000 - 3499 posts)
 
Join Date: Nov 2001
Location: London, UK
Posts: 3,297 christo User rank is Second Lieutenant (5000 - 10000 Reputation Level)christo User rank is Second Lieutenant (5000 - 10000 Reputation Level)christo User rank is Second Lieutenant (5000 - 10000 Reputation Level)christo User rank is Second Lieutenant (5000 - 10000 Reputation Level)christo User rank is Second Lieutenant (5000 - 10000 Reputation Level)christo User rank is Second Lieutenant (5000 - 10000 Reputation Level)christo User rank is Second Lieutenant (5000 - 10000 Reputation Level) 
Time spent in forums: 3 Days 1 h 9 m 17 sec
Reputation Power: 104
Send a message via ICQ to christo Send a message via Yahoo to christo
well it's probably a bit late, but this is a good way to do it..

create a file called file1 which contains all your original data like this:

Quote:
one~two~three~four~five~six~seven~eight~
one~two~three~four~five~six~seven~eight~nine
one~two~three~four~five~six~seven~eight~
one~two~three~four~five~six~seven~eight~
one~two~three~four~five~six~seven~eight~
one~two~three~four~five~six~seven~eight~
one~two~three~four~five~six~seven~eight~nine
one~two~three~four~five~six~seven~eight~
one~two~three~four~five~six~seven~eight~
one~two~three~four~five~six~seven~eight~nine
one~two~three~four~five~six~seven~eight~nine
one~two~three~four~five~six~seven~eight~
one~two~three~four~five~six~seven~eight~


now create a new file called myprogram.pl containing this:

Code:
#!/usr/bin/perl

$file1="/home/chris/bin/file1";
$file2="/home/chris/bin/file2";
$file3="/home/chris/bin/file3";

open(FILE1,"<$file1");
open(FILE2,">$file2");
open(FILE3,">$file3");

while(<FILE1>){
        chomp;
        @fields=split "~";
        if($fields[8]=~m/.+/){
                print FILE3 "$_\n";
                next;
        }
        print FILE2 "$_\n";
}


make sure you change the path from /home/chris/bin/ to whatever your working directory is. Chmod the script and run it.

It will create two new files, file2 and file3 which contain your results.

hth,


christo

Reply With Quote
  #3  
Old May 19th, 2005, 09:46 AM
zlutovsky zlutovsky is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Dec 2004
Location: Prague, Czech Rep.
Posts: 117 zlutovsky User rank is Corporal (100 - 500 Reputation Level)zlutovsky User rank is Corporal (100 - 500 Reputation Level)zlutovsky User rank is Corporal (100 - 500 Reputation Level)zlutovsky User rank is Corporal (100 - 500 Reputation Level) 
Time spent in forums: 22 h 42 m 44 sec
Reputation Power: 6
Quote:
Originally Posted by christo
well it's probably a bit late, but this is a good way to do it..

create a file called file1 which contains all your original data like this:



now create a new file called myprogram.pl containing this:

Code:
#!/usr/bin/perl

$file1="/home/chris/bin/file1";
$file2="/home/chris/bin/file2";
$file3="/home/chris/bin/file3";

open(FILE1,"<$file1");
open(FILE2,">$file2");
open(FILE3,">$file3");

while(<FILE1>){
        chomp;
        @fields=split "~";
        if($fields[8]=~m/.+/){
                print FILE3 "$_\n";
                next;
        }
        print FILE2 "$_\n";
}


make sure you change the path from /home/chris/bin/ to whatever your working directory is. Chmod the script and run it.

It will create two new files, file2 and file3 which contain your results.

hth,


christo


Hi,

perl is a fine tool, but in this simple case the old awk is quite good and efficient, as well. Try this:

:
awk '
BEGIN { FS = "~" }
$9 == "" {print | "cat" ; next}
{print | "cat >&2" }
' <<!
one~two~three~four~five~six~seven~eight~
one~two~three~four~five~six~seven~eight~nine
one~two~three~four~five~six~seven~eight~
one~two~three~four~five~six~seven~eight~
one~two~three~four~five~six~seven~eight~
one~two~three~four~five~six~seven~eight~
one~two~three~four~five~six~seven~eight~nine
one~two~three~four~five~six~seven~eight~
one~two~three~four~five~six~seven~eight~
one~two~three~four~five~six~seven~eight~nine
one~two~three~four~five~six~seven~eight~nine
one~two~three~four~five~six~seven~eight~
!


This script will send all lines with empty field 9 to the stdout and all other lines in stdrerr. You can then redirect them wher ever you need.

Regards

Reply With Quote
Reply

Viewing: Dev Shed ForumsOperating SystemsUNIX Help > Splitting files based on condition


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


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

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 3 hosted by Hostway