Quote:
Originally Posted by ampsys
I have a file like this:
Code:
./Daily/SERVER1/SERVER1-20160101010101
./Daily/SERVER1/SERVER1-20160102010101
./Daily/SERVER1/SERVER1-20160103010101
I want to get at the SERVER1 part of that path (by itself), as well as the SERVER1-'Date' part by itself, and store each as variables.
|
I'm going to guess that you are trying a shell script and not perl or python.
In shell variables are assigned without a dollar sign, but need one all other times.
Code:
foo="123 456 789";
echo $foo;
echo foo;
So if you have that list of files in a separate file to be read one line at a time, you could do it like this using
dirname and
basename:
Code:
while read line;
do
f=$(basename $line);
d=$(dirname $line);
echo File = $f;
echo Directory = $d;
done < files.txt
Be sure to see the manual pages for all the options. And see
realpath if you need to calculate the full path.
Code:
man dirname
man basename
man realpath
read is a built-in function of the shell. See the manual page for your particular shell. Treat that manual page like a reference and don't be afraid to skip the irrelevant material and search down to the right section.