file manipulation to get several output file out of 1 file

file manipulation to get several output file out of 1 file

Post by Jae Elliso » Fri, 03 Nov 2000 09:09:26



I am trying to manipulate a file such that I can get several output
depending on first column of my file.

I have 2 columes in my file.

x   x1

x   x2

x   x3

y    y1

y    y2

...

I want to create several files such that file x(first column) contains
x1, x2, x3...(2nd column).

Thanks for your help.

Jae

 
 
 

file manipulation to get several output file out of 1 file

Post by Brian Huntle » Sat, 04 Nov 2000 00:26:10




Quote:

> --------------0DF9903CC42DBC848F064C80
> Content-Type: text/plain; charset=us-ascii
> Content-Transfer-Encoding: 7bit

> I am trying to manipulate a file such that I can get several output
> depending on first column of my file.

> I have 2 columes in my file.

> x   x1

> x   x2

> x   x3

> y    y1

> y    y2

> ...

> I want to create several files such that file x(first column) contains
> x1, x2, x3...(2nd column).

What course is this homework for?

cat yourfile|while read file data
do
 echo data >> file
done

There are several side effects to this, of course. Like, what happens if
there's a third column? What if file x already exists? What if the
inpout file is listed in the data as a file to create?

Sent via Deja.com http://www.deja.com/
Before you buy.

 
 
 

file manipulation to get several output file out of 1 file

Post by Jae Elliso » Sat, 04 Nov 2000 02:19:37





> > --------------0DF9903CC42DBC848F064C80
> > Content-Type: text/plain; charset=us-ascii
> > Content-Transfer-Encoding: 7bit

> > I am trying to manipulate a file such that I can get several output
> > depending on first column of my file.

> > I have 2 columes in my file.

> > x   x1

> > x   x2

> > x   x3

> > y    y1

> > y    y2

> > ...

> > I want to create several files such that file x(first column) contains
> > x1, x2, x3...(2nd column).

> What course is this homework for?

> cat yourfile|while read file data
> do
>  echo data >> file
> done

> There are several side effects to this, of course. Like, what happens if
> there's a third column? What if file x already exists? What if the
> inpout file is listed in the data as a file to create?

> Sent via Deja.com http://www.deja.com/
> Before you buy.

Of course my problem was simplied, but basically my file had 2+ columns.
1st column had values that I want to use as unique file name and store the
2nd column to the first value. I've tried various ways, but I have not been
able to create file name as 1st column value.  I am listing several ways
I've tried that didn't work

cat file | while read file
do
  awk'{print $2}' >> $1_output
done

---
cat file | while read file
do
  awk'{if ( $1 == $x ) print $2 >> $1_output
            else x = $1}'
done

 
 
 

file manipulation to get several output file out of 1 file

Post by Matthew Sams --ISC--x47 » Sat, 04 Nov 2000 02:46:04



Quote:

> I am trying to manipulate a file such that I can get several output
> depending on first column of my file.

> I have 2 columes in my file.

> x   x1
> y    y1
> I want to create several files such that file x(first column) contains
> x1, x2, x3...(2nd column).

If you don't mind reading the file twice, you could do this
awk '/^x/{print $0}' /tmp/testdata > /tmp/xtestdata.out
awk '/^y/{print $0}' /tmp/testdata > /tmp/ytestdata.out

It does no error checking and could probably be done much better.

-Matthew

 
 
 

file manipulation to get several output file out of 1 file

Post by Laird Nelso » Sat, 04 Nov 2000 03:05:16



> Of course my problem was simplied, but basically my file had 2+ columns.
> 1st column had values that I want to use as unique file name and store the
> 2nd column to the first value.

Warning; untested code follows.

#!/usr/local/bin/perl -w

open(FILE, "yourfile") or die "Cannot open yourfile: $!\n";
while(<FILE>) {
  chomp();
  my ($newfile, $rest) = split(' ', $_, 2);
  unless (open(NEWFILE, ">>${newfile}_output")) {
    warn "Cannot open ${newfile}_output for writing: $!\n";
    next;
  }
  print NEWFILE "$rest\n";
  close(NEWFILE);

Quote:}

close(FILE);
exit(0);

Cheers,
Laird

--

http://www.amherst.edu/~ljnelson/
Good, cheap, fast: pick two.

 
 
 

file manipulation to get several output file out of 1 file

Post by Jae Elliso » Sat, 04 Nov 2000 03:11:09




> > I am trying to manipulate a file such that I can get several output
> > depending on first column of my file.

> > I have 2 columes in my file.

> > x   x1
> > y    y1
> > I want to create several files such that file x(first column) contains
> > x1, x2, x3...(2nd column).

> If you don't mind reading the file twice, you could do this
> awk '/^x/{print $0}' /tmp/testdata > /tmp/xtestdata.out
> awk '/^y/{print $0}' /tmp/testdata > /tmp/ytestdata.out

> It does no error checking and could probably be done much better.

> -Matthew

The problem is that I do not know the value of column 1.  For example, I
have repeating values(which I do not know what they are) in column 1, but
have different values in column 2.

So my input file looks like this:
x    test
x    test 2
y    y1
y    y2
y    y3
z    z1
....
and  I want to write a script that will generated output files(3+ output
files from above example).  The first output file is called "x_file" and
has 2 rows "test", "test 2".  The second output file is called "y_file" and
has 3 rows "y1", "y2", "y3" etc.
I want the output file to be the name that includes the 1st column value.

 
 
 

file manipulation to get several output file out of 1 file

Post by Barry Margoli » Sat, 04 Nov 2000 05:16:35




>> cat yourfile|while read file data
>> do
>>  echo data >> file
>> done

>Of course my problem was simplied, but basically my file had 2+ columns.
>1st column had values that I want to use as unique file name and store the
>2nd column to the first value. I've tried various ways, but I have not been
>able to create file name as 1st column value.

Except for the fact that he forgot the '$' in $data and $file, what's wrong
with the script that Brian posted above?

If the file can have more than two columns and you only want to write the
2nd column to the new files, change the read to "read file data rest".
$file will contain the 1st column, $data will contain the 2nd column, and
$rest will contain everything else, which you will simply ignore.

--

Genuity, Burlington, MA
*** DON'T SEND TECHNICAL QUESTIONS DIRECTLY TO ME, post them to newsgroups.
Please DON'T copy followups to me -- I'll assume it wasn't posted to the group.

 
 
 

file manipulation to get several output file out of 1 file

Post by Jae Elliso » Sat, 04 Nov 2000 04:00:00


Thank you for all your help.  Got it working...