Visit The New, Modern Unix Linux Community


Reading from one file and updating other file with constant entries


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Reading from one file and updating other file with constant entries
# 1  
Reading from one file and updating other file with constant entries

Hi All,

Thaks for the help in my last thread. I have one more question. I have two files with ldap entries in it. One file contains all the user LDAP parameter entries (26 MB) and other file contains only the user id's that have to be inactivated. Unix script has to read from the file that has user id information that has to be inactivated and update the main file with constant text. It has to update only those entries that are in the file that has the list of users to inactivate. Below is an example with input files and utput files that I need.

File 1
Code:
A:
uid: 1111
C:
D:
E:
 
A:
uid: 2222
C:
D:
E:
 
A:
uid: 3333
C:
D:
E:
 
A:
uid: 4444
C:
D:
E:

File 2
Code:
uid: 1111
uid: 3333
uid: 6666
.....

Output File

Code:
A:
uid: 1111
C:
D:
E:
nsrole: cn=nsdisabledrole,dc=wwww,dc=cccc,dc=com
nsrole: cn=nsmanageddisabledrole,dc=wwww,dc=cccc,dc=com
nsaccountlock: true
 
A:
uid: 2222
C:
D:
E:
 
A:
uid: 3333
C:
D:
E:
nsrole: cn=nsdisabledrole,dc=wwww,dc=cccc,dc=com
nsrole: cn=nsmanageddisabledrole,dc=wwww,dc=cccc,dc=com
nsaccountlock: true
 
A:
uid: 4444
C:
D:
E:


Thanks in advance
Samingla
Moderator's Comments:
Mod Comment
Please use code tags when posting data and code samples!

Last edited by vgersh99; 02-22-2011 at 05:42 PM.. Reason: code tags, please!
# 2  
Assuming there are no spaces between records in first file, like here:
Code:
A:
uid: 1111
C:
D:
E:
 <====== there should be no space here for this code to work!!
A:
uid: 2222
C:
D:
E:

Code:
awk -vRS= -vORS="\n\n" -vFS="\n" 'NR==FNR{for (i=1;i<=NF;i++){a[$i]}next}$2 in a{print $0"\nnsrole: cn=nsdisabledrole,dc=wwww,dc=cccc,dc=com\nnsrole: cn=nsmanageddisabledrole,dc=wwww,dc=cccc,dc=com\nnsaccountlock: true";next}1' file2 file1

# 3  
Thanks for the quick reply bartus11. we have that empty space after each user record. That new line divides different set of users in the file. Do we have any other option to process the file with the empty file.

Thanks,
Samingla
# 4  
Newline is fine. It is even required. What I meant, was that there should not be a single space character. Let me color it up for you:
Bad:
Code:
A:
uid: 1111
C:
D:
E:
_
A:
uid: 2222
C:
D:
E:

Good:
Code:
A:
uid: 1111
C:
D:
E:

A:
uid: 2222
C:
D:
E:

# 5  
Code:
nawk -f samig.awk file2 file1

samig.awk:
Code:
function p() {
    print "nsrole: cn=nsdisabledrole,dc=wwww,dc=cccc,dc=com" ORS \
           "nsrole: cn=nsmanageddisabledrole,dc=wwww,dc=cccc,dc=com" ORS \
           "nsaccountlock: true" ORS
}
FNR==NR{f2[$2];next}
/^uid:/ &&  $2 in f2 {f++}
!NF && f{
    p(); f=0; next
}
1
END {
  f && p()
}

# 6  
try:
Code:
awk 'NR==FNR{a[$2]=1;next}a[$2]==1{p=5}NF==0{p=0}NF>0{p--}
p==1{$0=$0 RS "nsrole: cn=nsdisabledrole,dc=wwww,dc=cccc,dc=com\nnsrole: cn=nsmanageddisabledrole,dc=wwww,dc=cccc,dc=com\nnsaccountlock: true"}1' file2 file1

# 7  
how to ignore case sensitivity

Quote:
Originally Posted by vgersh99
Code:
nawk -f samig.awk file2 file1

samig.awk:
Code:
function p() {
    print "nsrole: cn=nsdisabledrole,dc=wwww,dc=cccc,dc=com" ORS \
           "nsrole: cn=nsmanageddisabledrole,dc=wwww,dc=cccc,dc=com" ORS \
           "nsaccountlock: true" ORS
}
FNR==NR{f2[$2];next}
/^uid:/ &&  $2 in f2 {f++}
!NF && f{
    p(); f=0; next
}
1
END {
  f && p()
}


Hi,

