print upto second last character in unix print upto second last character in unix unix unix

print upto second last character in unix


If you are using BASH then it is fairly straight forward to remove last character:

s="string1,string2,"echo "${s%?}"

? matches any single character and %? removes any character from right hand side.

That will output:

string1,string2

Otherwise you can use this sed to remove last character:

echo "$s" | sed 's/.$//'string1,string2


You can do it with bash "parameter substitution":

string=12345new=${string:0:$((${#string}-1))}echo $new1234

where I am saying:

new=${string:a:b}

where:

a=0 (meaning starting from the first character)

and:

b=${#string} i.e. the length of the string minus 1, performed in an arithmetic context, i.e. inside `$((...))`


str="something"echo $str | cut -c1-$((${#str}-1))

will give result as

somethin

If you have two different variables, then you can try this also.

str="something"strlen=9echo $str | cut -c1-$((strlen-1))

cut -c1-8 will print from first character to eighth.