Unix Shell - trying to extract single lines from files using the bash shell

This is Interesting: Free IT Magazines  
Home > Archive > Unix Shell > October 2006 > trying to extract single lines from files using the bash shell





You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

Author trying to extract single lines from files using the bash shell
onetitfemme

2006-10-14, 1:20 pm

I need to extract to different files, just one or a number on lines
from files which names I know
..
The thing is that I can run a script by entering on the command
prompt, but what seems to me like the same thing does not appear to be
working when I try a script.
..
and BTW I don't want to use PERL for that
..
XKLD_LN="iny" "miny" "miney" "mo"
..
echo "// __ looping over files"
for i in 0 1 2 3; do
echo "// __ looping over words"
for w in $XKLD_LN; do
echo "// __ i: $i, "
grep -v "$w" log_"$i".txt > log__"$i"_"$w".txt
done
done
..
How could you achieve this?
..
onetitfemme

Janis Papanagnou

2006-10-14, 7:37 pm

onetitfemme wrote:
> I need to extract to different files, just one or a number on lines
> from files which names I know
> .
> The thing is that I can run a script by entering on the command
> prompt, but what seems to me like the same thing does not appear to be
> working when I try a script.
> .
> and BTW I don't want to use PERL for that
> .
> XKLD_LN="iny" "miny" "miney" "mo"


XKLD_LN="iny miny miney mo"

> .
> echo "// __ looping over files"
> for i in 0 1 2 3; do
> echo "// __ looping over words"
> for w in $XKLD_LN; do
> echo "// __ i: $i, "
> grep -v "$w" log_"$i".txt > log__"$i"_"$w".txt
> done
> done
> .
> How could you achieve this?
> .
> onetitfemme
>


Or use egrep...

egrep '(iny|miny|miney|mo)'


Janis
Chris F.A. Johnson

2006-10-14, 7:37 pm

On 2006-10-14, onetitfemme wrote:
> I need to extract to different files, just one or a number on lines
> from files which names I know
> .
> The thing is that I can run a script by entering on the command
> prompt, but what seems to me like the same thing does not appear to be
> working when I try a script.


What does "does not appear to be working" mean? What does happen?
What did you expect?

> and BTW I don't want to use PERL for that
> .
> XKLD_LN="iny" "miny" "miney" "mo"


That line assigns "iny" to variable XKLD_LN, then runs the command
"miny" with "miney" and "mo" as arguments. If you want all four
words in the variable, use:

XKLD_LN="iny miny miney mo"

In bash, you could also use an array:

XKLD_LN=( iny miny miney mo )

> echo "// __ looping over files"
> for i in 0 1 2 3; do
> echo "// __ looping over words"
> for w in $XKLD_LN; do
> echo "// __ i: $i, "
> grep -v "$w" log_"$i".txt > log__"$i"_"$w".txt


Do you really want lines that do NOT contain "iny" in a file named
"log__0_iny.txt"? That's what the -v option does.

> done
> done
> .
> How could you achieve this?


That depends on what you want to achieve.

--
Chris F.A. Johnson, author <http://cfaj.freeshell.org/shell>
Shell Scripting Recipes: A Problem-Solution Approach (2005, Apress)
===== My code in this post, if any, assumes the POSIX locale
===== and is released under the GNU General Public Licence
Bill Marcum

2006-10-15, 1:31 am

["Followup-To:" header set to comp.unix.shell.]
On 14 Oct 2006 11:01:50 -0700, onetitfemme
<onetitfemme2005@yahoo.com> wrote:
> I need to extract to different files, just one or a number on lines
> from files which names I know
> .
> The thing is that I can run a script by entering on the command
> prompt, but what seems to me like the same thing does not appear to be
> working when I try a script.
> .
> and BTW I don't want to use PERL for that
> .
> XKLD_LN="iny" "miny" "miney" "mo"

