From: StephaneLeFou on
Hi,

From a shell script, I'd like to extract the filenames
(file001...file004) provided by a flat file that looks like:

/disk2/incoming/file001;ABC;0000000010
/disk2/incoming/file002;ABC;0000000033
/disk2/incoming/file003;ABC;0000000002
/disk2/incoming/file004;ABC;0000000099

How do I extract those filenames and put them inside a varible for
example (in a loop)?

Thanks for your help.
From: Peter van Hooft on
On 2008-06-24, StephaneLeFou <stephanelefou(a)gmail.com> wrote:
> Hi,
>
> From a shell script, I'd like to extract the filenames
> (file001...file004) provided by a flat file that looks like:
>
> /disk2/incoming/file001;ABC;0000000010
> /disk2/incoming/file002;ABC;0000000033
> /disk2/incoming/file003;ABC;0000000002
> /disk2/incoming/file004;ABC;0000000099
>
> How do I extract those filenames and put them inside a varible for
> example (in a loop)?
>
> Thanks for your help.

It depends on what type of shell you use.

In bash, zsh and ksh the following should work

for i in "/disk2/incoming/file001;ABC;0000000010" \
"/disk2/incoming/file002;ABC;0000000033" \
"/disk2/incoming/file003;ABC;0000000002" \
"/disk2/incoming/file004;ABC;0000000099"
do
file=${i##*/}
file=${file%%;*}
echo $file
done


peter

From: Dave B on
StephaneLeFou wrote:

> Hi,
>
> From a shell script, I'd like to extract the filenames
> (file001...file004) provided by a flat file that looks like:
>
> /disk2/incoming/file001;ABC;0000000010
> /disk2/incoming/file002;ABC;0000000033
> /disk2/incoming/file003;ABC;0000000002
> /disk2/incoming/file004;ABC;0000000099
>
> How do I extract those filenames and put them inside a varible for
> example (in a loop)?

Many ways:

awk -F';' '{print $1}' yourfile
cut -d';' -f 1 yourfile
sed 's/\([^;]*\).*/\1/' yourfile
grep -o '[^;]*' yourfile

all of these can be piped to a loop.

--
echo 0|sed 's909=oO#3u)o19;s0#0ooo)].O0;s()(0bu}=(;s#}#.1m"?0^2{#;
s)")9v2@3%"9$);so%op]t(p$e#!o;sz(z^+.z;su+ur!z"au;sxzxd?_{h)cx;:b;
s/\(\(.\).\)\(\(..\)*\)\(\(.\).\)\(\(..\)*#.*\6.*\2.*\)/\5\3\1\7/;
tb'|awk '{while((i+=2)<=length($1)-18)a=a substr($1,i,1);print a}'
From: Juha Laiho on
StephaneLeFou <stephanelefou(a)gmail.com> said:
>From a shell script, I'd like to extract the filenames
>(file001...file004) provided by a flat file that looks like:
>
>/disk2/incoming/file001;ABC;0000000010
>/disk2/incoming/file002;ABC;0000000033
>/disk2/incoming/file003;ABC;0000000002
>/disk2/incoming/file004;ABC;0000000099
>
>How do I extract those filenames and put them inside a varible for
>example (in a loop)?

sed 's:.*/::; s:;.*::' flatfile | while read file; do
echo $file
done

.... and explained in parts (recommended that you also experiment with
these pieces yourself):

sed 's:.*/::; s:;.*::' flatfile
will process 'flatfile' by reading it a line a the time,
removing from start the longest possible part of the line that ends with
character / (the s:.*/:: part; it is a substitution operation, where the
replacement is an empty string),
and then removing the longest possible part of the line that starts with
a semicolon (the s:;.*:: part, similar substitution to the above).

So, if you run the above part by itself, it will print out just the file
names.

Then to the "processing":
while read file; do
echo $file
done

will simply read its input one line at a time, and assign the contents of
that line to variable called "file" (just a name I picked, could be more
or less anything). The 'echo $file' will then just output the variable -
but here is where you could place your own processing; whatever you would
like to do with the file names.
--
Wolf a.k.a. Juha Laiho Espoo, Finland
(GC 3.0) GIT d- s+: a C++ ULSH++++$ P++@ L+++ E- W+$@ N++ !K w !O !M V
PS(+) PE Y+ PGP(+) t- 5 !X R !tv b+ !DI D G e+ h---- r+++ y++++
"...cancel my subscription to the resurrection!" (Jim Morrison)
From: Dave B on
Dave B wrote:

> StephaneLeFou wrote:
>
>> Hi,
>>
>> From a shell script, I'd like to extract the filenames
>> (file001...file004) provided by a flat file that looks like:
>>
>> /disk2/incoming/file001;ABC;0000000010
>> /disk2/incoming/file002;ABC;0000000033
>> /disk2/incoming/file003;ABC;0000000002
>> /disk2/incoming/file004;ABC;0000000099
>>
>> How do I extract those filenames and put them inside a varible for
>> example (in a loop)?
>
> Many ways:
>
> awk -F';' '{print $1}' yourfile
> cut -d';' -f 1 yourfile
> sed 's/\([^;]*\).*/\1/' yourfile
> grep -o '[^;]*' yourfile
>
> all of these can be piped to a loop.

Sorry, I misread your question. Try these:

awk -F'[/;]' '{print $4}' yourfile
sed 's_.*/\([^/;]*\);.*_\1_' yourfile

--
echo 0|sed 's909=oO#3u)o19;s0#0ooo)].O0;s()(0bu}=(;s#}#.1m"?0^2{#;
s)")9v2@3%"9$);so%op]t(p$e#!o;sz(z^+.z;su+ur!z"au;sxzxd?_{h)cx;:b;
s/\(\(.\).\)\(\(..\)*\)\(\(.\).\)\(\(..\)*#.*\6.*\2.*\)/\5\3\1\7/;
tb'|awk '{while((i+=2)<=length($1)-18)a=a substr($1,i,1);print a}'