[BASH] Possible to echo only certain range of WORDS in a string?

In summary, the conversation is about a person who is new to Bash scripting and needs help with echoing a specified range of words from a string based on index. They are trying to do this in one line without using a loop or array, and are looking for a shorter way to do it using something similar to {$var:1:3} for arrays. The conversation also mentions the use of the "cut" command, but the person is not allowed to use it. They are currently using a loop and an output string to achieve their goal, but are wondering if there is a more efficient way to do it.
  • #1
zeion
466
1
Hi,

I'm new to Bash scripting and I need some help with this:
I need to echo only a specified range of words from a string based on index;
so for example if I had:

a="hello how are you today"

Is there a way to echo only "how are you" by specifying the index 2 to 4?
(in one line, possible with one command and without a loop or array?)

Thanks.
 
Technology news on Phys.org
  • #2
Code:
echo "hello how are you today" | cut -d' ' -f2-4
 
  • #3
Actually I'm not allowed to use cut either haha, but thanks.
 
  • #4
then spell out the complete set of rules...better yet, show what you have tried...you have tried something...haven't you?
 
  • #5
Basically I need to imitate a version of cut without actually using cut.
Right now I'm passing each line read from file into an array, looping through each element (words, split based on IFS), and then updating an output string each time (I don't want it to print on every new line), then printing the combined output string for that line for each line in the file.

I was just wondering it there is a shorter way of doing this; ie. I know you can extract certain range of characters from a string with {$var:1:3} or something like that, was just wondering if there is one similar for arrays; I've looked around but couldn't find any.

Thanks.
 
  • #6
bash does have arrays, though, or lists, if you will
Code:
arr=(one two three)
echo ${arr[1]}
 

FAQ: [BASH] Possible to echo only certain range of WORDS in a string?

Can I use a specific range of words in a string when using the echo command in bash?

Yes, it is possible to use a specific range of words in a string when using the echo command in bash. This can be achieved by using the substring syntax, which allows you to specify the start and end index of the words you want to include.

How do I specify the range of words I want to include in a string with the echo command?

To specify the range of words you want to include in a string with the echo command, you can use the syntax ${string:start:end}, where "string" is the variable that contains the string, "start" is the index of the first word you want to include, and "end" is the index of the last word you want to include.

Can I use a negative index to specify the range of words in a string with the echo command?

Yes, you can use a negative index to specify the range of words in a string with the echo command. A negative index starts counting from the end of the string, with -1 representing the last character. For example, if you want to include the last three words of a string, you can use ${string:-3}.

Is it possible to include a range of words from a specific position until the end of the string with the echo command?

Yes, it is possible to include a range of words from a specific position until the end of the string with the echo command. This can be done by omitting the "end" index in the syntax ${string:start:end}, and only specifying the "start" index. This will include all words from the specified index until the end of the string.

Can I use variables in the substring syntax to specify the range of words in a string with the echo command?

Yes, you can use variables in the substring syntax to specify the range of words in a string with the echo command. This allows for more flexibility, as you can use the value of a variable to determine the start and end index of the words you want to include in the string.

Similar threads

Replies
10
Views
1K
Replies
1
Views
2K
Replies
13
Views
2K
Replies
2
Views
1K
Replies
1
Views
2K
Replies
15
Views
2K
Replies
10
Views
2K
Back
Top