simple one 
Author Message
 simple one

Hi all,
is there a way to print column 1 of file 1 and column 2 of
file 2 into a new file?
cheers ingo
--
############# ingo at agnld dot uni-potsdam dot de ###########
############# http://www.*-*-*.com/ ~ingo ##########


Sat, 21 Dec 2002 03:00:00 GMT  
 simple one

Quote:

>Hi all,
>is there a way to print column 1 of file 1 and column 2 of
>file 2 into a new file?

awk 'FILENAME=="file_1" {print $1}
     FILENAME=="file_2" {print $2}' file_1 file_2 > new_file

Chuck Demas
Needham, Mass.

--
  Eat Healthy    |   _ _   | Nothing would be done at all,

  Die Anyway     |    v    | That no one could find fault with it.



Sat, 21 Dec 2002 03:00:00 GMT  
 simple one


Quote:
>is there a way to print column 1 of file 1 and column 2 of
>file 2 into a new file?

Do you mean for example

File 1
------
a 1
b 2
c 3

File 2
------
x 0
y 9
z 8

Result 1
--------
a
b
c
0
9
8

Result 2
--------
a 0
b 9
c 8

If you want Result 1, use the approach Charles Demas provided.

If you want Result 2 and all records in File 1 have the same number of
columns, use paste to combine the files then cut to write only the
desired columns. However, if File 1 has varying numbers of columns from
line to line, awk is probably better than using cut twice followed by
paste.

FNR == 1 { ++file }
FNR > rmax { rmax = FNR }
{ col[FNR, file] = $file }
END { for (i = 1; i <= rmax; ++i) print col[i, 1], col[i, 2] }

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



Sat, 21 Dec 2002 03:00:00 GMT  
 simple one
(awk '{print $1}' fileone ; awk '{print $2}' filetwo) > filethree


Quote:
> Hi all,
> is there a way to print column 1 of file 1 and column 2 of
> file 2 into a new file?
> cheers ingo
> --
> ############# ingo at agnld dot uni-potsdam dot de ###########
> ############# http://www.agnld.uni-potsdam.de/~ingo ##########



Sat, 21 Dec 2002 03:00:00 GMT  
 simple one

Quote:

> Hi all,
> is there a way to print column 1 of file 1 and column 2 of
> file 2 into a new file?
> cheers ingo
> --
> ############# ingo at agnld dot uni-potsdam dot de ###########
> ############# http://www.agnld.uni-potsdam.de/~ingo ##########

*grin*  I'm back..
using gawk, do...
gawk '{print $(ARGIND)}' file1 file2

you did say simple....
Jennifer



Fri, 27 Dec 2002 03:00:00 GMT  
 simple one


Quote:

>> Hi all,
>> is there a way to print column 1 of file 1 and column 2 of
>> file 2 into a new file?
>> cheers ingo
>> --
>> ############# ingo at agnld dot uni-potsdam dot de ###########
>> ############# http://www.*-*-*.com/ ~ingo ##########

>*grin*  I'm back..
>using gawk, do...
>gawk '{print $(ARGIND)}' file1 file2

>you did say simple....
>Jennifer

You really do have a {*filter*}ed mind.

I think I'm in love.  :-)

Chuck Demas
Needham, Mass.

--
  Eat Healthy    |   _ _   | Nothing would be done at all,

  Die Anyway     |    v    | That no one could find fault with it.



Fri, 27 Dec 2002 03:00:00 GMT  
 simple one
Now, is there an easy way to print them as two columns instead of
simply concatenating them into one column?  ...where column 1 of file
1 will be the first column in the new file and column 2 of file 2 will
be the second column in the new file.

Dan

On Mon, 10 Jul 2000 13:43:40 +0800, Jennifer Cross

Quote:


>> Hi all,
>> is there a way to print column 1 of file 1 and column 2 of
>> file 2 into a new file?
>> cheers ingo
>> --
>> ############# ingo at agnld dot uni-potsdam dot de ###########
>> ############# http://www.agnld.uni-potsdam.de/~ingo ##########

>*grin*  I'm back..
>using gawk, do...
>gawk '{print $(ARGIND)}' file1 file2

>you did say simple....
>Jennifer



Sat, 04 Jan 2003 03:00:00 GMT  
 simple one

Quote:

>Now, is there an easy way to print them as two columns instead of
>simply concatenating them into one column?  ...where column 1 of file
>1 will be the first column in the new file and column 2 of file 2 will
>be the second column in the new file.

Assuming the usual Unix toolkit, the answer is: man paste

Quote:
>>> ############# ingo at agnld dot uni-potsdam dot de ###########
>>> ############# http://www.agnld.uni-potsdam.de/~ingo ##########

>>*grin*  I'm back..
>>using gawk, do...
>>gawk '{print $(ARGIND)}' file1 file2

>>you did say simple....
>>Jennifer

Or you could extend this idea as:

# Re-implementation of "paste" in Thompson AWK
# This depends on some T-AWK specific features, including array sorting
# It should work with any number of input files - and do the right thing if
# the files do not all contain the same number of lines.
BEGIN { fmt = "%-10s" }               # Adjust as necessary
{ x[FNR][ARGI] = $0 }
END     {
        for (i in x) {
            for (j in x[i])
                printf fmt,x[i][j]
            print ""
            }
        }



Sat, 04 Jan 2003 03:00:00 GMT  
 simple one


...

Quote:
>I believe he did, at that.

>mawk '{FNR==1 && ++n; print $n}' file1 file2 file3 ...

I take it you are saying that mawk doesn't have an ARGI(ND) type variable?

In which case, you surely meant to type:

        mawk '{print $(n+=(FNR==1))}' file1 file2 file3 ...



Sun, 05 Jan 2003 03:00:00 GMT  
 simple one

Quote:


>> is there a way to print column 1 of file 1 and column 2 of
>> file 2 into a new file?
>> cheers ingo
>> --
>> ############# ingo at agnld dot uni-potsdam dot de ###########
>> ############# http://www.agnld.uni-potsdam.de/~ingo ##########

>*grin*  I'm back..
>using gawk, do...
>gawk '{print $(ARGIND)}' file1 file2

>you did say simple....

I believe he did, at that.

mawk '{FNR==1 && ++n; print $n}' file1 file2 file3 ...
--
John Savage            (for email, replace "ks" with "k" and delete "n")



Mon, 06 Jan 2003 03:00:00 GMT  
 simple one

Quote:

>Now, is there an easy way to print them as two columns instead of
>simply concatenating them into one column?  ...where column 1 of file
>1 will be the first column in the new file and column 2 of file 2 will
>be the second column in the new file.

mawk '{n+=FNR==1; if(n==1){x[NR]=$(n)} else {print x[FNR],$(n)}}' dat1 dat2

It's a good candidate for the () ? .. : .. abbreviation, but I've never
been able to get that construction to work in mawk.  :-(
--
John Savage            (for email, replace "ks" with "k" and delete "n")



Fri, 10 Jan 2003 03:00:00 GMT  
 
 [ 11 post ] 

 Relevant Pages 

1. Here is a simple one..

2. Simple one

3. Probably a simple one

4. simple one sate machine

5. Simple one for ya all

6. Problem with a piece of code (probably a simple one)

7. A kingdom for one simple ...

8. A kingdom for one simple ...

9. One Simple Type declaration Question

10. How to replace one or two words with one word with one line of awk code

11. See one, do one, teach one...

12. strip one field from one record

 

 
Powered by phpBB® Forum Software