shell script: cut part of a string

I have following string

11-22-33-44-55-66-77-88-99-1010-1111-1212

I want to extract 88-99-1010-111 from that string

1

2 Answers

Use cut:

Based on delimiter and fields:

echo 11-22-33-44-55-66-77-88-99-1010-1111-1212 | cut -d- -f 8-11

Based on character position:

echo 11-22-33-44-55-66-77-88-99-1010-1111-1212 | cut -c 22-36

Or use bash parameter substition:

var=11-22-33-44-55-66-77-88-99-1010-1111-1212
echo ${var:21:15}

With awk:

echo 11-22-33-44-55-66-77-88-99-1010-1111-1212 | awk 'BEGIN {FS=OFS="-"} {print $8,$9,$10,$11}'
  • BEGIN {FS=OFS="-"} is used to set both the field separator (FS) and the output field separator (OFS) to -.

Your Answer

Sign up or log in

Sign up using Google Sign up using Facebook Sign up using Email and Password

Post as a guest

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

You Might Also Like