LinuxQuestions.org
Visit Jeremy's Blog.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 06-29-2007, 07:22 AM   #1
mystical dervish
LQ Newbie
 
Registered: May 2007
Posts: 25

Rep: Reputation: 15
substring in shell


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
can someone help
thanks
 
Old 06-29-2007, 08:08 AM   #2
Hobbletoe
Member
 
Registered: Sep 2004
Location: Dayton, Oh
Distribution: Linux Mint 17
Posts: 150

Rep: Reputation: 18
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

Code:
line=${line:2}
 
Old 06-30-2007, 08:40 PM   #3
cfaj
Member
 
Registered: Dec 2003
Location: Toronto, Canada
Distribution: Mint, Mandriva
Posts: 221

Rep: Reputation: 31
Quote:
Originally Posted by mystical dervish
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

 
Old 07-03-2007, 05:09 AM   #4
mystical dervish
LQ Newbie
 
Registered: May 2007
Posts: 25

Original Poster
Rep: Reputation: 15
Quote:
Originally Posted by cfaj

There are several mistakes in that code. This does what you want using
only POSIX syntax:

Code:
   ## if [ $len -gt 3 ] && [ $len -lt 5 ]  ##  Why?


I wanted 3 and 5 to be changable through tow variables

thanks for your reply

Last edited by mystical dervish; 07-03-2007 at 07:21 AM.
 
Old 07-09-2007, 07:24 AM   #5
mystical dervish
LQ Newbie
 
Registered: May 2007
Posts: 25

Original Poster
Rep: Reputation: 15
is there any problem in my code here
Code:
if [ grep -w "$line" /lexic.wlst] | [ grep -w "$line" /wordofcorpus/wrd.txt]; then
because I execute my script , it results out a problem refering to that line saying " [: missing `]'"
 
Old 07-09-2007, 08:53 AM   #6
cfaj
Member
 
Registered: Dec 2003
Location: Toronto, Canada
Distribution: Mint, Mandriva
Posts: 221

Rep: Reputation: 31
Quote:
Originally Posted by mystical dervish
is there any problem in my code here
Code:
if [ grep -w "$line" /lexic.wlst] | [ grep -w "$line" /wordofcorpus/wrd.txt]; then
because I execute my script , it results out a problem refering to that line saying " [: missing `]'"

You need spaces before the closing brackets:

Code:
if [ grep -w "$line" /lexic.wlst ] |
 [ grep -w "$line" /wordofcorpus/wrd.txt ]; then
 
Old 07-09-2007, 09:40 AM   #7
mystical dervish
LQ Newbie
 
Registered: May 2007
Posts: 25

Original Poster
Rep: Reputation: 15
Unhappy

Quote:
Originally Posted by cfaj

You need spaces before the closing brackets:

Code:
if [ grep -w "$line" /lexic.wlst ] |
 [ grep -w "$line" /wordofcorpus/wrd.txt ]; then
I did so,but it gives another error,
Code:
if [ grep -w "$nline" /lexic.wlst ] | [ grep -w "$nline" /wordofcorpus/wrd.txt ]; then
[: too many arguments
 
Old 07-09-2007, 09:50 AM   #8
cfaj
Member
 
Registered: Dec 2003
Location: Toronto, Canada
Distribution: Mint, Mandriva
Posts: 221

Rep: Reputation: 31
Quote:
Originally Posted by mystical dervish
I did so,but it gives another error,
Code:
if [ grep -w "$nline" /lexic.wlst ] | [ grep -w "$nline" /wordofcorpus/wrd.txt ]; then
[: too many arguments

Sorry; I shouldn't post before I have by first shot of caffeine.

That should be:

Code:
if grep -w "$nline" /lexic.wlst |
 grep -w "$nline" /wordofcorpus/wrd.txt ; then
But that doesn't make sense; you are giving the second grep two inputs, stdin and a file. Do one or the other.

Perhaps you mean:

Code:
if grep -w "$nline" /lexic.wlst ||
 grep -w "$nline" /wordofcorpus/wrd.txt ; then
Or (better):

Code:
if grep -w "$nline" /lexic.wlst /wordofcorpus/wrd.txt ; then
 
Old 07-09-2007, 11:37 AM   #9
mystical dervish
LQ Newbie
 
Registered: May 2007
Posts: 25

Original Poster
Rep: Reputation: 15
Quote:
Originally Posted by cfaj

Sorry; I shouldn't post before I have by first shot of caffeine.

That should be:

Code:
if grep -w "$nline" /lexic.wlst |
 grep -w "$nline" /wordofcorpus/wrd.txt ; then
But that doesn't make sense; you are giving the second grep two inputs, stdin and a file. Do one or the other.

Perhaps you mean:

Code:
if grep -w "$nline" /lexic.wlst ||
 grep -w "$nline" /wordofcorpus/wrd.txt ; then
Or (better):

Code:
if grep -w "$nline" /lexic.wlst /wordofcorpus/wrd.txt ; then
thanx a lot and merci bien
 
Old 07-09-2007, 08:22 PM   #10
ghostdog74
Senior Member
 
Registered: Aug 2006
Posts: 2,697
Blog Entries: 5

Rep: Reputation: 244Reputation: 244Reputation: 244
Quote:
Originally Posted by mystical dervish
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
can someone help
thanks
in awk:
Code:
awk 'length > 3 && length<5{
       print substr($0,3)
}' "file"
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Shell Scripting: Getting a pid and killing it via a shell script topcat Programming 15 10-28-2007 02:14 AM
LXer: Shell tip: Set the shell prompt and themes in Linux Terminal LXer Syndicated Linux News 0 06-12-2007 03:02 AM
I made a shortcut to a shell script and it is using default shell icon... shlinux Linux - Software 2 04-20-2006 06:29 AM
Alias or shell script to confirm 'exit' commands from a shell rose_bud4201 Programming 2 03-08-2006 02:34 PM
'sh' shell - Actually calls legacy Bourne shell, or uses system default? Dtsazza Linux - Software 1 10-28-2005 09:20 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 12:25 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration