A
A
Azat20152017-03-31 22:43:03
linux
Azat2015, 2017-03-31 22:43:03

How to get part of a filename in bash?

There is a directory with files named according to the pattern username_phonenumber.txt . How to get a piece of phonenumber in bash?
Thank you.

Answer the question

In order to leave comments, you need to log in

4 answer(s)
V
Valentine, 2017-03-31
@Azat2015

for i in ls dir; do
   var=${i#*_};
   echo ${var%%.txt};
done

ls -1 dir | grep -Eo '_(\w+)\.'

A
Andrey Burov, 2017-03-31
@BuriK666

ls *_*.txt|sed 's/^.*_\(.*\)\.txt$/\1/'

X
xotkot, 2017-04-01
@xotkot

ls | awk -F"_|.txt" '{print $2}'

S
Saboteur, 2017-03-31
@saboteur_kiev

ls -1 | cut -d "_" -f 2 | cut -d "." -f 1

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question