The time now is Tue 24 Apr 2018, 04:49
All times are UTC - 4 |
Author |
Message |
Rattlehead

Joined: 11 Sep 2008 Posts: 367
|
Posted: Tue 10 Aug 2010, 00:31 Post subject:
Best way to format data in bash? Subject description: (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
|
|
 |
ken geometrics
Joined: 23 Jan 2009 Posts: 76 Location: California
|
Posted: Tue 10 Aug 2010, 11:03 Post subject:
Re: Best way to format data in bash? Subject description: (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
|
|
 |
Rattlehead

Joined: 11 Sep 2008 Posts: 367
|
Posted: 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
|
|
 |
technosaurus

Joined: 18 May 2008 Posts: 4787 Location: Kingwood, TX
|
Posted: 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
_________________ Check out my github repositories. I may eventually get around to updating my blogspot.
|
Back to top
|
|
 |
ken geometrics
Joined: 23 Jan 2009 Posts: 76 Location: California
|
Posted: 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
|
|
 |
Rattlehead

Joined: 11 Sep 2008 Posts: 367
|
Posted: 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 .
|
Back to top
|
|
 |
|
|
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 vote in polls in this forum You cannot attach files in this forum You can download files in this forum
|
Powered by phpBB © 2001, 2005 phpBB Group
|