Must break a string in bash after a certain number of characters

I am writing a bash script that will execute the command and store the value in a string variable, now I need to split the string after certain characters. Is there any way? I cannot use delimiters because the format is similar to this

  PV Name /dev/sda2
  PV Size 10.39 GB

Here I need to get / dev / sda 2 and 10.39 GB (if possible, only 10.39) and write it to a new file. I cannot use a separator because space is first. I have not done many bash scripts. Is there any way to do this?

+5
source share
3 answers
echo "${var:8}"

will display content $varstarting at character 8 (based on zero).

To remove everything from the first place:

data=${var:8}
echo "${data%% *}"
+6

, cut:

 $ echo '1234567' | cut -c2-5
 2345

awk :

$ echo '  PV Size 10.39 GB' | awk '{ print $3 }'
10.39

, /,

+3

You can use cut:

$ echo "PV Name /dev/sda2" |cut -d " " -f 3
/dev/sda2
+2
source

All Articles