Visit Our UNIX and Linux User Community


Write a new file from 2 files as input to the script


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Write a new file from 2 files as input to the script
# 1  
Old 06-10-2009
Write a new file from 2 files as input to the script

Hi-
I am hoping someone can give me some pointers to get me started. I have a file which contains some dn's .e.g file 1
cn=bob,cn=user,dc=com
cn=kev,cn=user,dc=com
cn=john,cn=user,dc=com

I have a second file e.g. file.template which looks something like :-

dn: <dn>
objectclass: inetOrgPerson
objectclass: ePerson
objectclass: organizationalPerson
objectclass: person
objectclass: top
cn: <name>
sn: <name>
userpassword: password
uid: <name>

I want to read each line from file 1 and then by using file.template, generate a third file. I don't have to do it this way though - I basically have a file containing a list of dn's and I need to generate an ldif file. Anyone got any ideas please?
Thanks
# 2  
Old 06-10-2009
can you post how the resulting file should look like??
# 3  
Old 06-11-2009
It should look like :-

dn: cn=bob,cn=user,dc=com
objectclass: inetOrgPerson
objectclass: ePerson
objectclass: organizationalPerson
objectclass: person
objectclass: top
cn: bob
sn: bob
userpassword: password
uid: bob

dn: cn=john,cn=user,dc=com
objectclass: inetOrgPerson
objectclass: ePerson
objectclass: organizationalPerson
objectclass: person
objectclass: top
cn: john
sn: john
userpassword: password
uid: john

etc

I have been thinking that this may not be such a good way of doing this. I think maybe it would be better to have one input file containing the dn's and then just write a new file from the script.

