awk or sed to split dn


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting awk or sed to split dn
# 1  
Old 09-01-2009
awk or sed to split dn

Hello -

I have an input file with user dn's. e.g.
Code:
cn=peter,cn=users,dc=com,dc=uk
cn=simon,cn=users,dc=com,dc=uk
cn=john,cn=users,dc=com,dc=uk
cn=fred,cn=users,dc=com,dc=uk

My script needs to generate an LDIF file based on this input file as below:-

Code:
awk '{ FS="[=|,]"}
   {
      print "dn: " $1
      print "changetype: add"
      print "objectclass: top"
      print "changetype: person"
      print "uid: " $1
      print "mail: " $1"@com.uk"
      print "cn: " $1
      print "userpassword: " $password
}' $INFILE >> $OUTFILE

Now I can't work out what the awk statement should look like so that "dn" is assigned the full dn from the file and uid and cn are assigned just a segment. For example, the output for one line should look like:

Code:
dn: cn=peter,cn=users,dc=com,dc=uk
 changetype: add
objectclass: top
changetype: person
uid: peter
mail: peter@com.uk
cn: peter
userpassword: somepass

Clearly the above will not work. Any help much appreciated.
Many thanks

Last edited by Franklin52; 09-01-2009 at 08:00 AM.. Reason: Please use code tags!
# 2  
Old 09-01-2009
To keep the forums high quality for all users, please take the time to format your posts correctly.

First of all, use Code Tags when you post any code or data samples so others can easily read your code. You can easily do this by highlighting your code and then clicking on the # in the editing menu. (You can also type code tags [code] and [/code] by hand.)

Second, avoid adding color or different fonts and font size to your posts. Selective use of color to highlight a single word or phrase can be useful at times, but using color, in general, makes the forums harder to read, especially bright colors like red.

Third, be careful when you cut-and-paste, edit any odd characters and make sure all links are working property.

Thank You.

The UNIX and Linux Forums

++++++++++++++++++++++++++++++++++++++++++++++++++

Try this:

Code:
awk -F, '
{
split($1,a,"=")
print "dn: " $0
print "changetype: add"
print "objectclass: top"
print "changetype: person"
print "uid: " a[2]
print "mail: " a[2]"@com.uk"
print "cn: " a[2]
print "userpassword: " "somepass"
}' $INFILE >> $OUTFILE

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

sed split string

Greetings, i have a string that looks like Network "123" "ABC" i need to make it look like: Network "123" Network "ABC" Help please? Thanks again Please use CODE tags when displaying sample input, sample output, and code segments (as required by forum rules). (2 Replies)
Discussion started by: hoyanet
2 Replies

2. UNIX for Beginners Questions & Answers

sed awk: split a large file to unique file names

Dear Users, Appreciate your help if you could help me with splitting a large file > 1 million lines with sed or awk. below is the text in the file input file.txt scaffold1 928 929 C/T + scaffold1 942 943 G/C + scaffold1 959 960 C/T +... (6 Replies)
Discussion started by: kapr0001
6 Replies

3. Shell Programming and Scripting

awk split and awk calculation in the same command

I am trying to run the awk below. My question is when I split the input, then run anotherawk to perform a calculation using that splitas the input there are no issues. When I try to combine them the output is not correct, is the split not working or did I do it wrong? Thank you :). input ... (8 Replies)
Discussion started by: cmccabe
8 Replies

4. Shell Programming and Scripting

split string using sed, perl

How can I split following input into stwo strings: Input: 1^~^2^~^3^~^4^~^5^~^6^~^7^~^8^~^9 Output: $string1 = 1^~^2^~^ $string2 = 3^~^4^~^5^~^6^~^7^~^8^~^9 Note: the length of string may vary, say upto 15. String 1 will contain only first two. string2 will contain... (10 Replies)
Discussion started by: som.nitk
10 Replies

5. Shell Programming and Scripting

awk to split one field and print the last two fields within the split part.

Hello; I have a file consists of 4 columns separated by tab. The problem is the third fields. Some of the them are very long but can be split by the vertical bar "|". Also some of them do not contain the string "UniProt", but I could ignore it at this moment, and sort the file afterwards. Here is... (5 Replies)
Discussion started by: yifangt
5 Replies

6. Shell Programming and Scripting

Split line to multiple files Awk/Sed/Shell Script help

Hi, I need help to split lines from a file into multiple files. my input look like this: 13 23 45 45 6 7 33 44 55 66 7 13 34 5 6 7 87 45 7 8 8 9 13 44 55 66 77 8 44 66 88 99 6 I want to split every 3 lines from this file to be written to individual files. (3 Replies)
Discussion started by: saint2006
3 Replies

7. Shell Programming and Scripting

Split a line based on : using sed

Hi, i have a file say file1 having following data /abc/def:ghi/jkl/ some other text Now i want to extract only ghi/jkl/using sed, can some one please help me. Thanks Sarbjit (2 Replies)
Discussion started by: sarbjit
2 Replies

8. Shell Programming and Scripting

Split a file based on pattern in awk, grep, sed or perl

Hi All, Can someone please help me write a script for the following requirement in awk, grep, sed or perl. Buuuu xxx bbb Kmmmm rrr ssss uuuu Kwwww zzzz ccc Roooowwww eeee Bxxxx jjjj dddd Kuuuu eeeee nnnn Rpppp cccc vvvv cccc Rhhhhhhyyyy tttt Lhhhh rrrrrssssss Bffff mmmm iiiii Ktttt... (5 Replies)
Discussion started by: kumarn
5 Replies

9. UNIX for Dummies Questions & Answers

Split a file with no pattern -- Split, Csplit, Awk

I have gone through all the threads in the forum and tested out different things. I am trying to split a 3GB file into multiple files. Some files are even larger than this. For example: split -l 3000000 filename.txt This is very slow and it splits the file with 3 million records in each... (10 Replies)
Discussion started by: madhunk
10 Replies

10. UNIX for Advanced & Expert Users

sed help split line

hi i have a file containing lines like word1,word2,word3,word4,.. word4,word5,word3,word6,... now i need to make it to look like word1 word2 word3 word4 . . . in other words ','(comma) is replaced with new line. (5 Replies)
Discussion started by: Raom
5 Replies
Login or Register to Ask a Question