Question

-rw-r--r-- 1 root root 3178 Jun 29 16:08 FTS_TESTCAMPAIGN1_29_06_2013_160823.html
-rw-r--r-- 1 root root  711 Jun 29 16:08 FTS_ABORT_BACKUP_JOB_1_29_06_2013_160823.html
-rw-r--r-- 1 root root  405 Jun 29 16:08 FTS_ABORT_BACKUP_JOB_1_29_06_2013_160823.csv
-rw-r--r-- 1 root root  466 Jun 29 16:08 FTS_ABORT_BACKUP_JOB_1_29_06_2013_160823
-rw-r--r-- 1 root root 3178 Jun 29 16:14 FTS_TESTCAMPAIGN1_29_06_2013_161404.html
-rw-r--r-- 1 root root  711 Jun 29 16:14 FTS_ABORT_BACKUP_JOB_1_29_06_2013_161404.html
-rw-r--r-- 1 root root  405 Jun 29 16:14 FTS_ABORT_BACKUP_JOB_1_29_06_2013_161404.csv
-rw-r--r-- 1 root root  466 Jun 29 16:14 FTS_ABORT_BACKUP_JOB_1_29_06_2013_161404
-rw-r--r-- 1 root root    0 Jun 29 16:25 log

I have the directory lising in a file as above. I need the output as:

FTS_TESTCAMPAIGN1_29_06_2013_160823.html
FTS_ABORT_BACKUP_JOB_1_29_06_2013_160823.html
FTS_ABORT_BACKUP_JOB_1_29_06_2013_160823.csv
FTS_ABORT_BACKUP_JOB_1_29_06_2013_160823
FTS_TESTCAMPAIGN1_29_06_2013_161404.html
FTS_ABORT_BACKUP_JOB_1_29_06_2013_161404.html
FTS_ABORT_BACKUP_JOB_1_29_06_2013_161404.csv
FTS_ABORT_BACKUP_JOB_1_29_06_2013_161404
log

How can I do it using Perl?

Was it helpful?

Solution

As long as the directory listing maintains the same structure, the following quick and dirty perl one-liner will do the job:

perl -pi.bak -ae 's/^.*$/$F[8]/' FILENAME

OTHER TIPS

use File::Listing qw(parse_dir);
use File::Slurp qw(read_file);

print $_->[0], "\n" for parse_dir read_file 'ls-l';
perl -naE 'say $F[$#F]' file

That is, enable auto-split and print the last field on each line.

Analogously, in awk:

awk '{print $NF}' file
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top