Computing.Net > Forums > Unix > ksh data file manipulation

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.

ksh data file manipulation

Reply to Message Icon

Name: STN
Date: March 1, 2007 at 21:41:19 Pacific
OS: SunOS 5.8
CPU/Ram: E250
Product: Sun
Comment:

I have a file which has patterns of repeated lines. The pattern should be:

a = name
b = var1
c = var2
d = var3

Sometimes though one of these lines (or more) is missing (2nd c line below):

a = name1
b = 100
c = 33
d = 800
a = name2
b = 100
d = 800
a = name3
b = 100
c = 33
d = 800

When this happens I want to inject a default value (say 35) for that missing variable.
I dont necessarily need it to be inserted into the file as it is displayed above because the next step is to produce a file that looks like:

name1 800 100 33
name2 800 100 35
name3 800 100 33

Note above that 35 was inserted to maintain consistancy (and has meaning). So this value could be inserted as the values are put into the column format. This would then allow me to sort the file based on column 2 first, then 3, then 4:

name1 800 100 33
name3 800 100 33
name2 800 100 35

Then using uniq -d I could display any duplicates so the end result would be the script alerting me to duplicate lines (not including checking the name for dups as these will be always be unique):

#script output:
namex 100 33 800

Not sure which dup line would be displayed, but the important thing would be that one of the two dup lines was displayed.

I have some commands to do the last couple parts

# sort based on column2, then column3, then column4
sort +1 -2 +2 -3 +3 -4
# display any duplicates lines not including column 1
uniq -d -1


What I'm struggling with is setting an expected pattern of lines, and then inserting a value when part of the pattern is missing. Any help that could be provided is appreciated.



Sponsored Link
Ads by Google

Response Number 1
Name: nails
Date: March 2, 2007 at 14:31:58 Pacific
Reply:

The following script creates 3 output lines like so:

name1 100 33 800
name2 100 35 800
name3 100 33 800

This awk script is tailored to fit you data structure and is easily broken if you change the data requirements:

awk ' BEGIN { nb=nc=nd=0; defalt="35" }
{
if($1 == "a" && $3 ~ /^name/)
{
if(NR == 1)
{
print $3
continue
}

if(nb == 0)
print defalt
else
print nb

if(nc == 0)
print defalt
else
print nc

if(nd == 0)
print defalt
else
print nd

na=nb=nc=nd=0
print $3
continue
}

if($1 == "b" && $2 == "=")
nb=$3

if($1 == "c" && $2 == "=")
nc=$3

if($1 == "d" && $2 == "=")
nd=$3

}
END {
if(nb == 0)
print defalt
else
print nb

if(nc == 0)
print defalt
else
print nc

if(nd == 0)
print defalt
else
print nd

}
' data.file|xargs -n 4



0

Response Number 2
Name: STN
Date: March 2, 2007 at 16:47:55 Pacific
Reply:

That is fantastic. Thanks nails, it works like a charm.

One follow up on the default, how would I set unique defaults for b,c and d.

Example defaults:
b = 1
c = 35
d = 1

I've tried a couple things but couldnt quiet get it to work.

Thanks again.


0

Response Number 3
Name: STN
Date: March 2, 2007 at 16:53:18 Pacific
Reply:

Actually I may have just stumbled on it. Perhaps you could verify it looks like how you would do it:

Assuming b,d default is 1 and c default is 35:

awk ' BEGIN { nb=nc=nd=0; defalt="35"; defalt2="1" }
{
if($1 == "a" && $3 ~ /^name/)
{
if(NR == 1)
{
print $3
continue
}

if(nb == 0)
print defalt2
else
print nb

if(nc == 0)
print defalt
else
print nc

if(nd == 0)
print defalt2
else
print nd

na=nb=nc=nd=0
print $3
continue
}

if($1 == "b" && $2 == "=")
nb=$3

if($1 == "c" && $2 == "=")
nc=$3

if($1 == "d" && $2 == "=")
nd=$3

}
END {
if(nb == 0)
print defalt2
else
print nb

if(nc == 0)
print defalt
else
print nc

if(nd == 0)
print defalt2
else
print nd

}
' data.file|xargs -n 4


0

Response Number 4
Name: nails
Date: March 2, 2007 at 21:12:54 Pacific
Reply:

Glad you like it. Yes, I'd say your change is perfectly reasonable.


0

Response Number 5
Name: STN
Date: March 3, 2007 at 14:10:37 Pacific
Reply:

Thanks alot. I appreciate your time and expertise.


0

Related Posts

See More



Sponsored Link
Ads by Google
Reply to Message Icon






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: ksh data file manipulation

creating data file with new line www.computing.net/answers/unix/creating-data-file-with-new-line/7803.html

Merging two data files www.computing.net/answers/unix/merging-two-data-files/7293.html

Spinning a data file 90 degrees www.computing.net/answers/unix/spinning-a-data-file-90-degrees/5803.html