Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

expanding a set of data and setting lines to previous lines 1

Status
Not open for further replies.

moreton

Technical User
Jun 3, 2008
5
CA
Hello All,
I have had a thorough search around for a solution to problem but am yet to find one. I have found part solutions and getting close but can't seem to close the deal.

My raw data looks something like, where ValueX is a comma seperated values.

1,ValueX
6,ValueY
8,ValueZ

etc

I need to fill this data out to get something like

1,Value X
2,Value X
3,Value X
4,Value X
5,Value X
6,Value Y
7,Value Y
8,Value Z
9,Value Z

I have managed to get:
1,Value X
2,
3,
4,
5,
6,Value Y
7,
8,Value Z
9,

using
Code:
 awk ' BEGIN { FS = OFS = ","};{a[$1]=$0}; END{for(i=1; i <= 10; ++i) {print((i in a)?a[i]:i)}}'

At this stage I am completely lost and don't know how to refer back toa previous line (NR maybe?)'
Any assistance you could offer would be greatly appreciated
Thanking you in advance
Benjamin

 
You don't need to refer back to a previous line because you have alrady stored all of the input in an array.

Just save the last printed value in a variable, and if a subsequent line doesn't have a value, use the saved one.

Annihilannic.
 

Yes Annihilannic, but he also wants to generate missing lines between 1-6,7-8 and 9.

I'm too lazy today to think, you help him. [morning]


----------------------------------------------------------------------------
The person who says it can't be done should not interrupt the person doing it. -- Chinese proverb
 
Annihiannic,
what you suggest sort of makes sense but am unable to work out the code
Any suggestions would be greatly appreciated.
 
A starting point:
Code:
awk -F, '{i=$1;$1="";a[i]=$0}END{for(i=1;i<=10;++i){if(i in a)x=a[i];print i","x}}' /path/to/input

Hope This Helps, PH.
FAQ219-2884
FAQ181-2886
 
ZING - works a treat,
Thanks for that PHV and Annihiannic for some useful information


 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top