With quoting like this, I'd be surprised if it works from the command
line. This line would try to execute the command "miny" with arguments
"miney" "mo" and the variable XKLD_LN="iny". If you change the line to
XKLD_LN="iny miny miney mo"
the rest of the script looks like it would work.

> .
> echo "// __ looping over files"
> for i in 0 1 2 3; do
> echo "// __ looping over words"
> for w in $XKLD_LN; do
> echo "// __ i: $i, "
> grep -v "$w" log_"$i".txt > log__"$i"_"$w".txt
> done
> done
> .
> How could you achieve this?
> .
> onetitfemme
>



--
Consider well the proportions of things. It is better to be a young June-bug
than an old bird of paradise.
-- Mark Twain, "Pudd'nhead Wilson's Calendar"
onetitfemme

2006-10-18, 1:36 am

OK, I might not have been clear enough:
..
I am trying to extract a number of possible strings (which may contain
spaces) from a number of files. I was thinking of coding it like this:
..
XKLD_LN=("iny miny" "miney mo");

echo "// __ looping over files"
for i in 0 1 2 3; do
echo "// __ looping over words"
for w in $XKLD_LN; do
echo "// __ i: $i, $w"
# grep -v "$w" log_$i.txt > log__$i_$w.txt
done
done
..
Also, better than having the name of the reslting file as
"log__$i_$w.txt", I would like to use an index How could you get an
index from the second loop>
..
Thanks
otf

Chris F.A. Johnson

2006-10-18, 1:36 am

On 2006-10-18, onetitfemme wrote:
> OK, I might not have been clear enough:


About what? Please include context from previous articles.

> I am trying to extract a number of possible strings (which may contain
> spaces) from a number of files. I was thinking of coding it like this:
> .
> XKLD_LN=("iny miny" "miney mo");
>
> echo "// __ looping over files"
> for i in 0 1 2 3; do
> echo "// __ looping over words"
> for w in $XKLD_LN; do


That doesn't loop over anything but the first element in the
array.

for w in "${XKLD_LN[@]}"; do

> echo "// __ i: $i, $w"
> # grep -v "$w" log_$i.txt > log__$i_$w.txt
> done
> done
> .
> Also, better than having the name of the reslting file as
> "log__$i_$w.txt", I would like to use an index How could you get an
> index from the second loop>


What type of index?

--
Chris F.A. Johnson, author <http://cfaj.freeshell.org/shell>
Shell Scripting Recipes: A Problem-Solution Approach (2005, Apress)
===== My code in this post, if any, assumes the POSIX locale
===== and is released under the GNU General Public Licence
onetitfemme

2006-10-19, 1:25 am

> for w in "${XKLD_LN[@]}"; do
..
Thank you, this is exactly what I needed.
..
Now, I have a last question. How do you make each command run in a
loop run as part of its own process? say:
..
for w in ${W}; do
# read in its own thread ...
doce
..

Chris F.A. Johnson

2006-10-19, 1:25 am

On 2006-10-19, onetitfemme wrote:
> .
> Thank you, this is exactly what I needed.
> .
> Now, I have a last question. How do you make each command run in a
> loop run as part of its own process? say:
> .
> for w in ${W}; do
> # read in its own thread ...
> doce


You can create a subshell (is that what you want?) by enclosing the
command in parentheses:

( read x y z
: do something with $x, $y, $z
)

--
Chris F.A. Johnson, author <http://cfaj.freeshell.org/shell>
Shell Scripting Recipes: A Problem-Solution Approach (2005, Apress)
===== My code in this post, if any, assumes the POSIX locale
===== and is released under the GNU General Public Licence
onetitfemme

2006-10-19, 7:32 am

> ... is that what you want?
..
Subshells do not appear to be what I need because when I time the
worker tasks I notice they run one after the other (even in a
subshell).
..
I need for each worker (thread or process) to work independently so
the loop just launches them without waiting for the previous one to
finish
..
How can you do that using bash from the OS?
..
otf

Pascal Bourguignon

2006-10-19, 7:32 am

"onetitfemme" <onetitfemme2005@yahoo.com> writes:

