Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to use cut a string including whitespace

I’m working on a little Bash script where I need to cut a line into several variables.

Here’s an example of the line to cut:

00577APP01     REC1_005 R_ESP01_R_TPP2SJAPPCB1

I’ve written this for now:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

CDSOC=$(echo "${line}" | cut -c1-5)
CDAPP=$(echo "${line}" | cut -c6-15)
IDEXT=$(echo "${line}" | cut -c16-)

However, as you can see, the IDEXT in our example is:

REC1_005 R_ESP01_R_TPP2SJAPPCB1

I’d like to get it back in one piece, but the cut stops at the white space and doesn’t continue.

What can I do so that it takes it all?

>Solution :

You’ll probably need to replace multiple spaces with a single space using Bash before. In order to translate or delete characters you could use tr –squeeze-repeats

"replace each sequence of a repeated character that is listed in the last specified ARRAY, with a single occurrence of that character"

tr -s ' '

Then, further processing can be done on --fieds | -f

"select only these fields; also print any line that contains no delimiter character, unless the -s option is specified"

echo "00577APP01     REC1_005 R_ESP01_R_TPP2SJAPPCB1" | tr -s ' ' | cut -d ' ' -f 2-
REC1_005 R_ESP01_R_TPP2SJAPPCB1
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading