Shell Script: Cut Part of a String

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

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

Elena Rostova
Author

Elena Rostova

Elena Rostova holds a Master's degree in Public Health Journalism. She covers groundbreaking medical research, holistic wellness trends, mental health awareness, and nutritional science.