-3

folks I have string

 str1="MVM GT RT BHHT SYSTEMG RW"

I need to get "BHHT" and add it to variable str2.

I shouldn't use file. I need something like this:

str1 | cut -d ' ' -f 3

Joe
  • 9

2 Answers2

1

That's very simple :

str2=$(echo $str1 | cut -d' ' -f4)
nullPointer
  • 4,419
  • 1
  • 15
  • 27
1

This can be done with variable expansion

# removes the first three fields ( # for the shortest prefix )
str2=${str1#* * * }

# removes all after next space ( %% for the largest suffix )
elem=${str2%% *}
Nahuel Fouilleul
  • 18,726
  • 2
  • 31
  • 36