Thanks for the update. I have a question regarding ignoring case sensitivity while searching from file 2. File 2 always has entries in CAPS. file 1 may contain the UID value in capitol or small letters. The current code is working only if it matches the case. Help in this is highly appriciated.

Thanks,
Sam

Previous Thread | Next Thread
Thread Tools Search this Thread
Search this Thread:
Advanced Search

Test Your Knowledge in Computers #187
Difficulty: Easy
The UNIX.com man page repository contains over 340,000 man page entries from both UNIX and Linux distributions.
True or False?

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Howto add a constant column to the text file

Hi, I am converting a .DBF file to pipe delimited file my requirement is like lets say my .DBF is residing in path /a/b/c/d/f/abc.DBF I need my .txt file as having a column with source _cd =f sample data in .DBF in folder "f" c1 c2 c3 1 2 3 in txt file it should be... (4 Replies)
Discussion started by: angel12345
4 Replies

2. Shell Programming and Scripting

Need to insert text(constant) at the beginning of file

I am very new to scripting and I know this request is simple but I am having no luck with it. I have a file a.dat with the following data in it. aa bb cc dd I need to run a script that will take each line of a.dat and put dsjc/ubin/ in front of each record, so the output looks like ... (2 Replies)
Discussion started by: jclanc8
2 Replies

3. Shell Programming and Scripting

awk for splitting file in constant chunks

Hi gurus, I wanted to split main file in 20 files with 2500 lines in each file. My main file conatins total 2500*20 lines. Following awk I made, but it is breaking with error. awk '{ for (i = 1; i <= 20; i++) { starts=2500*$i-1; ends=2500*$i; NR>=starts && NR<=ends {f=My$i".txt"; print >> f;... (10 Replies)
Discussion started by: mukesh.lalwani
10 Replies

4. Programming

[Makefile]File + constant problem

Hi guys, I am writing a Makefile with some strange features. What I have is: list of files *.efi list of guids (guid is just a number) one *.efi file is supposed to be used with one guid, so efi file and guid is a pair. What I need is: list of files *.fv How to make *.fv file... (1 Reply)
Discussion started by: Chrisdot
1 Replies

5. Shell Programming and Scripting

reading and updating property file

I have to do a read operation for a field in property file, which looks like follows: 123|xyz|datetime|count '|' is delimiter. Finally I managed to read the contents of property file using statement like cut -d"|" -f1 $PROPERTIES_FILE | tr '\n' ' ' <-- Reading first column But now I... (2 Replies)
Discussion started by: rakeshranjanscs
2 Replies

6. Shell Programming and Scripting

Help with reading file with values and updating another file

Hi I need some help with a task, i am an absolute newbie to any form of shell scripting and request guidance. Task: 1. Read a config file in form of name value pair ex host=www.test.com port=8080 binding="dynamic" or "other" or "something else" key1=value1 key2=value2 key3=value4... (4 Replies)
Discussion started by: mk7074
4 Replies

7. Shell Programming and Scripting

how I can add a constant to a field without changing the file format

Hi, I need to edit a file Protein Data Bank (pdb) and then open that file with the program VMD but when I edit the file with awk, it changes pdb format and the VMD program can not read it. I need to subtract 34 to field 6 ($ 6). this is a pdb file : ATOM 918 N GLY B 103 -11.855 8.675... (8 Replies)
Discussion started by: bio_
8 Replies

8. UNIX for Dummies Questions & Answers

Updating a field in a File without creating temp file's

Hi Experts, I have a requirement where i need to update the below items in file, 1. END TIME 2. PREV_STATUS For the first time the PREV_status and end time of all job the job will be sysdate & NULL reply as below, Session_name,Load Type,Frequency,Seesion End time,Prev_Status... (2 Replies)
Discussion started by: prabhutkl
2 Replies

9. Shell Programming and Scripting

awk updating one file with another, comparing, updating

Hello, I read and search through this wonderful forum and tried different approaches but it seems I lack some knowledge and neurones ^^ Here is what I'm trying to achieve : file1: test filea 3495; test fileb 4578; test filec 7689; test filey 9978; test filez 12300; file2: test filea... (11 Replies)
Discussion started by: mecano
11 Replies

10. Programming

File descriptor constant

I have a requirement to close all the file descriptors from 3 to 1024 for a particular application. Right now, this is how I do it .. for ( int i = 3 ; i <= 1024; ++i ) close(i); The change I am looking at is, I want to do away with the number 1024 and replace it with a constant which... (4 Replies)
Discussion started by: vino
4 Replies

Featured Tech Videos