Print strings in bash in loop

Hello All,

I am a newbie and have been struggling to print in loop.

I have one line hold in a variable and need to print them with first six strings.

We have get the value with below macOS terminal command.

osascript -e 'tell application "System Events" to get properties of every login item'

need to print them with new line like below:

I know i did wrong but i use simple logic to print them with below.

Did you try something simple like?

sed 's/, name/\nname/g' yourfile.txt
ubuntu$ sed 's/, name/\nname/g' yourfile.txt
name:Pages, path:/Applications/Pages.app, class:login item, kind:Application, hidden:false
name:Calculator, path:/System/Applications/Calculator.app, class:login item, kind:Application, hidden:true
name:Calendar, path:/System/Applications/Calendar.app, class:login item, kind:Application, hidden:false
ubuntu$  cat yourfile.txt
name:Pages, path:/Applications/Pages.app, class:login item, kind:Application, hidden:false, name:Calculator, path:/System/Applications/Calculator.app, class:login item, kind:Application, hidden:true, name:Calendar, path:/System/Applications/Calendar.app, class:login item, kind:Application, hidden:false

Thank you @Neo for your quick response. But that didn't work as expected.

I tried with two ways with your advise:

Is it due to the variable holds as one word?

Deleted post because my comment was accidentally on the output and not the input.

See my post below specific to macos and using perl instead of sed or using gsed on macos .

Earlier sed code works on linux but not macos .

Apologies. I tried again. I am confused here. I did exactly same as you advised. They printed in other way as highlighted below.
The strings changed to smiley when I paste it from my terminal and had to edit. The code still remain same as in my initial post.

user$ cat yourfile.txt
name:Pages, path:/Applications/Pages.app, class:login item, kind:Application, hidden:false, name:Calculator, path:/System/Applications/Calculator.app, class:login item, kind:Application, hidden:true, name:Calendar, path:/System/Applications/Calendar.app, class:login item, kind:Application, hidden:false

user$ sed 's/, name/\nname/g' yourfile.txt
name:Pages, path:/Applications/Pages.app, class:login item, kind:Application, hidden:falsenname:Calculator, path:/System/Applications/Calculator.app, class:login item, kind:Application, hidden:truenname:Calendar, path:/System/Applications/Calendar.app, class:login item, kind:Application, hidden:false

What's your shell and sed versions? Either of them may not like the \n notation and interprets it as a plain n... as you can see in falsenname Try a verbatim new line char:

sed 's/, name/\
name/g' file
name:Pages, path:/Applications/Pages.app, class:login item, kind:Application, hidden:false
name:Calculator, path:/System/Applications/Calculator.app, class:login item, kind:Application, hidden:true
name:Calendar, path:/System/Applications/Calendar.app, class:login item, kind:Application, hidden:false

Hi,

@rudic, you might be correct something must be with shell version. Below is the output that might help to provide additional advice. I was not able to find sed version on my macOS 10.13.6 neither verbatim new line

@neo, may be we are not on same page, the code you have quoted is my output actually.

tests-Mac:Desktop test$ $SHELL --version
GNU bash, version 3.2.57(1)-release (x86_64-apple-darwin17)
Copyright (C) 2007 Free Software Foundation, Inc.


tests-Mac:Desktop test$ uname -v
Darwin Kernel Version 17.7.0: Sun Jun  2 20:31:42 PDT 2019; root:xnu-4570.71.46~1/RELEASE_X86_64


tests-Mac:Desktop test$ sw_vers -productVersion
10.13.6


tests-Mac:Desktop test$ sed --version
sed: illegal option -- -
usage: sed script [-Ealn] [-i extension] [file ...]
       sed [-Ealn] [-i extension] [-e script] ... [-f script_file] ... [file ...]


tests-Mac:Desktop test$ osascript -e 'tell application "System Events" to get properties of every login item' | sed 's/, name/\ name/g'
name:Mail, path:/Applications/Mail.app, class:login item, kind:Application, hidden:false name:DVD Player, path:/Applications/DVD Player.app, class:login item, kind:Application, hidden:true name:Calculator, path:/Applications/Calculator.app, class:login item, kind:Application, hidden:false name:Calendar, path:/Applications/Calendar.app, class:login item, kind:Application, hidden:false name:App Store, path:/Applications/App Store.app, class:login item, kind:Application, hidden:false


tests-Mac:Desktop test$ sed 's/, name/\nname/g' yourfile.txt
name:Pages, path:/Applications/Pages.app, class:login item, kind:Application, hidden:falsenname:Calculator, path:/System/Applications/Calculator.app, class:login item, kind:Application, hidden:truenname:Calendar, path:/System/Applications/Calendar.app, class:login item, kind:Application, hidden:false


tests-Mac:Desktop test$ osascript -e 'tell application "System Events" to get properties of every login item' | sed 's/, name/\nname/g'
name:Mail, path:/Applications/Mail.app, class:login item, kind:Application, hidden:falsenname:DVD Player, path:/Applications/DVD Player.app, class:login item, kind:Application, hidden:truenname:Calculator, path:/Applications/Calculator.app, class:login item, kind:Application, hidden:falsenname:Calendar, path:/Applications/Calendar.app, class:login item, kind:Application, hidden:falsenname:App Store, path:/Applications/App Store.app, class:login item, kind:Application, hidden:false

Yes, I see that now.... sorry busy working on apache2 error.... missed that (your output versus input)... my bad.

