How to merge two files with awk

How to merge two files with awk

Post by Joel Turo » Wed, 22 Apr 1998 04:00:00



Greetings!

I am trying to create a new UNIX password file for use with a system
running BSD/OS by merging the two password files on an existing linux
server.

Basically, I'd like to create a new file called "master.passwd" where
fields number 1,3,4,5,6 and 7 are taken from the linux "/etc/passwd"
file and field number 2 is taken from the linux "/etc/shadow" file.  

I've been trying (without success) to use awk to build the new
password file, but I am having trouble understanding how to insert a
field from a second file.

The question is what strategy to use with awk so that I can take
certain fields from one file, and merge them with fields from another
file into a third new file.

I'd appreciate any insight into this problem.

Joel

 
 
 

How to merge two files with awk

Post by Charles Dem » Wed, 22 Apr 1998 04:00:00




>Greetings!

>I am trying to create a new UNIX password file for use with a system
>running BSD/OS by merging the two password files on an existing linux
>server.

>Basically, I'd like to create a new file called "master.passwd" where
>fields number 1,3,4,5,6 and 7 are taken from the linux "/etc/passwd"
>file and field number 2 is taken from the linux "/etc/shadow" file.  

>I've been trying (without success) to use awk to build the new
>password file, but I am having trouble understanding how to insert a
>field from a second file.

>The question is what strategy to use with awk so that I can take
>certain fields from one file, and merge them with fields from another
>file into a third new file.

>I'd appreciate any insight into this problem.

Take a look at the Unix join command, it should do pretty much what
you want, allowing you to take fields from two different files.

man join

Chuck Demas
Needham, Mass.

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

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


 
 
 

How to merge two files with awk

Post by Allen Kirb » Wed, 22 Apr 1998 04:00:00


You're going to kick yourself.  Forget awk and try join - it just happens
to do exactly what you are trying to do.  You just have to sort the files
first.:

sort /etc/passwd >/tmp/passwd
sort /etc/shadow >/tmp/shadow
join -t: -o1.1,2.2,1.3,1.4,1.5,1.6,1.7 /tmp/passwd /tmp/shadow

Quote:>master.passwd

-t: says to use the : as the input and output field separator
-o says to print file 1 field 1, then file 2 field 2, then file 1 field
3, etc.etc.


> Greetings!

> I am trying to create a new UNIX password file for use with a system
> running BSD/OS by merging the two password files on an existing linux
> server.

> Basically, I'd like to create a new file called "master.passwd" where
> fields number 1,3,4,5,6 and 7 are taken from the linux "/etc/passwd"
> file and field number 2 is taken from the linux "/etc/shadow" file.

> I've been trying (without success) to use awk to build the new
> password file, but I am having trouble understanding how to insert a
> field from a second file.

> The question is what strategy to use with awk so that I can take
> certain fields from one file, and merge them with fields from another
> file into a third new file.

> I'd appreciate any insight into this problem.

> Joel

--
Allen Kirby
AT&T Information Technology Services
Alpharetta, GA.
The views expressed here are mine, not my employers.
 
 
 

How to merge two files with awk

Post by EDS Systems Integratio » Wed, 29 Apr 1998 04:00:00



>Greetings!

>I am trying to create a new UNIX password file for use with a system
>running BSD/OS by merging the two password files on an existing linux
>server.

>Basically, I'd like to create a new file called "master.passwd" where
>fields number 1,3,4,5,6 and 7 are taken from the linux "/etc/passwd"
>file and field number 2 is taken from the linux "/etc/shadow" file.

>I've been trying (without success) to use awk to build the new
>password file, but I am having trouble understanding how to insert a
>field from a second file.

>The question is what strategy to use with awk so that I can take
>certain fields from one file, and merge them with fields from another
>file into a third new file.

>I'd appreciate any insight into this problem.

>Joel

Joel,

your problem of joining 2 files happens so often that someone has invented
a poweful tool already.  It is the 'join' command.

First, sort the files /etc/passwd and /etc/shadow, then join the 2 sorted
files
taking fields 1,3-7 from the first and field 2 from the second, assuming
fields
are separated by colon ":", and both files have matching set of first fields
(so that they have a key to join on):

sort /etc/passwd > /tmp/passwd
sort /etc/shadow > /tmp/shadow
join -t: -o 1.1 2.2 1.3 1.4 1.5 1.6 1.7 /tmp/passwd /tmp/shadow

This is much slicker and faster than using awk, if you have 'join' on your
system.

Regards,
Clyde Ingram, EDS UK

 
 
 

1. How to merge two files in awk??

 I am not sure if this question has been asked before...

 Supposing I have two files with three collumns in each. How do
 I merge the files and generate a single file with six or more
 collumns using shell script?  for example if File A has collumns a, c, e
 and File B has collumns b, d, f. I want to generate File C
 with collumns a,b,c,d,e,f.  Also it would be nice to be able to
 using the arithematic feature in awk...

 Finally, how do u specify the "rest of the line" in awk??

 thanks
 ashok

2. Problems with pipes (and tcl)

3. How to merge two columns of numbers from two different files?

4. linux installation question

5. how to merge two awk scripts

6. Native Java compiler for Linux ?

7. Can I merge two files into one?

8. SOLUTION to Method not Implemented Apache error

9. Sorting two files, then merging

10. Merge of two files

11. Need to regularly merge two passwd files

12. ** Merging Two Text Files Parallel **

13. Merging two TXT files