0

i have hundreds of file having name in a specific pattern , i want to get portion of filename only for example

like if filenames are REE_45K.txt or REE_46B.txt or REE_21K.txt or REE_229N.txt ot REE9999G.txt i want to display only 45K 46B ,21K,229N,9999G

if possible please give Linux and windows versions commands

findstr _.*.  *.*  i found this command working for windows , can u verify it 
user143252
  • 57
  • 9

3 Answers3

1

For bash on linux:

#!/bin/bash
filename="REE_45K.txt"
no_ext="${filename%.*}"  # "REE_45K"
echo "${no_ext##*_}"

gives 45K

Kind Stranger
  • 1,498
  • 11
  • 18
0

For linux bash:

for filename in REE_45K.txt  REE_46B.txt  REE_21K.txt
do
  echo $filename |sed  's/REE_\(.*\).txt/\1/g'
done

It gives:

45K
46B
21K
xiawi
  • 1,648
  • 4
  • 16
  • 19
0

Here is how you string replace in a windows batch file. {var} = %{var}:{find}={relacement}% Here is an example for you:

@echo off
set filename="REE_45K.txt"
echo %filename%
set filename=%filename:REE_=%
set filename=%filename:.txt=%
echo %filename%

Output:

"REE_45K.txt"
"45K"