Tom's Guide | Tom's Hardware | Tom's Games
![]() |
![]() |
![]() |
All:
I have a file, following this format:
[START FILE]
CN=user01
telephoneNumber=123-4567
whenChanged=20081118132545.0Z
employeeID=00001885
mail=user.01@home.comCN=user02
telephoneNumber=234-5678
whenChanged=20081116194043.0Z
employeeID=00001984
mail=user.02@home.comCN=user03
telephoneNumber=345-6789
whenChanged=20081118124212.0Z
employeeID=00001988
[END FILE]I want to reformat like this (BUT ONLY FOR THOSE RECORDS WHOSE whenChanged VALUE IS 20081118*):
employeeID:telephoneNumber:mail
00001885:123-4567:user.01@home.com
00001984:234-5678:user.02@home.com
00001988:345-6789::Any ideas?
Thanks in advance.
Preferable shell is KSH.
./Brett

Parse by the = sign, then f1 is your main field name, employeeid, whenchanged,etc. and f2 is the value. Print it out when the new header flag CN appears. Need to skip the first header CN and print out the last record:
#!/bin/kshtnum=""
wchg=""
eid=""
mm=""
ff=0
while IFS="=" read f1 f2 f3
do
if [[ $ff -eq 0 && "$f1" = "CN" ]]
then # skip the first CN
ff=1
continue
fiif [[ "$f1" = "telephoneNumber" ]]
then
tnum="$f2"
fi
if [[ "$f1" = "whenChanged" ]]
then
wchg="$f2"
fi
if [[ "$f1" = "employeeID" ]]
then
eid="$f2"
fi
if [[ "$f1" = "mail" ]]
then
mm="$f2"
fi# print if first field is CN
if [[ "$f1" = "CN" ]]
then
case "$wchg" in
20081118*)
echo "$eid:$tnum:$mm"
tnum=""
wchg=""
eid=""
mm=""
;;
esac
fi
done < data4.txt
# print the last record
case "$wchg" in
20081118*)
echo "$eid:$tnum:$mm"
;;
esac

![]() |
![]() |
![]() |

This post is quite old and has been locked from receiving new replies. Please create a new posting instead.
| Ads by Google |