I have list of files like below:
[root@ods1 backup]# ls -l
total 93892810
-rw-r----- 1 root root 651248 Feb 17 08:34 abc_def_g_17-02-2022.sql
-rw------- 1 root root 665248 Mar 23 08:46 bbc_23-03-2022.sql
-rw-r----- 1 root root 676992 Apr 04 16:52 zz_b_04-04-2022.sql
What i need to achieve is:
abc_def_g
bbc
zz_b
I tried to do this but it didn't work:
ls -l | awk '{print $9}' | cut -d '_' -f1-2
How can I achieve this goal? Any series of commands that get the lines until last _ character should do the job.
Edit: This commands are executed in CentOS 8.
tokens+=("${f%_*}")
should work for appending to the array in the shells that support arrays. Or eventokens=(*); tokens=("${tokens[@]%_*}")
for the whole deal. – ilkkachu Apr 21 '22 at 07:12