Extracting a portion of the filename

Hi

I would like to extract the first portion of filename from a list of files.

The filename pattern is of the form 123456789_TEXT_TEXT_TEXT_.csv. I want to extract just the numerical portion of this filename from the list of files and then output this into another text file.

K

Hi,

What have you tried so far ?

Thanks
Pravin

Hi Pravin

I am thinking of using the sed command, but dont know how to go about it.

I have a script

for i in `ls *TEXTPATTERN*`;do grep String $i | wc -l | grep -v 0 | $i >> matchedfiles.txt;done

on the last $i I would like to just output a portion filenames for matched file.

$ a="123456789_TEXT_TEXT_TEXT_.csv"
$ echo ${a%%_*}
123456789
$ echo $a | sed 's/_.*//'
123456789
for i in *TEXT*
do echo ${i%%_*}
done >matchedfiles.txt

Could this help you ?
This will print the first part of the file (string seperated by "_")

 
awk -F"_" '{print $1}' ListFiles > newFile.txt
cut -d"_" -f1 filename

This is easieds and simple you can do

filename="123456789_TEXT_TEXT_TEXT_.csv"
ext="${filename:0:9}"
echo $ext