How can I read from file? (Solved)

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
oldyeller
Posts: 889
Joined: Tue 15 Nov 2011, 14:26
Location: Alaska

How can I read from file? (Solved)

#1 Post by oldyeller »

Hello Everyone,

How can I have this read from a file and not have to do a string? I have four margins to do. It would be a lot easier just to do a file instead of doing this for all four.

Code: Select all

TOPMARGIN=" "1" " 2" "3" "36" "
COMBOLIST2=""
for M in $TOPMARGIN
do
 COMBOLIST2="$COMBOLIST2<item>$M</item>"
done
Last edited by oldyeller on Fri 23 May 2014, 08:16, edited 1 time in total.

User avatar
dejan555
Posts: 2798
Joined: Sun 30 Nov 2008, 11:57
Location: Montenegro
Contact:

#2 Post by dejan555 »

Code: Select all

for M in $(cat /path/to/file);do ...
puppy.b0x.me stuff mirrored [url=https://drive.google.com/open?id=0B_Mb589v0iCXNnhSZWRwd3R2UWs]HERE[/url] or [url=http://archive.org/details/Puppy_Linux_puppy.b0x.me_mirror]HERE[/url]

User avatar
oldyeller
Posts: 889
Joined: Tue 15 Nov 2011, 14:26
Location: Alaska

#3 Post by oldyeller »

Hi dejan555,

Thanks that did work. I have been doing a lot of studying on bash just didn't think about that one. I guess the more I do code the more I will learn.

I do have another issue How can I keep the settings once they have been set. Would I need a config file for this and how would I do it?

Cheers

User avatar
dejan555
Posts: 2798
Joined: Sun 30 Nov 2008, 11:57
Location: Montenegro
Contact:

#4 Post by dejan555 »

Not sure what exactly do you need to do.
Settings for some variables, paths? That can be changed?
You can define defaults at the begining of your script you can also include config file like this:

Code: Select all

 . $HOME/.someconfigfile
You can export settings something like

Code: Select all

echo "VAR1=\"$VAR1\" " > $HOME/.someconfigfile
echo "VAR2=\"$VAR2\" " >> $HOME/.someconfigfile
And yep, I learn by doing too, never actually studied commands when I didn't need them.
puppy.b0x.me stuff mirrored [url=https://drive.google.com/open?id=0B_Mb589v0iCXNnhSZWRwd3R2UWs]HERE[/url] or [url=http://archive.org/details/Puppy_Linux_puppy.b0x.me_mirror]HERE[/url]

amigo
Posts: 2629
Joined: Mon 02 Apr 2007, 06:52

#5 Post by amigo »

Actually, the best way to do that is by redirection:

Code: Select all

while read LINE ; do
 # your actions here
done < /path/to/file
Using cat inside for is a waste of using cat and if there are lines in the file which have entries with spaces in them, the for loop will break along the spaces and not the lines.

User avatar
oldyeller
Posts: 889
Joined: Tue 15 Nov 2011, 14:26
Location: Alaska

#6 Post by oldyeller »

amigo wrote:Actually, the best way to do that is by redirection:

Code: Select all

while read LINE ; do
 # your actions here
done < /path/to/file
Using cat inside for is a waste of using cat and if there are lines in the file which have entries with spaces in them, the for loop will break along the spaces and not the lines.
for this code how would I apply it?

Code: Select all

COMBOLIST2=""
for M in $(cat /$WORKDIR/margins);
do
 COMBOLIST2="$COMBOLIST2<item>$M</item>"
done
Cheers

User avatar
dejan555
Posts: 2798
Joined: Sun 30 Nov 2008, 11:57
Location: Montenegro
Contact:

#7 Post by dejan555 »

Instead a for loop it would be a while loop

Code: Select all

while read LINE; do
COMBOLIST2="$COMBOLIST2<item>$LINE</item>" 
done < /$WORKDIR/margins
but instead of 1 2 3 as individual items it would take a whole line as variable using like this
so if you need individual you would need to break $LINE in another loop I guess

Code: Select all

while read LINE; do
for M in $LINE;do
COMBOLIST2="$COMBOLIST2<item>$M</item>" 
done
done < /$WORKDIR/margins
amigo will correct me if wrong
puppy.b0x.me stuff mirrored [url=https://drive.google.com/open?id=0B_Mb589v0iCXNnhSZWRwd3R2UWs]HERE[/url] or [url=http://archive.org/details/Puppy_Linux_puppy.b0x.me_mirror]HERE[/url]

User avatar
oldyeller
Posts: 889
Joined: Tue 15 Nov 2011, 14:26
Location: Alaska

#8 Post by oldyeller »

I have used the less command in place of cat, I suppose I could also use cut as well. Just thinking of different ways on how to do this. any other Ideas?


Cheers

slavvo67
Posts: 1610
Joined: Sat 13 Oct 2012, 02:07
Location: The other Mr. 305

#9 Post by slavvo67 »

All:

This script has me curious. Can someone explain maybe with a real example as to what it does?

Thanks,


Slavvo67

User avatar
dejan555
Posts: 2798
Joined: Sun 30 Nov 2008, 11:57
Location: Montenegro
Contact:

#10 Post by dejan555 »

This is just a part of his code, seems like he needs to build list of items to choose from in gtk combobox.

oldyeller, do you need separate words/numbers from file as items or whole lines?
Not sure how exactly your file looks or what's the full combobox that you need to get but cat or while loop should do the work depending on what you need.
puppy.b0x.me stuff mirrored [url=https://drive.google.com/open?id=0B_Mb589v0iCXNnhSZWRwd3R2UWs]HERE[/url] or [url=http://archive.org/details/Puppy_Linux_puppy.b0x.me_mirror]HERE[/url]

musher0
Posts: 14629
Joined: Mon 05 Jan 2009, 00:54
Location: Gatineau (Qc), Canada

#11 Post by musher0 »

Hi, guys.

Here's my take on oldyeller's problem:

Code: Select all

#!/bin/sh
# essai-oldyeller.sh # Using array instead of string.
####
declare -a TOPMARGIN=(1 2 3 36)
COMBOLIST2=""
M="0"
for i in `seq ${#TOPMARGIN[@]}`;do
	COMBOLIST2="<item>\"${TOPMARGIN[$M]}\"</item>"
	echo $COMBOLIST2
	M="`expr $M + 1`"
done
Result is shown in attached pic.

Judging from the name of the variable, I'd say oldyeller is trying to place the
"top margin" :) of some window on the screen! :)

I hope this helps. BFN.

musher0
~~~~~~~~~~
Edit - A couple of remarks.
1) The line < echo $COMBOLIST2 > is for checking during test. Remove when script
checks out ok or when not needed anymore.

2) The seq command is a spoiled little brat: it hates the zero (0). I had to use a
workaround, since arrays are counted starting from zero.
Attachments
essai-oldyeller.jpg
(31.07 KiB) Downloaded 145 times
musher0
~~~~~~~~~~
"You want it darker? We kill the flame." (L. Cohen)