> .
> Subshells do not appear to be what I need because when I time the
> worker tasks I notice they run one after the other (even in a
> subshell).
> .
> I need for each worker (thread or process) to work independently so
> the loop just launches them without waiting for the previous one to
> finish
> .
> How can you do that using bash from the OS?


By reading the manual of bash.

man bash


--
__Pascal Bourguignon__ http://www.informatimago.com/
The rule for today:
Touch my tail, I shred your hand.
New rule tomorrow.
Janis

2006-10-19, 1:21 pm

onetitfemme wrote:

[ please quote sufficient context ]
[vbcol=seagreen]
> Subshells do not appear to be what I need because when I time the
> worker tasks I notice they run one after the other (even in a
> subshell).
> .
> I need for each worker (thread or process) to work independently so
> the loop just launches them without waiting for the previous one to
> finish


Then you seem to ask for a subshell as a background process...

( ...whatever to be done... )&


Janis

> .
> How can you do that using bash from the OS?
> .
> otf


Chris F.A. Johnson

2006-10-19, 1:21 pm

On 2006-10-19, onetitfemme wrote:
> .
> Subshells do not appear to be what I need because when I time the
> worker tasks I notice they run one after the other (even in a
> subshell).
> .
> I need for each worker (thread or process) to work independently so
> the loop just launches them without waiting for the previous one to
> finish
> .
> How can you do that using bash from the OS?


Put them into the background with &.

--
Chris F.A. Johnson, author <http://cfaj.freeshell.org/shell>
Shell Scripting Recipes: A Problem-Solution Approach (2005, Apress)
===== My code in this post, if any, assumes the POSIX locale
===== and is released under the GNU General Public Licence
onetitfemme

2006-10-21, 1:27 am

Still having a little problem:
..
if you run a batch/sequence of scripts a la:
..
../script00.sh
..
../script02.sh
..
../script04.sh
..
and say the first one, script00.sh, runs a batch that fires independent
longer running process, like this:
..
for i in 0 1 2 3; do
(what ever takes time and can be run in parallel)&
done
..
how can you make sure that the subsequent scripts start only if all
processes fired by script00 are done?
..
otf

Bill Marcum

2006-10-21, 7:25 am

["Followup-To:" header set to comp.unix.shell.]
On 20 Oct 2006 23:14:34 -0700, onetitfemme
<onetitfemme2005@yahoo.com> wrote:
> Still having a little problem:
> .
> if you run a batch/sequence of scripts a la:
> .
> ./script00.sh
> .
> ./script02.sh
> .
> ./script04.sh
> .
> and say the first one, script00.sh, runs a batch that fires independent
> longer running process, like this:
> .
> for i in 0 1 2 3; do
> (what ever takes time and can be run in parallel)&
> done
> .
> how can you make sure that the subsequent scripts start only if all
> processes fired by script00 are done?
> .


wait


--
The question of whether computers can think is just like the question of
whether submarines can swim.
-- Edsger W. Dijkstra
Pascal Bourguignon

2006-10-21, 7:25 am

"onetitfemme" <onetitfemme2005@yahoo.com> writes:

> Still having a little problem:
> .
> if you run a batch/sequence of scripts a la:
> .
> ./script00.sh
> .
> ./script02.sh
> .
> ./script04.sh
> .
> and say the first one, script00.sh, runs a batch that fires independent
> longer running process, like this:
> .
> for i in 0 1 2 3; do
> (what ever takes time and can be run in parallel)&
> done
> .
> how can you make sure that the subsequent scripts start only if all
> processes fired by script00 are done?


By waiting. You still need to read the manual of bash.

man bash


--
__Pascal Bourguignon__ http://www.informatimago.com/

Nobody can fix the economy. Nobody can be trusted with their finger
on the button. Nobody's perfect. VOTE FOR NOBODY.
Sponsored Links






Free braindumps | Software forum | Database administration forum

Copyright 2003 - 2008 webservertalk.com