Hi,
I need to cut filename in Linux ksh.
for example file name is c_xxxx_cp_200908175035.zip. I need to get variable with only c_xxxx_cp value.
what have you tried?
dir_name=${zip_file%.*}
dir_name_1=${dir_name#*JULIY/}
This will give me file name without ".zip", but still I have to get rid of timestamp.
the last "_" underscore is before the extension. therefore you can use % which means "shortest match from the back"
${zip_file%_*}
please read again Advance Bash guide (google) for details on string manipulation
Thanks a lot.
Even Simpler :
echo "c_xxxx_cp_200908175035.zip" | cut -d "." -f 1 | cut -d "_" -f 1,2,3
certainly not simpler. you don't need to call 2 cuts.
fname="c_xxxx_cp_200908175035.zip"
file_without_ext=$(basename $fname .zip)
echo $file_without_ext
output:
c_xxxx_cp_200908175035
-----Post Update-----
fname="test_high_prio123.zip"
file_name_wo_ext=$(basename $fname .zip)
echo $file_name_wo_ext
O/P:
test_high_prio123
echo c_xxxx_cp_200908175035.zip | sed 's/_[^_]*$//'
Or using ksh93 extended patterns:
fname="test_high_prio123_200908175035.zip"
echo "${fname//_{12,12}([[:digit:]]).*/}
test_high_prio123
echo "c_xxxx_cp_200908175035.zip"|cut -d_ -f1-3
c_xxxx_cp