Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to concatenate 2 files using awk 3

Status
Not open for further replies.

pdtak

Technical User
Feb 25, 2008
63
0
0
US
I have to select a field ($8) from file1, then add an entry to the file2 in the correct format:

/tmp> cat file1
3001-233 Invalid admin value in /etc/security/group for "imnadm"
3001-233 Invalid admin value in /etc/security/group for "perfmon"
3001-233 Invalid admin value in /etc/security/group for "extract"
3001-233 Invalid admin value in /etc/security/group for "vality"
3001-233 Invalid admin value in /etc/security/group for "profadm"
3001-233 Invalid admin value in /etc/security/group for "db2asgrp"
3001-233 Invalid admin value in /etc/security/group for "db2adm"

/tmp> cat file2
system:
admin = true
staff:
admin = false
bin:
admin = true

Any ideas?

Thanks.
 
I'm not good at it yet, but this is what I have so far...

awk '
BEGIN { FS=OFS=" " }
FNR==NR
{
print $8, ":"
}
' file1 file2 > file3
 
Hi

I'm not 100% sure what you are trying to achieve.

To print the last field, just
Code:
print $NF

You probably want to get rid of the quotes, so you could try
Code:
{ last=$NF; gsub( /"/, "", last ); print last }

That will get you the last bit of file 1 but I'm not sure how your file 2 is supposed to work.

~ Michael
 
That's a good start, and wardy's tip to remove the quotes will help. You don't actually need to read in file2 as input (unless you need to do some checking whether the entry you are adding is already present, I'm presuming not). i.e. awk '<yourscript>' file1 >> file2 will append the entries that you print from your awk script to the end of file2.

Annihilannic.
 
Thanks ward66 and Annihilannic,

This is what I was trying to achieve:

awk '
{
last=$NF; gsub( /"/, "", last );
last = last ":"
print last
print " admin = false"
print " "
}
' file1 >> file2
 
Why not simply this ?
Code:
awk -F'"' '{print $2"\n        admin = false"}' file1 >> file2

Hope This Helps, PH.
FAQ219-2884
FAQ181-2886
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top