Home > front end >  egrep returning matches separated by string
egrep returning matches separated by string

Time:09-21

I'm trying to extract specific lines from the output of a command that outputs a lot of lines of text. I've hit a bit of a wall at this point:

pathsText=$(system_profiler SPApplicationsDataType | egrep '^\s Location:\s .*$')

This extracts the correct lines, but it puts them all into a single long line, with matches separated by spaces rather than newlines.

In other words, I'm expecting something like this:

  Location: /a/b
  Location: /c/d/e
  ...

And that's what I get if I run that same egrep command against a text file containing the output of system_profiler SPApplicationsDataType in the Terminal. But the command above does not put that into pathsText. Instead, it's giving me:

Location: /a/b Location: /c/d/e ...

Unfortunately, I can't just split based on spaces, because the paths contained in each line may contain spaces.

What am I doing wrong?

CodePudding user response:

Still not reproducable:

luuk@mini ~ % pathsText=$(system_profiler SPApplicationsDataType | egrep '^\s Location:\s .*$')
luuk@mini ~ % echo $pathsText 
      Location: /System/Applications/System Preferences.app
      Location: /System/Applications/Utilities/Terminal.app
      Location: /System/Applications/TV.app
      Location: /System/Applications/FaceTime.app
      Location: /System/Applications/TextEdit.app
      Location: /System/Applications/Time Machine.app
      Location: /System/Applications/App Store.app
      Location: /System/Library/ColorSync/Calibrators/Display Calibrator.app
      Location: /System/Library/CoreServices/P.....

EDIT:

The answer is here:

https://www.google.com/search?q=macos assign variable looses carriage return of line feed

finding a link on stackoverflow.com to:

How to preserve line breaks when storing command output to a variable?

  • Related