Puppy Linux Discussion Forum Forum Index Puppy Linux Discussion Forum
Puppy HOME page : puppylinux.com
"THE" alternative forum : puppylinux.info
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

The time now is Wed 26 Nov 2014, 12:23
All times are UTC - 4
 Forum index » Off-Topic Area » Programming
Best way to format data in bash?
Post_new_topic   Reply_to_topic View_previous_topic :: View_next_topic
Page 1 of 1 Posts_count  
Author Message
Rattlehead


Joined: 11 Sep 2008
Posts: 222
Location: Madrid, Spain

PostPosted: Tue 10 Aug 2010, 00:31    Post_subject:  Best way to format data in bash?
Sub_title: (Or maybe bash is not suitable for this task?)
 

I am trying to program a sort of "virtual corkboard" in console. It simply shows a grid with tags, whose colors vary according to their state, and each of them with an associated metadata file with notes, etc. The number of colums of the display can be modified on demand, and you can also insert blank slots on demand.

This is more or less where am I now. My main problem now is how to swap two tags. For example, tag 7 and tag 12 by typing 7x12, and the like.

I tried to store the information in the simplest way: a text file, and in each line either a tag, or only a newline to indicate a blank space in the corkboard.

I thought that swapping two lines inside a file would be a routine task, and that I was going to find in a second the one-liner to do it. To my surprise, there is very little information in Internet, only this linkhttp://www.unix.com/shell-programming-scripting/85230-swapping-switching-2-lines-using-sed.html, with a complicated awk script that I cannot get to work right. Seemingly, it is something nobody does.

Maybe my script has accumulated other errors along the way, but the absence of information on the issue makes me think that maybe:

a) Bash is not designed for the dynamic modification of files (I find it hard to believe, but the fact is I've googled for hours and found nothing)

b) Simple as it seems, the one-item-per-line format is counter-intuitive for bash, and I should have stored the info in a different way to access it (a huge single line with columns? Some kind of array? Using ampersands instead of newlines?)

Am I wasting my time? I did not think that it was going to be so difficult. Please give me some advice on what to do next...
Back to top
View user's profile Send_private_message 
ken geometrics

Joined: 23 Jan 2009
Posts: 76
Location: California

PostPosted: Tue 10 Aug 2010, 11:03    Post_subject: Re: Best way to format data in bash?
Sub_title: (Or maybe bash is not suitable for this task?)
 

Rattlehead wrote:
I am trying to program a sort of "virtual corkboard" in console. It simply shows a grid with tags, whose colors vary according to their state, and each of them with an associated metadata file with notes, etc. The number of colums of the display can be modified on demand, and you can also insert blank slots on demand.

This is more or less where am I now. My main problem now is how to swap two tags. For example, tag 7 and tag 12 by typing 7x12, and the like.

I tried to store the information in the simplest way: a text file, and in each line either a tag, or only a newline to indicate a blank space in the corkboard.

I thought that swapping two lines inside a file would be a routine task, and that I was going to find in a second the one-liner to do it. To my surprise, there is very little information in Internet, only this linkhttp://www.unix.com/shell-programming-scripting/85230-swapping-switching-2-lines-using-sed.html, with a complicated awk script that I cannot get to work right. Seemingly, it is something nobody does.

Maybe my script has accumulated other errors along the way, but the absence of information on the issue makes me think that maybe:

a) Bash is not designed for the dynamic modification of files (I find it hard to believe, but the fact is I've googled for hours and found nothing)

b) Simple as it seems, the one-item-per-line format is counter-intuitive for bash, and I should have stored the info in a different way to access it (a huge single line with columns? Some kind of array? Using ampersands instead of newlines?)

Am I wasting my time? I did not think that it was going to be so difficult. Please give me some advice on what to do next...


Bash can handle arrays.

You can remember the location of each line instead of the line for each location. The advantage is that it takes less work to a change because you only need to swap two numbers instead of two strings.