Will test something on my mac and post back.

I think on macos , perl is a better choice (for this REGEX):

macos$ perl -p -e 's/name:/\nname:/g' test.txt > a.txt
macos$ cat test.txt a.txt
name:Pages, path:/Applications/Pages.app, class:login item, kind:Application, hidden:false, name:Calculator, path:/System/Applications/Calculator.app, class:login item, kind:Application, hidden:true, name:Calendar, path:/System/Applications/Calendar.app, class:login item, kind:Application, hidden:false

name:Pages, path:/Applications/Pages.app, class:login item, kind:Application, hidden:false, 
name:Calculator, path:/System/Applications/Calculator.app, class:login item, kind:Application, hidden:true, 
name:Calendar, path:/System/Applications/Calendar.app, class:login item, kind:Application, hidden:false
1 Like

Also, FYI.. on macOS, you can also install GNU sed and it works as expected:

brew install gnu-sed

macos$ gsed  's/name:/\nname:/g' test.txt > a.txt
macos$ cat test.txt a.txt
name:Pages, path:/Applications/Pages.app, class:login item, kind:Application, hidden:false, name:Calculator, path:/System/Applications/Calculator.app, class:login item, kind:Application, hidden:true, name:Calendar, path:/System/Applications/Calendar.app, class:login item, kind:Application, hidden:false

name:Pages, path:/Applications/Pages.app, class:login item, kind:Application, hidden:false, 
name:Calculator, path:/System/Applications/Calculator.app, class:login item, kind:Application, hidden:true, 
name:Calendar, path:/System/Applications/Calendar.app, class:login item, kind:Application, hidden:false
1 Like

Hi @Neo,

Thank you very much. Yes, perl did the job. I will find something with sed or awk and avoiding installing gsed.

tests-Mac:~ test$ osascript -e 'tell application "System Events" to get properties of every login item'| perl -p -e 's/, name:/\nname:/g'
name:Dictionary, path:/Applications/Dictionary.app, class:login item, kind:Application, hidden:false
name:CCleaner, path:/Applications/CCleaner.app, class:login item, kind:Application, hidden:false
name:Calendar, path:/Applications/Calendar.app, class:login item, kind:Application, hidden:false
name:Dashboard, path:/Applications/Dashboard.app, class:login item, kind:Application, hidden:false
tests-Mac:~ test$ 

Thank you very much.

Welcome.

I think you will have some trouble with sed on the mac. I checked the web, and this newline issue on the macos version of sed is very frustrating for many people . The best idea for sed (on macos ) is to install gsed .

However, perl works well, as you discovered.

I'm sure one of our many very talented and world exceptional awk experts has a great awk idea for you. I never really enjoy using awk , my bad, and generally process text with PHP or Python , only because I prefer more self-descriptive syntax for future maintenance and code changes years later (but that's just me... YMMV).

1 Like

See the post #6.
The two lines approach works with any sed and any standard shell.

That works from the shell command line for sure.

But, I don't think the original poster wants to necessarily force a line break like that into his sequence of commands, when their are clearly alternatives:

For example, using perl:

 osascript -e 'tell application "System Events" to get properties of every login item'| perl -p -e 's/, name:/\nname:/g'

so it becomes:

 osascript -e 'tell application "System Events" to get properties of every login item'| sed 's/, name/\
name/g' 

It's a matter of personal preference (goes without saying) and in this case, what is the preference of the original poster, 7adi, on macOS ?

I know what my personal preference is (sed on Linux, gsed or perl on macOS ). But for the original poster, I have no idea what is their preference.

1 Like

Hi @MadeInGermany,

Thank you.

My preference is awk as my whole (learning) script is based on it.

Since I find perl is doing better job and clean as well, I would start digging it and learn more.

Yes, your two options worked well.

#alwayslearning

--- Post updated at 11:04 AM ---

Hi All,

I found how to break line with sed in macOS bash.

This doesn't require to change line.

osascript -e 'tell application "System Events" to get properties of every login item'| sed 's/, name:/\'$'\nname/g'
1 Like

Good for you and happy learning!

BTW, I tried that solution you just posted and had trouble when testing; but I was in a rush coding other projects so I guess I missed it somehow, maybe misplaced the single quotes and dollar sign!

Good for you! Well done!!

How about this awk approach?

awk '{gsub (/, name/, ORS "name")} 1' file
1 Like

This does not work as you need. You forgot to add the colon (:slight_smile: back in the REGEX:

sed 's/, name:/\'$'\nname:/g'

So,

macos$ sed 's/, name:/\'$'\nname:/g' test.txt > a.txt

macos$ cat test.txt
name:Pages, path:/Applications/Pages.app, class:login item, kind:Application, hidden:false, name:Calculator, path:/System/Applications/Calculator.app, class:login item, kind:Application, hidden:true, name:Calendar, path:/System/Applications/Calendar.app, class:login item, kind:Application, hidden:false

macos$ cat a.txt
name:Pages, path:/Applications/Pages.app, class:login item, kind:Application, hidden:false
name:Calculator, path:/System/Applications/Calculator.app, class:login item, kind:Application, hidden:true
name:Calendar, path:/System/Applications/Calendar.app, class:login item, kind:Application, hidden:false
1 Like

Hi RudiC,

Yes, it works. Don't know why I want loop, when there is so many other ways.

@Neo, yes semicolon was missed. Thank you for noticing it.

I will post new topic soon that I am struggling with.

Happy Holidays!!