Computing.Net > Forums > Unix > UNIX FIle Parsing

Computer Problems? Computing.Net has over 1,000,000 posts about all things technology related! Over 90% answered within 24 hours! Click here to start participating now! Also, be sure to check out the New User Guide.

UNIX FIle Parsing

Reply to Message Icon

Name: BrettAFD
Date: November 19, 2008 at 13:24:20 Pacific
OS: AIX
CPU/Ram: 8192
Product: IBM 570
Comment:

All:

I have a file, following this format:

[START FILE]
CN=user01
telephoneNumber=123-4567
whenChanged=20081118132545.0Z
employeeID=00001885
mail=user.01@home.com

CN=user02
telephoneNumber=234-5678
whenChanged=20081116194043.0Z
employeeID=00001984
mail=user.02@home.com

CN=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



Sponsored Link
Ads by Google

Response Number 1
Name: nails
Date: November 19, 2008 at 14:54:24 Pacific
Reply:

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/ksh

tnum=""
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
fi

if [[ "$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



0

Response Number 2
Name: BrettAFD
Date: November 20, 2008 at 08:48:07 Pacific
Reply:

Nails:

Well done, thank you very much for the example.

//Brett


0

Sponsored Link
Ads by Google
Reply to Message Icon

Related Posts

See More







Post Locked

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


Go to Unix Forum Home


Sponsored links

Ads by Google


Results for: UNIX FIle Parsing

to see unix file system in windows www.computing.net/answers/unix/to-see-unix-file-system-in-windows/3206.html

problems with unix file system www.computing.net/answers/unix/problems-with-unix-file-system/7541.html

UNIX file system www.computing.net/answers/unix/unix-file-system/3086.html