ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
hello
I'm trying to examin each word fom list in text file (one word per line)
if the condition is true it has to remove the fisrt two chars from accepted word
the problem is in // line = ${line:2:$len-1} which it had to get substring from the word
Code:
while read line; do
len=${#line}
if $len > 3 && $len < 5; then
echo $line
line = ${line:2:$len-1}
echo $line;
done < wrdstxt
What you want is the following. Note also that I took out the spaces around the '='. Bash doesn't like those. Since you want to do some math, you need to use the $(( )) construct in bash.
Code:
line=${line:2:$((${len}-1))}
Note that you could forgo the $len variable entirely.
Code:
line=${line:2:$((${#line}-1))}
Actually, since it looks like you want from the second character to the end of the line, you could just use
hello
I'm trying to examin each word fom list in text file (one word per line)
if the condition is true it has to remove the fisrt two chars from accepted word
the problem is in // line = ${line:2:$len-1} which it had to get substring from the word
Code:
while read line; do
len=${#line}
if $len > 3 && $len < 5; then
echo $line
line = ${line:2:$len-1}
echo $line;
done < wrdstxt
There are several mistakes in that code. This does what you want using
only POSIX syntax:
Code:
while read line
do
len=${#line}
## if [ $len -gt 3 ] && [ $len -lt 5 ] ## Why?
if [ $len -eq 4 ]
then
printf "%s\n" "$line"
line=${line#??}
printf "%s\n" "$line"
fi
done < wrdstxt
Or:
Code:
while read line
do
len=${#line}
case $len in
4) printf "%s\n" "$line"
line=${line#??}
printf "%s\n" "$line"
;;
esac
done < wrdstxt
hello
I'm trying to examin each word fom list in text file (one word per line)
if the condition is true it has to remove the fisrt two chars from accepted word
the problem is in // line = ${line:2:$len-1} which it had to get substring from the word
Code:
while read line; do
len=${#line}
if $len > 3 && $len < 5; then
echo $line
line = ${line:2:$len-1}
echo $line;
done < wrdstxt
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.