System Administration
cancel
Showing results for 
Search instead for 
Did you mean: 

perl regular expressions help

SOLVED
Go to solution
Junior C.
Frequent Advisor

perl regular expressions help

I have a text file consist of the following.
/usr/bin/file1.txt
/home/user/file2.txt
/sbin/init.d/file3.txt
.
.
/etc/opt/file20.txt

I wan't read the file and send the following output to text file or array.

/usr/bin
/home/user
/sbin/init.d
.
.
/etc/opt


Appreciate all help.

Thanks,

JC
3 REPLIES
Matti_Kurkela
Honored Contributor
Solution

Re: perl regular expressions help

You don't even need perl for that - it's easy enough with shell scripting:

#!/bin/sh
while read PATHNAME
do
dirname "$PATHNAME"
done

Pipe in the input file and redirect the output to wherever you want:

sh script.sh outputfile.txt

MK
MK
Stuart Browne
Honored Contributor

Re: perl regular expressions help

perl -n -e 'm#(.*/).*# and print $1 . "\n"' yourfile > outfile

A perl solution.
One long-haired git at your service...
James R. Ferguson
Acclaimed Contributor

Re: perl regular expressions help

Hi JC:

Yet another Perl solution:

# perl -MFile::Basename -nle 'print dirname($_)' filenames

Regards!

...JRF...