-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,188
  • 1
  • 14
  • 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
  • 17,834
  • 1
  • 28
  • 34