Cut a string for last 8 characters

Hello All

I have a file like this

abc.tpt.ctl
bdc.tpt.ctl
cdw.tpt.ctl

I have looped every line using the for Loop, now I want to take each line and cut the .tpt.ctl part of it and store it in a variable and use the variable in same loop.

The part I am stuck at is how do I cut the last 8 characters of each line or a single line using cut command

Using awk

awk '{print substr($0,length($0)-7)}' filename
1 Like

Try:

for i in *.tpt.ctl
do
  var=${i%.tpt.ctl}
  printf "%s\n" "$var"
done
1 Like

Or using sed:

sed 's/\(.*\).\{8\}$/\1/' file

1 Like

@krishmaths
Simpler approach

awk -F. '{print $1}'

But why do it complicate, when we can do it the Scrutinizer way :slight_smile:

1 Like
echo abc.tpt.ctl | rev | cut -c 9- | rev
abc
 
bash-3.2$ cat x
abc.tpt.ctl
bdc.tpt.ctl
cdw.tpt.ctl
bash-3.2$ cat x | rev | cut -c 9- | rev
abc
bdc
cdw
1 Like

Hello,

My loop is like this


TPT_PATH='/home/ctl/tpt'
CP_PATH='/home/ctl/copy'

for line in filelist.txt ;
do
  if [ -f $TPT_PATH/$line ]
     then
     echo "###################################################################################################" >> $CP_PATH/$line

The problem is when i write the echo statement into the file the file is named as .tpt.ctl, instead i need it to be .copy.vsql
So I want to put a variable inside this loop which takes maiden name of file without .tpt.ctl and then I can use the variable in echo statement >> $CP_PATH/$variable.copy.vsql

---------- Post updated at 01:36 PM ---------- Previous update was at 01:27 PM ----------

Thanks Mr Bean,

The solution worked for me.

Thanks you all.

May this also help you..

Last_eight_char=$(echo ${filename#*.})
echo "$Last_eight_char"

the code will take out the all the extensions instead of last 8 characters. i think this is your requirement. i suggest this because extensions can be more than 3 characters also.

---------- Post updated at 02:20 PM ---------- Previous update was at 01:53 PM ----------

hi, you can use the below code also.. i think file extensions can be more tha 3 characters also.

TPT_PATH='/home/ctl/tpt'
CP_PATH='/home/ctl/copy'

for line in filelist.txt ;
do
  if [ -f $TPT_PATH/$line ]
  then
        line=$(echo ${line%*.*.*}).copy.vsql
        echo "###################################################################################################" >> $CP_PATH/$line
  fi
done

@little:

$(echo ${line%*.*.*}).copy.vsql

could be this:

${line%.*.*}.copy.vsql

Also note that the first asterisk should not be there. It only works because you are using lazy matching (single percent sign) so the first asterisk is matching zero characters .

Assuming the file is linear, a longhand version.
Inside a _variable_ as you originally stated:-

Last login: Mon Aug 26 09:37:04 on ttys000
AMIGA:barrywalker~> printf "abc.tpt.ctl\nbcd.tpt.ctl\ncdw.tpt.ctl\n" > /tmp/string.file
AMIGA:barrywalker~> text=$(cat < /tmp/string.file)
AMIGA:barrywalker~> printf "${text:0:3}\n"
abc
AMIGA:barrywalker~> printf "${text:12:3}\n"
bcd
AMIGA:barrywalker~> printf "${text:24:3}\n"
cdw
AMIGA:barrywalker~> _