Bash can loop through a file of modest length reasonably fast. Looping through writing a new file each time is going to be slow but once to read in the information and then once to write the finished changes shouldn't bee too bad.
Back to top
View user's profile Send_private_message 
Rattlehead


Joined: 11 Sep 2008
Posts: 222
Location: Madrid, Spain

PostPosted: Tue 10 Aug 2010, 11:41    Post_subject:  

Great! Thank you very much Ken, your answer opens some light after many hours of of darkness.

So, correct me if I got you wrong, the best working scheme would be:

1) when opening the program, loop through the data file to transfer it to an array

2) make all the modifications in the array (I should modify the display part so it reads the data from it)

3) when it's closing time, dump the modified array into the file to store data


Thank you very much
Back to top
View user's profile Send_private_message 
technosaurus


Joined: 18 May 2008
Posts: 4379

PostPosted: Tue 10 Aug 2010, 12:53    Post_subject:  

I never really understood how bash arrays worked, so I followed Barry's example of using "|" (same as the pipe symbol) for the separator and then using cut -d "|" -f $COLUMNNUMBER
_________________
Web Programming - Pet Packaging 100 & 101
Back to top
View user's profile Send_private_message 
ken geometrics

Joined: 23 Jan 2009
Posts: 76
Location: California

PostPosted: Tue 10 Aug 2010, 22:04    Post_subject:  

Rattlehead wrote:
Great! Thank you very much Ken, your answer opens some light after many hours of of darkness.

So, correct me if I got you wrong, the best working scheme would be:

1) when opening the program, loop through the data file to transfer it to an array

2) make all the modifications in the array (I should modify the display part so it reads the data from it)

3) when it's closing time, dump the modified array into the file to store data


Thank you very much


Yes that is basically it.
If you want to make reading it in easier, you can write the file as:

#!/bin/bash
A[1]="This is line one"
A[2]="This is line two"

To load the array you can then just say:

. MyFile

Note the "." makes bash do it in the same shell

Then to display you just need to reference ${A[1]} and ${A[2]}

You can do
I=2
echo ${A[$I]}

This lets you loop through the array with a for loop
Back to top
View user's profile Send_private_message 
Rattlehead


Joined: 11 Sep 2008
Posts: 222
Location: Madrid, Spain

PostPosted: Wed 11 Aug 2010, 11:31    Post_subject:  

Thank you guys both for the great resources. I am not a system administrator, just a guy who has rediscovered computing thanks to Linux and wants to get some simple bleep blops from his computer as a hobby, and this stuff really helps me.

Technosaurus, thank you for the cut oneliner too, it looks like a very "tight" way to define configurations.

Ken, I have some doubts about the solution you propose:

Quote:
If you want to make reading it in easier, you can write the file as:

#!/bin/bash
A[1]="This is line one"
A[2]="This is line two"


You talk about writing a file, but in the next lines you define an array, not a file, and later:

Quote:
To load the array you can then just say:

. MyFile


AFAIK, this command would source MyFile, i.e., would sort of "paste" the file inside my script and proceed to execute it. But being a data file, it cannot be executed. How does MyFile relates to the array you've called A? Maybe you've skipped some steps for considering them obvious, please remember that I am a poor, helpless newbie Sad Wink .
Back to top
View user's profile Send_private_message 
Display_posts:   Sort by:   
Page 1 of 1 Posts_count  
Post_new_topic   Reply_to_topic View_previous_topic :: View_next_topic
 Forum index » Off-Topic Area » Programming
Jump to:  

Rules_post_cannot
Rules_reply_cannot
Rules_edit_cannot
Rules_delete_cannot
Rules_vote_cannot
You cannot attach files in this forum
You can download files in this forum


Powered by phpBB © 2001, 2005 phpBB Group
[ Time: 0.0650s ][ Queries: 12 (0.0048s) ][ GZIP on ]