1826214 Members
3135 Online
109691 Solutions
New Discussion

vi question

 
navin
Super Advisor

vi question

Hello,
I have minimaly used sed and awk stuffs.
Now i have a file which has a user data seperated with :
In which i have to replace a coulmn E(has user dept) with new information. some users have multiple entries in the file . How can i script to replace a particular column with new info (i have a file with the new info)
Thanks in advance
Learning ...
3 REPLIES 3
Dennis Handly
Acclaimed Contributor

Re: awk question

What is your "key" for info you want to take from that other file? You may want to look at join(1), after you sort the two files on their key.

If the number of lines in your two files is not too big, you could read your new file with awk and make an associative array then read in your data file and just replace that column E with data in the array that matches.

If your separator is a ":", then you want awk -F:

Rasheed Tamton
Honored Contributor

Re: vi question

Hello Navin,

Looks like you want to modify /etc/passwd.

A quick one came to my mind is here. Below you are excluding the column 5 (column E) from the original file and redirect the result to file1

#awk -F: 'BEGIN { OFS=":" } { $5 = ""; print }' /etc/passwd > file1

Now you are merging the file1 with the newinfo file which contains the new info and redirect that result to a file called result

#paste -d: file1 newinfo > result

Finally you are extracting
#awk -F: 'BEGIN { OFS=":" } {print $1, $2, $3, $4, $8, $6, $7}' result > final


#diff /etc/passwd final


Regards,
Rasheed Tamton.
Sandman!
Honored Contributor

Re: vi question

A sample of the replacement file and the file to be replaced posted here will help in providing an accurate solution. And as mentioned the files need a common key on which they need to be joined in order for the substitutions to take place.