You are not logged in.

#1 2008-07-07 20:38:50

jnwebb
Member
Registered: 2008-07-07
Posts: 52

newbie bash scripting help!

Hello,

I would like some help with a simple script.

I have say 10 text files (***.txt) in a directory.  I want to make each *** into a subdirectory, insert the respective ***.txt file into that subdirectory as well as insert three other files into each subdirectory.

I then want to create a loop that will run a seperate script, which I already have, on each folder.

Thanks for any help

Offline

#2 2008-07-07 21:00:17

Garns
Member
Registered: 2008-05-28
Posts: 239

Re: newbie bash scripting help!

Welcome,

O.K. I'll just assume the ***.txt files are the only .txt files in the folder.

I won't write a full script but this snippet should give you an idea:

for f in *.txt; do
d=`basename $f .txt`
mkdir $d
mv $f [and other stuff] $d
otherscript $d; #you can do a second loop if you want to
done

Something like this should do what you want, depending on the other script you might have to cd to $d and then back.

Disclaimer: This is right out of my head, it is totally untested and might be completely wrong. If while experimenting with this something eats your box, it is not my fault!

Offline

#3 2008-07-08 20:20:26

jnwebb
Member
Registered: 2008-07-07
Posts: 52

Re: newbie bash scripting help!

Thank you very much...another question

I have a several folders with several files (all have ****-123.ars) in each folder that I want to run through gnuplot and plot.  All files in each folder are to be plotted together.

some sort of loop that will go into each folder
some sort of loop that will go through each file and call this set of commands
gnuplot
set terminal postscript
set output file.ps
set logscale x
plot "****-123.ars" using 8:6 with line


or somehow use multiplot

I am new to both.

Thanks

Offline

#4 2008-07-08 21:34:37

dyscoria
Member
Registered: 2008-01-10
Posts: 1,007

Re: newbie bash scripting help!

something like this would list all files in each subdirectory together and allow you to execute a command on them (see man page for find):

find -iname "*-123.ars" -type f -exec blah blah

flack 2.0.6: menu-driven BASH script to easily tag FLAC files (AUR)
knock-once 1.2: BASH script to easily create/send one-time sequences for knockd (forum/AUR)

Offline

#5 2008-07-09 03:47:26

peets
Member
From: Montreal
Registered: 2007-01-11
Posts: 936
Website

Re: newbie bash scripting help!

Furethermore, if you want to run "blah blah" only once and give it the found files as a list of arguments, the xargs(1) program will help:

find -iname "*-123.ars" -type f | xargs blah blah

Last edited by peets (2008-07-09 03:48:07)

Offline

#6 2008-07-09 13:01:20

emphire
Member
From: Canada
Registered: 2007-03-21
Posts: 203

Re: newbie bash scripting help!

peets wrote:
find -iname "*-123.ars" -type f | xargs blah blah

xargs is very handy, but be careful when using it with the find command.  If find returns too many results, xargs will generate a command that's too long for bash and it won't run the command.

Offline

#7 2008-07-09 19:16:42

jnwebb
Member
Registered: 2008-07-07
Posts: 52

Re: newbie bash scripting help!

thanks again for the help ... I will post my final script in a few.

Anyone know how to delete/remove the last 3 characters in a text file.  They occur elsewhere in the file but I need them removed at the end.

Offline

#8 2008-07-09 20:28:25

briest
Member
From: Katowice, PL
Registered: 2006-05-04
Posts: 468

Re: newbie bash scripting help!

head -c -3 textfile maybe?

Offline

#9 2008-07-09 20:52:22

jnwebb
Member
Registered: 2008-07-07
Posts: 52

Re: newbie bash scripting help!

that returns the first three lines of the file

Offline

#10 2008-07-09 21:51:58

lldmer
Member
From: Amsterdam
Registered: 2008-05-17
Posts: 119

Re: newbie bash scripting help!

tail?:P


For lack of better words: chair, never, toothbrush, really. Ohw, and fish!

Offline

#11 2008-07-10 09:53:05

shining
Pacman Developer
Registered: 2006-05-10
Posts: 2,043

Re: newbie bash scripting help!

If the last 3 char are on their own line, you can just delete the last line, for example with sed :
sed '$d' file
Otherwise if these 3 char are not alone, but are always the same string, for example "abc" you can do this:
sed '$s/abc//' file

The $ indicate that this substitution will be only made on the last line.


