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

Click For Summary

Discussion Overview

The discussion revolves around extracting a specific range of words from a string in Bash scripting, focusing on methods to achieve this without using certain commands like 'cut'. Participants explore alternatives and seek more efficient ways to handle word extraction based on index.

Discussion Character

  • Exploratory
  • Technical explanation
  • Debate/contested

Main Points Raised

  • One participant asks how to echo a specified range of words from a string using a single command without loops or arrays.
  • Another participant suggests using 'cut' to achieve the desired output.
  • A participant clarifies that they cannot use 'cut' and requests alternative methods.
  • One participant encourages providing more details about the rules or previous attempts to assist better.
  • A participant describes their current method of using arrays and looping through elements to construct the output string, expressing a desire for a more concise solution.
  • Another participant notes that Bash supports arrays and provides an example of accessing an array element.

Areas of Agreement / Disagreement

Participants do not reach a consensus on a specific solution, and multiple competing views and methods are presented without resolution.

Contextual Notes

Participants mention limitations regarding the use of certain commands and the need for a solution that avoids loops or arrays, indicating a specific context for their requests.

Who May Find This Useful

Individuals interested in Bash scripting, particularly those looking for efficient string manipulation techniques without relying on certain built-in commands.

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]}
 

Similar threads

  • · Replies 10 ·
Replies
10
Views
2K
  • · Replies 1 ·
Replies
1
Views
5K
Replies
5
Views
2K
  • · Replies 13 ·
Replies
13
Views
2K
Replies
55
Views
7K
  • · Replies 2 ·
Replies
2
Views
2K
Replies
6
Views
3K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 15 ·
Replies
15
Views
2K
  • · Replies 10 ·
Replies
10
Views
4K