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

AI Thread Summary
The discussion centers on a user's request for help with Bash scripting, specifically on how to echo a specified range of words from a string based on index without using the `cut` command or loops. The user provides an example string, "hello how are you today," and seeks to extract "how are you" by specifying the index range of 2 to 4. While a suggestion using `cut` is offered, the user clarifies that they cannot use it. They describe their current method of processing lines from a file by passing each line into an array and looping through the elements to build an output string. The user is looking for a more concise method, similar to character extraction using `${var:1:3}`, but for arrays. The discussion highlights the need for efficient string manipulation techniques in Bash scripting without relying on certain commands or structures.
zeion
Messages
455
Reaction score
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
Code:
echo "hello how are you today" | cut -d' ' -f2-4
 
Actually I'm not allowed to use cut either haha, but thanks.
 
then spell out the complete set of rules...better yet, show what you have tried...you have tried something...haven't you?
 
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.
 
bash does have arrays, though, or lists, if you will
Code:
arr=(one two three)
echo ${arr[1]}
 
Thread 'Star maps using Blender'
Blender just recently dropped a new version, 4.5(with 5.0 on the horizon), and within it was a new feature for which I immediately thought of a use for. The new feature was a .csv importer for Geometry nodes. Geometry nodes are a method of modelling that uses a node tree to create 3D models which offers more flexibility than straight modeling does. The .csv importer node allows you to bring in a .csv file and use the data in it to control aspects of your model. So for example, if you...
I tried a web search "the loss of programming ", and found an article saying that all aspects of writing, developing, and testing software programs will one day all be handled through artificial intelligence. One must wonder then, who is responsible. WHO is responsible for any problems, bugs, deficiencies, or whatever malfunctions which the programs make their users endure? Things may work wrong however the "wrong" happens. AI needs to fix the problems for the users. Any way to...

Similar threads

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