0

I have file contains gigantic data about users. For example:

Username:Firstname:Lastname:Telephone number
Username:John:anderson:857-201-2213
Username:Sam:Asper:876-590-8223
Username:Adam:marlen:871-343-2233
Username:Suzi:sanders:991-531-9821
Username:paul:sepan:992-432-2267

I would like to create label for every user. How can I read the data from the input file above and generate label for every user like below:

dn: uid=John anderson, dc=example, dc=com
cn: John anderson
sn: anderson
telephoneNumber: 857-201-2213

===================
dn: uid=Adam marlen, dc=example, dc=com
cn: Adam marlen
sn: marlen
telephoneNumber: 871-343-2233

===================
1

With Awk alone:

awk -F: '
  NR > 1 {
    printf("dn: uid=%s, dc=example, dc=com\ncn: %s\nsn: %s\ntelephoneNumber: %s\n", 
              $2 " " $3, $2 " " $3, $3, $4); 
    print "\n===================";
  }
' file
0

You can do something like this:

#!/bin/bash
cat file | sed '1d;/^\s*$/d;s/:/\t/g' | while IFS='\t' read line; do

firstname=$(echo $line | awk '{print $2}')
secondname=$(echo $line | awk '{print $3}')
phone=$(echo $line | awk '{print $4}')

echo "
dn: uid=$firstname $secondname, dc=example, dc=com
cn: $firstname $secondname
sn: $secondname
telephoneNumber: $phone

==================="    
done

Output:

dn: uid=John anderson, dc=example, dc=com
cn: John anderson
sn: anderson
telephoneNumber: 857-201-2213

===================

dn: uid=Sam Asper, dc=example, dc=com
cn: Sam Asper
sn: Asper
telephoneNumber: 876-590-8223

===================

dn: uid=Adam marlen, dc=example, dc=com
cn: Adam marlen
sn: marlen
telephoneNumber: 871-343-2233

===================

dn: uid=Suzi sanders, dc=example, dc=com
cn: Suzi sanders
sn: sanders
telephoneNumber: 991-531-9821

===================

dn: uid=paul sepan, dc=example, dc=com
cn: paul sepan
sn: sepan
telephoneNumber: 992-432-2267

===================
  • thank you this is exactly what i want. I can apply the idea now on other parts of the label! – user308606 Oct 6 '18 at 17:40

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy