I have an ASCII file containing filepaths which I read by running:
while read p; do echo $p; done < filelist.txt
The file contains filepaths with the following pattern:
./first/example1/path
./second/example1/path
./third/example2/path
How can I get a specific part of the path string (from /
to /
), e.g.
I need to get an output that prints:
first
second
third
and also
example1
example1
example2
I'm sure there is a way of doing this using regular expressions and sed
, but I'm not familiar with it.
Best Answer
Use
cut
:The
-d/
sets the column delimiter to/
and the-f2
selects the 2nd column.You can of course also use Bash variables instead of a file name or pipe data into the
cut
command: