Cut the string

Hi in a directory i've files having the following name

for_category_info_19990101984301
for_catgry_meta_19991111214601
-------

I just want the name till year and month i.e;

for_category_info_199901
for_catgry_meta_199911

How can i achieve the above string

Thanks

echo "for_category_info_19990101984301" | sed 's/\(.*_[0-9]\{6\}\).*/\1/'
1 Like

It depends how you are planning on using it. Is this within a shell script loop perhaps? ksh and others should allow you to slice the string too. I avoid the word cut as that is a command.

Consider the code:-

#!/bin/ksh

for file in *
do
   file_trimmed="${file%??}"
   echo "File \"$file\" is trimmed to \"$file_trimmed\""
done

If you explain more about what the end goal is, then you may get a better suggestion.

I hope that this helps.

Robin
Liverpool/Blackburn
UK