User avatar
oldyeller
Posts: 889
Joined: Tue 15 Nov 2011, 14:26
Location: Alaska

#12 Post by oldyeller »

Hello Everyone,

What this code is going to help me with is printing. I have already got it done, just wanted to see a better way to code the variable. I will try this one out as well musher0.

I have gotten printing setup for the editor that I made a few years ago for Manna Bible Software. When all is done and looking how I see it; it will be available for anyone to use.

Cheers

musher0
Posts: 14629
Joined: Mon 05 Jan 2009, 00:54
Location: Gatineau (Qc), Canada

#13 Post by musher0 »

oldyeller wrote:Hello Everyone,

What this code is going to help me with is printing. I have already got it done, just wanted to see a better way to code the variable. I will try this one out as well musher0.

I have gotten printing setup for the editor that I made a few years ago for Manna Bible Software. When all is done and looking how I see it; it will be available for anyone to use.

Cheers
No worries, oldyeller. Best of luck! :)

musher0
musher0
~~~~~~~~~~
"You want it darker? We kill the flame." (L. Cohen)

User avatar
oldyeller
Posts: 889
Joined: Tue 15 Nov 2011, 14:26
Location: Alaska

#14 Post by oldyeller »

I did some more studying on the for command and this is what I did.

Code: Select all

COMBOLIST2=""
for M in {1..72}
do
 COMBOLIST2="$COMBOLIST2<item>$M</item>"
done
Thanks to everyone who helped.

Cheers

Post Reply