freemen
freemen

Reputation: 25

Ubuntu linux command extract substring inside

I have a txt file on ubuntu containing

-rw-r--r-- 1 ftp ftp 0 Feb 26 11:37 6.txt
-rw-r--r-- 1 ftp ftp 0 Feb 26 11:37 7.txt
-rw-r--r-- 1 ftp ftp 0 Feb 26 11:37 8.txt

can I just want to retrieve the file name like

6.txt
7.txt
8.txt

to another text file

Upvotes: 0

Views: 211

Answers (4)

admiring_shannon
admiring_shannon

Reputation: 934

If your file contains space, it could look like

-rw-r--r-- 1 ftp ftp 0 Feb 26 11:37 6.txt 
-rw-r--r-- 1 ftp ftp 0 Feb 26 11:37 7.txt 
-rw-r--r-- 1 ftp ftp 0 Feb 26 11:37 a filename with space

use this

cut -d' ' -f9- f1.txt > f2.txt

Upvotes: 0

runejuhl
runejuhl

Reputation: 2237

If you file name are pretty uniform, I'd go with grep and a regular expression:

$ grep --color=never -oE '[[:alnum:]]+\.[[:alnum:]]{3}$' files.txt

The above disables color (which may interfere), only outputs the matching part (-o), uses extended regex (-E), and then finds one or more alpha-numeric characters, followed by a dot ("."), followed by exactly three alpha-numeric characters ([[:alnum:]]) and a line ending (the '$').

Upvotes: 0

Vijay
Vijay

Reputation: 67301

awk '{print $NF}' your_file >only_names.txt

or

perl -lane '{print $F[scalar(@F)-1]}' your_file >only_names.txt

and if you want to change the existing file:

perl -i -lane '{print $F[scalar(@F)-1]}' your_file

Upvotes: 2

user184968
user184968

Reputation:

cut -d' ' -f 9 f1.txt > f2.txt

Upvotes: 2

Related Questions