Register
It is currently Thu Oct 23, 2014 4:08 am

Create a list of values from array of values with sed


All times are UTC - 6 hours


Post new topic Reply to topic  [ 6 posts ] 
Author Message
 PostPosted: Wed Aug 18, 2010 2:50 pm   

Joined: Wed Aug 18, 2010 2:40 pm
Posts: 1
Hi all

I'm building a script that will take an array of values in a file (broken up by commas) and covert the array into a list of values, each preceded by variable identifier.

Example:

Quote:
,10,20,30
,40,50,60
,70,80,90

to

value_1=10
value_2=20
value_3=30

value_4=40
value_5=50
value_6=60

value_7=70
value_8=80
value_9=90


Here is the script logic as I see it:

Quote:
Load file of arrayed values [FILENAME].csv
initiate script variables # don't need help with this area
update the value identifier text that's in the form of a string # don't need help with this area

Use Sed to
find the first comma in the loaded file
replace the comma with a "new line character" then followed by the value identifier string
end Sed command

Save the Sed command exiting status to a variable (was the Sed command successful or did it fail)

Encompass the operation in a while statement that says continue searching for and replacing commas while the Sed command is ending successfully. I.E. repeat the process there are still commas that need to be found and replaced. Otherwise end script


At this point I'm a total noob when it comes to Sed.
Could anyone help me develop a Sed command and a logic structure (the while statement) that will do what I've described above?

Thanks,
Reactor89


Top
 Profile  
 PostPosted: Wed Aug 18, 2010 3:29 pm   
User avatar

Joined: Sun Jun 27, 2010 12:57 am
Posts: 192
Even though you're specifically asking for a 'sed' script, the forum is called sed and awk, so I'm going to provide an awk version.
Code:
awk '{ n=split($0, arr, ","); for (i=1; i<=n; i++) if (arr[i]) printf("value_%d=%s\n", ++cnt, arr[i]); else printf("\n"); }'

I hope it still helps you in some way, if not, you may throw it in your garbage bin :)


Top
 Profile  
 PostPosted: Wed Aug 18, 2010 4:43 pm   
User avatar

Joined: Tue Apr 27, 2010 2:28 pm
Posts: 172
Location: Czech Republic
Code:
sed 's/\(.\),/\1\n/g;s/,//' | nl | sed 's/^ \+/value_/;s/\t/=/'

Explanation: replace commas by newlines, remove the leading ones. Number non-empty lines. Then just add value_ before the line number and = after it. :-B


Top
 Profile  
 PostPosted: Wed Aug 18, 2010 11:03 pm   
User avatar

Joined: Sun Jun 27, 2010 12:57 am
Posts: 192
Then to be a total nitpick here; that's not the precise output that you requested in your OP ;)


Top
 Profile  
 PostPosted: Thu Aug 19, 2010 12:05 am   
User avatar

Joined: Tue Apr 27, 2010 2:28 pm
Posts: 172
Location: Czech Republic
Patsie wrote:
Then to be a total nitpick here; that's not the precise output that you requested in your OP ;)

OK, then (even shorter):
Code:
sed 's/,/\n/g' | nl | sed 's/\t/=/;/=/s/^ \+/value_/'

But if you are going to use the output as a script, I fear using such variable names is not the right way to go. You can profit from bash's arrays.
Code:
sed '1s/^,/value=([1]=/;s/,/ /g;$a)'

You can then access your values by
Code:
echo ${value[1]}
echo "${value[@]}"
i=9
echo ${value[i]}


Top
 Profile  
 PostPosted: Wed Jun 08, 2011 10:50 am   
User avatar

Joined: Wed Jun 08, 2011 8:27 am
Posts: 189
Location: outer Shpongolia
Patsie wrote:
Even though you're specifically asking for a 'sed' script, the forum is called sed and awk, so I'm going to provide an awk version.
Code:
awk '{ n=split($0, arr, ","); for (i=1; i<=n; i++) if (arr[i]) printf("value_%d=%s\n", ++cnt, arr[i]); else printf("\n"); }'

I hope it still helps you in some way, if not, you may throw it in your garbage bin :)


I had another approach.

Code:
awk -F, 'NR != 1 && /^,/ {printf("\n")} {for (i=2; i<=NF; i++) printf("value_%d=%d\n", ++j, $i)}'


Top
 Profile  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 6 posts ] 

All times are UTC - 6 hours


Who is online

Users browsing this forum: No registered users and 3 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Jump to:  


BashScripts | Promote Your Page Too
Powered by phpBB © 2011 phpBB Group
© 2003 - 2011 USA LINUX USERS GROUP