Best way to format data in bash?

For discussions about programming, programming questions/advice, and projects that don't really have anything to do with Puppy.
Post Reply
Message
Author
User avatar
Rattlehead
Posts: 368
Joined: Thu 11 Sep 2008, 11:40

Best way to format data in bash?

#1 Post by Rattlehead »

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-s ... g-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...

ken geometrics
Posts: 76
Joined: Fri 23 Jan 2009, 14:59
Location: California

Re: Best way to format data in bash?

#2 Post by ken geometrics »

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-s ... g-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.

User avatar
Rattlehead
Posts: 368
Joined: Thu 11 Sep 2008, 11:40

#3 Post by Rattlehead »

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

User avatar
technosaurus
Posts: 4853
Joined: Mon 19 May 2008, 01:24
Location: Blue Springs, MO
Contact:

#4 Post by technosaurus »

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 [url=https://github.com/technosaurus]github repositories[/url]. I may eventually get around to updating my [url=http://bashismal.blogspot.com]blogspot[/url].

ken geometrics
Posts: 76
Joined: Fri 23 Jan 2009, 14:59
Location: California

#5 Post by ken geometrics »

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

User avatar
Rattlehead
Posts: 368
Joined: Thu 11 Sep 2008, 11:40

#6 Post by Rattlehead »

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:
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:
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 :( :wink: .

Post Reply