It is currently Mon, 25 Sep 2023 12:09:21 GMT



 
Author Message
 file manipulation to get several output file out of 1 file

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



 Mon, 21 Apr 2003 08:09:26 GMT   
 file manipulation to get several output file out of 1 file
In article <3A00B0B6.76BCA...@hp.com>,
  Jae Ellison <jae_elli...@hp.com> wrote:

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.



 Mon, 21 Apr 2003 23:26:10 GMT   
 file manipulation to get several output file out of 1 file

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



 Tue, 22 Apr 2003 01:19:37 GMT   
 file manipulation to get several output file out of 1 file
  Jae Ellison <jae_elli...@hp.com> wrote:

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



 Tue, 22 Apr 2003 01:46:04 GMT   
 file manipulation to get several output file out of 1 file

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);
close(FILE);
exit(0);

Cheers,
Laird

--
W: laird.nel...@time0.com / P: ljnel...@unix.amherst.edu
http://www.amherst.edu/~ljnelson/
Good, cheap, fast: pick two.



 Tue, 22 Apr 2003 02:05:16 GMT   
 file manipulation to get several output file out of 1 file

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.



 Tue, 22 Apr 2003 02:11:09 GMT   
 file manipulation to get several output file out of 1 file
In article <3A01A229.337B1...@hp.com>, Jae Ellison  <jae_elli...@hp.com> wrote:

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.

--
Barry Margolin, bar...@genuity.net
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.



 Tue, 22 Apr 2003 04:16:35 GMT   
 file manipulation to get several output file out of 1 file
Thank you for all your help.  Got it working...


 Tue, 22 Apr 2003 03:00:00 GMT   
 
   [ 8 post ] 

Similar Threads

1. how to make one object file from several object files

2. restore one file from a cpio backup of several files

3. Sweep several files into one text file?

4. compress one large file into several smaller files?

5. Help:how do you split a large file into several small files and

6. Getting a list of new file from several directories

7. Create batch file and send screen output to a file

8. Getting output from telnet session into a file


 
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group.
Designed by ST Software