0

I want to list all services from /etc/init.d/ and grep pattern and store them in array. for instance like below.

ls -la /etc/init.d/ | grep example-subs | awk '{ print $9 }'
example-subs-sidekick
example-subs-unicorn

and store the strings in array those are returned. but somehow it is not happening. I used below commands.

 gawk -F: '{ print $9 }' ls -la /etc/init.d/ | grep example-subs
 gawk -F: '{ print $9 }' "ls -la /etc/init.d/ | grep example-subs"
 gawk -F: '{ print $9 }' 'ls -la /etc/init.d/ | grep example-subs'
 gawk -F: '{ print $9 }' `ls -la /etc/init.d/ | grep example-subs`
 awk '{ print $9 ls -la /etc/init.d/ | grep example-subs }'
 awk '{ print $9 "ls -la /etc/init.d/ | grep example-subs" }'
 awk '{ print $9 `ls -la /etc/init.d/ | grep example-subs` }'
 awk '{ print $9 (ls -la /etc/init.d/ | grep example-subs) }'
 awk '{ print $9 "(ls -la /etc/init.d/ | grep example-subs)" }'

But nothing works. Can someone help here.

3
  • Your first command works, why aren't you using that command? – Barmar Feb 2 '17 at 22:47
  • first command isn't working for me. – Shailesh Sutar Feb 3 '17 at 14:20
  • I don't understand. Isn't that a transcript of the command? What's wrong with it? – Barmar Feb 3 '17 at 16:44
0

Use your first command as part of the assignment to the array:

array=($(ls -a /etc/init.d/ | grep example-subs))

You don't need to use awk. If you use ls instead of ls -l, the only thing it prints is the filename.

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.