What is the best way to read in a file, process it line by line and write out a new file please (bear in my mind I don't know shell scripting very well) ?

e.g.
while read line
do

print "$line" >> $outfile

print "objectclass: inetOrgPerson" >> $outfile

print " objectclass: ePerson" >> $outfile
print "objectclass: organizationalPerson" >> $outfile
print "objectclass: person" >> $outfile
print "objectclass: top" >> $outfile
print "cn: john" >> $outfile
print "sn: john" >> $outfile
print "userpassword: password" >> $outfile
print "uid: john" >> $outfile
done < $filename

I am sure the above syntax is wrong .. can someone tell me the best way to do something like that ?

Going one step further, if my input file contained the dn and additional info, e.g. :-

# dn, firstname, surname, password
cn=bob,cn=user,dc=com, bob, jones, Password123
cn=john,cn=user,dc=com, john, smith, Password456

how could I read each line into the script but put dn, firstname, surname and password into separate variables and ignore comments ??

Many thanks
# 4  
Old 06-11-2009
how about doing something like this...
Code:
awk '{FS="[,=]"}
{print "dn: " $0
print "objectclass: inetOrgPerson"
print "objectclass: ePerson"
print "objectclass: organizationalPerson"
print "objectclass: person"
print "objectclass: top"
print "cn: "$2
print "sn: "$2
print "userpassword: password"
print "uid: "$2
}' infile

infile should contain

cn=bob,cn=user,dc=com
cn=kev,cn=user,dc=com
cn=john,cn=user,dc=com

from here you can do the addition stuff too.
# 5  
Old 06-11-2009
snipper ,

It would be highly appreciative if u post what is exact input your giving and the output your expecting instead of the explaining the same.

Thanks ,

panyam
# 6  
Old 06-11-2009
Hi -
This is the input file :-
# dn, firstname, surname, password
cn=bob,cn=user,dc=com, bob, jones, Password123
cn=john,cn=user,dc=com, john, smith, Password456

and the output file should look like:-

dn: cn=bob,cn=user,dc=com
objectclass: inetOrgPerson
objectclass: ePerson
objectclass: organizationalPerson
objectclass: person
objectclass: top
cn: bob
sn: bob
userpassword: password
uid: bob

dn: cn=john,cn=user,dc=com
objectclass: inetOrgPerson
objectclass: ePerson
objectclass: organizationalPerson
objectclass: person
objectclass: top
cn: john
sn: john
userpassword: password
uid: john

Thanks for the suggestions.

Previous Thread | Next Thread
Test Your Knowledge in Computers #380
Difficulty: Medium
Apple's macOS (previously known as Mac OS X or OS X) is a UNIX 03 registered product.
True or False?

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

Script to break up file (write new files) in bash

Hello experts, I need help writing individual files from a data matrix, with each new file being written every time there is a blank line: From this cat file.txt col1 col2 col3 6661 7771 8881 6661 7771 8881 6661 7771 8881 col1 col2 col3 3451 2221 1221... (6 Replies)
Discussion started by: torchij
6 Replies

2. Shell Programming and Scripting

Read input files and merge them in given order and write them to input one param or one file

Dear Friends, I am looking for a shell script to merge input files into one file .. here is my idea: 1st paramter would be outfile file (all input files content) read all input files and merge them to input param 1 ex: if I pass 6 file names to the script then 1st file name as output file... (4 Replies)
Discussion started by: hyd1234
4 Replies

3. Shell Programming and Scripting

Read user input, Encrypt the data and write to file

Hi, can some one help me how to encrypt and decrypt a file. AIM: reade user input, encrypt it and save it to file. while decryption read the encrypted file decrypt it and save the output in some variable. Example: consider we have Credentials.txt file with content username: password... (5 Replies)
Discussion started by: saichand1985
5 Replies

4. Shell Programming and Scripting

Script to delete files with an input for directories and an input for path/file

Hello, I'm trying to figure out how best to approach this script, and I have very little experience, so I could use all the help I can get. :wall: I regularly need to delete files from many directories. A file with the same name may exist any number of times in different subdirectories.... (3 Replies)
Discussion started by: *ShadowCat*
3 Replies

5. Shell Programming and Scripting

Unfold the data from a input file and write to a file

I am working on a script to unfold data for each column from a specific line of data and write output in a single line. Input data looks like this. 2011-09-26 INF UM_10 UserMana Starting synchronization for security domain 14:37:31 080 gementSe . rvice I... (2 Replies)
Discussion started by: svajhala
2 Replies

6. Shell Programming and Scripting

Separating list of input files (*.file) with a comma in bash script

Hi all, I'm trying to get a bash script working for a program (bowtie) which takes a list of input files (*.fastq) and assembles them to an output file (outfile.sam). All the .fastq files are in one folder in my home directory (~/infiles). The problem is that the 'bowtie' requires that... (7 Replies)
Discussion started by: TuAd
7 Replies

7. Shell Programming and Scripting

Help to write a script or program to automatic execute and link input file data

Output file template format <input_file_name>a</input_file_name> <total_length_size>b</total_length_size> <log_10_length_size>c</log_10_length_size> Input_file_1 (eg. sample.txt) SDFSDGDGSFGRTREREYWW Parameter: a is equal to the input file name b is equal to the total length of... (2 Replies)
Discussion started by: perl_beginner
2 Replies

8. Shell Programming and Scripting

How to write shell script for input file name format checking?

Hello, I had written a shell script that accepts input file as cmd line argument and process this file. if ; then if ; then . $1 LOGFILE="$LOG_FILE/MIG_BIOS.log"; get_input_file else ERROR_CODE=MSCRM0005_003 error "$ERROR_CODE : Input file $1 is not available"; exit... (3 Replies)
Discussion started by: Poonamol
3 Replies

9. UNIX and Linux Applications

Input a variable and write to a file using awk

Hi I am trying to edit a csv file. Bacically I need to input a search variable and the value that must be changed in one of the fields corresponding to that searched variable. My csv file looks like so: 1,1A,5 1,1B,2 1,1C,3 2,2A,7 2,2B,4 2,2C,0 3,3A,1 3,3B,6 3,3C,4 I want to... (4 Replies)
Discussion started by: ladyAnne
4 Replies

10. Shell Programming and Scripting

shell script to remove old files and write to a log file

Hi, I have a script that works on a unix box but am trying to get it working on a linux box that uses shell. I am not a programmer so this is proving harder than I imagined. I made some changes and ended up with the script below but when I run it I get the following messages. Any help would be... (4 Replies)
Discussion started by: yabai
4 Replies

Featured Tech Videos