pacman roulette : pacman -S $(pacman -Slq | LANG=C sort -R | head -n $((RANDOM % 10)))

Offline

#12 2008-07-10 10:01:36

DonVla
Member
From: Bonn, Germany
Registered: 2007-06-07
Posts: 997

Re: newbie bash scripting help!

jnwebb wrote:

Hello,

I would like some help with a simple script.

I have say 10 text files (***.txt) in a directory.  I want to make each *** into a subdirectory, insert the respective ***.txt file into that subdirectory as well as insert three other files into each subdirectory.

I then want to create a loop that will run a seperate script, which I already have, on each folder.

Thanks for any help

i think you need some basics:
http://www.gnu.org/software/bash/manual/bashref.html
http://wooledge.org:8000/BashGuide
http://wooledge.org:8000/BashFAQ
http://wooledge.org:8000/BashPitfalls
http://sed.sourceforge.net/sed1line.txt
http://student.northpark.edu/pemente/awk/awk1line.txt

Offline

#13 2008-07-10 14:23:57

peets
Member
From: Montreal
Registered: 2007-01-11
Posts: 936
Website

Re: newbie bash scripting help!

DonVla wrote:
jnwebb wrote:

Hello,

I would like some help with a simple script.

I have say 10 text files (***.txt) in a directory.  I want to make each *** into a subdirectory, insert the respective ***.txt file into that subdirectory as well as insert three other files into each subdirectory.

I then want to create a loop that will run a seperate script, which I already have, on each folder.

Thanks for any help

i think you need some basics:
http://www.gnu.org/software/bash/manual/bashref.html
http://wooledge.org:8000/BashGuide
http://wooledge.org:8000/BashFAQ
http://wooledge.org:8000/BashPitfalls
http://sed.sourceforge.net/sed1line.txt
http://student.northpark.edu/pemente/awk/awk1line.txt

Sorry for always being so propagandish, but I feel it's worthwile for your enjoyment and efficiency to mention that perl does all this and more. http://perldoc.perl.org

But, uh, "use the right tool for the right job". For the last 3 characters problem, briest gives the right solution. It works for me. Make sure you put the '-' in front of the 3.

Offline

#14 2008-07-10 14:30:19

shining
Pacman Developer
Registered: 2006-05-10
Posts: 2,043

Re: newbie bash scripting help!

peets wrote:

But, uh, "use the right tool for the right job". For the last 3 characters problem, briest gives the right solution. It works for me. Make sure you put the '-' in front of the 3.

Waoh, I feel stupid now, I just read the answers quickly and saw that jnwebb said it didn't work so I did not look further and tried with other tools.
"head -c -3" works perfectly fine indeed, and it is a very easy and elegant solution. Sorry.

PS : I suppose my rank title is ironic and is here for making fun of me

Last edited by shining (2008-07-10 14:39:05)


pacman roulette : pacman -S $(pacman -Slq | LANG=C sort -R | head -n $((RANDOM % 10)))

Offline

#15 2008-07-11 20:07:11

jnwebb
Member
Registered: 2008-07-07
Posts: 52

Re: newbie bash scripting help!

thanks to all for suggestions and help... I am progressing...although slowly...in learning the basics.  Your advice and help is both encouraging and helpful.

how can I list out the files in a folder into an array?  I am doing the following:

ls *.filesIneed.txt > temp.txt
old_IFS=$IFS
IFS=$'/n'
ARRAY=($(cat temp.txt))
IFS=$old_IFS

But this seems to pump all of the files listed into the first entry of the ARRAY....any help?

Offline

#16 2008-07-11 20:35:50

shining
Pacman Developer
Registered: 2006-05-10
Posts: 2,043

Re: newbie bash scripting help!

Are you sure you need to do this? Usually, you can just do : for i in *; do [ something with $i ] ; done


pacman roulette : pacman -S $(pacman -Slq | LANG=C sort -R | head -n $((RANDOM % 10)))

Offline

#17 2008-07-11 20:47:23

jnwebb
Member
Registered: 2008-07-07
Posts: 52

Re: newbie bash scripting help!

yes...i will have a list of files   (***.RS, and ***.CS) .  I need to list them into two separate arrays and then use the data sets that are in them to do ration plots in gnuplot....

I need to be able set up a loop that will create a file with each line of the file having the following for each respective set of files)

"< paste FILE1.RS FILE1.CS using 1:($2:$4) with lines...etc" for gnuplot

Offline

Board footer

Powered by FluxBB