awk : ORS not to be printed after the last record

Hello Team,

here is the code:

scripts]# ls /etc/init.d/ | awk 'BEGIN{ORS=" && "} /was.init/ && !/interdependentwas/ && !/NodeAgent/ && !/dmgr/{print "\$\{service_cmd\} "$0 " status"}' 2>/dev/null
${service_cmd} cmserver_was.init status && ${service_cmd} fmserver_was.init status && ${service_cmd} ihs_was.init status && ${service_cmd} intgserver_was.init status && ${service_cmd} itsmserver_was.init status && ${service_cmd} server2_was.init status && ${service_cmd} sysserver_was.init status && 

in the above output i dont want the ORS(&&) to be printed after the last record, Can you help with this awk?

Thanks,
Chandana

While easy in sed (where the last line is known and can be addressed with "$") this is more difficult in awk . One way would be to print the ORS before the output string except for the first line.

Thanks RudiC,

i have written something like below in sed & awk,

var1=$(ls /etc/init.d/*was.init | sed '/interdependentwas/d;/NodeAgent/d;/dmgr/d;s#/etc/init.d/##g;s#.*#/sbin/service & status#g' | awk 'BEGIN {ORS=" && "} y{print s}{s=$0;y=1}END{ORS="";print s}')
echo $var1

Output is :

/sbin/service cmserver_was.init status && /sbin/service fmserver_was.init status && /sbin/service ihs_was.init status && /sbin/service intgserver_was.init status && /sbin/service itsmserver_was.init status && /sbin/service server2_was.init status && /sbin/service sysserver_was.init status

execution of the above $var1 executes only

/sbin/service cmserver_was.init status

, but not all the services status check with logical AND, can you suggest whats going wrong!

$var1 execution output:

scripts]# $var1
Server - cmserver is running (5330).

while just the copy paste of the $var content execution:

# /sbin/service cmserver_was.init status && /sbin/service fmserver_was.init status && /sbin/service ihs_was.init status && /sbin/service intgserver_was.init status && /sbin/service itsmserver_was.init status && /sbin/service server2_was.init status && /sbin/service sysserver_was.init status
Server - cmserver is running (5330).
Server - fmserver is running (6977).
Server is running.
Server - intgserver is running (15401).
Server - itsmserver is running (8771).
Server - server2 is running (7679).
Server - sysserver is running (2698).

simpler code is also welcome :smiley:

Thanks,
Chandana

How about

ls /etc/init.d/*was.init | awk '{print TRS "${service_cmd} "$0 " status"; TRS = " && "}' ORS=""

---------- Post updated 03-17-17 at 09:11 AM ---------- Previous update was 03-16-17 at 04:59 PM ----------

Thanks RudiC.

used the suggested and it works fine, But the execution of the variable fails like below,

[node14 init.d]# export service_cmd=/sbin/service
[node14 init.d]# var1=`cd /etc/init.d/; ls *was.init | awk '!/dmgr/ && !/NodeAgent/ && !/interdependentwas/ {print TRS "${service_cmd} "$0 " status"; TRS = " && "}' ORS=""`
[node14 init.d]# echo $var1
${service_cmd} cmserver_was.init status && ${service_cmd} fmserver_was.init status && ${service_cmd} ihs_was.init status && ${service_cmd} intgserver_was.init status && ${service_cmd} itsmserver_was.init status && ${service_cmd} server2_was.init status && ${service_cmd} sysserver_was.init status
[node14 init.d]# $var1
-bash: ${service_cmd}: command not found
[node14 init.d]#

but the copy paste of the command gives the proper output,

node14 ~]# ${service_cmd} cmserver_was.init status && ${service_cmd} fmserver_was.init status && ${service_cmd} ihs_was.init status && ${service_cmd} intgserver_was.init status && ${service_cmd} itsmserver_was.init status && ${service_cmd} server2_was.init status && ${service_cmd} sysserver_was.init status
Server - cmserver is running (5330).
Server - fmserver is running (6977).
HTTPServer is running.
Server - intgserver is running (15401).
Server - itsmserver is running (8771).
Server - server2 is running (7679).
Server - sysserver is running (2698).

Can yu pls suggest and guide whats going wrong, and how to achieve this?

[node14 ~]# cat /etc/redhat-release
Red Hat Enterprise Linux Server release 6.8 (Santiago)

even below one fails to execute completely :frowning:

[node14 ~]# var1=$(cd /etc/init.d/; ls *was.init | awk 'BEGIN{service_cmd="/sbin/service"} !/dmgr/ && !/NodeAgent/ && !/interdependentwas/ {print TRS service_cmd" "$0 " status"; TRS = " && "}' ORS="")
[node14 ~]# echo $var1
/sbin/service cmserver_was.init status && /sbin/service fmserver_was.init status && /sbin/service ihs_was.init status && /sbin/service intgserver_was.init status && /sbin/service itsmserver_was.init status && /sbin/service server2_was.init status && /sbin/service sysserver_was.init status
[node14 ~]# $(echo $var1)
Server - cmserver is running (5330).
[node14 ~]#

Thanks,
Chandana

That's because ${service_cmd} has to be expanded by the shell. Which is no problem when entering that line at the command prompt. When contained in a variable like var , expansion would need to occur twice... That what the (dangerous and deprecated) eval shell command is for - be aware that you need to know exactly what you are doing and what the contents of the to-be-interpreted string (here: var's contents) is. Any malicious command will be executed without further notice / warning.

Did you try to pipe above awk 's output through another shell like ls ... | awk ... | sh ? Here as well you need to know exactly WHAT will be interpreted!

Thanks RudiC.

but even after the service_cmd value is substituted, execution fails.

Not getting much, went for simpler for loop. But will see if i get something :frowning:

for was_service in `ls /etc/init.d | awk '/was.init/ && !/dmgr/ && !/interdependentwas/'`; do $service_cmd $was_service status; service_status=$(($service_status + `echo $?`));done

Thanks,
Chandana