awk : ORS not to be printed after the last record


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting awk : ORS not to be printed after the last record
# 1  
Old 03-16-2017
awk : ORS not to be printed after the last record

Hello Team,

here is the code:
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
# 2  
Old 03-16-2017
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.
# 3  
Old 03-16-2017
Thanks RudiC,

i have written something like below in sed & awk,
Code:
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 :
Code:
/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
Code:
/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:
Code:
scripts]# $var1
Server - cmserver is running (5330).

while just the copy paste of the $var content execution:
Code:
# /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 Smilie

Thanks,
Chandana

Last edited by chandana.hs; 03-16-2017 at 07:44 AM.. Reason: added sample output
# 4  
Old 03-16-2017
How about
Code:
ls /etc/init.d/*was.init | awk '{print TRS "${service_cmd} "$0 " status"; TRS = " && "}' ORS=""

# 5  
Old 03-17-2017
Quote:
Originally Posted by RudiC
How about
Code:
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,

Code:
[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,

Code:
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?

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

even below one fails to execute completely Smilie

Code:
[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

Last edited by chandana.hs; 03-17-2017 at 01:08 AM.. Reason: added one more code output
# 6  
Old 03-17-2017
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!
# 7  
Old 03-20-2017
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 Smilie

Code:
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

Last edited by Scrutinizer; 03-20-2017 at 05:55 AM.. Reason: Removed font definition within code tags
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Can someone please explain why we need to set ORS in below awk code?

Question: Write a command to print the fields in a text file in reverse order? awk 'BEGIN {ORS=""} { for(i=NF;i>0;i--) print $i," "; print "\n"}' filename I was thinking it should be (what is the need to set ORS="" ? )- awk 'BEGIN { for(i=NF;i>0;i--) print $i," "; print "\n"}' filename (3 Replies)
Discussion started by: Tanu
3 Replies

2. Shell Programming and Scripting

How to get row data printed in column using awk?

Hi team, I have below sample file. $ cat sample dn: MSISDN=400512345677,dc=msisdn,ou=NPSD,serv=CSPS,ou=servCommonData,dc=stc structuralObjectClass: NphData objectClass: NphData objectClass: MSISDN entryDS: 0 nodeId: 35 createTimestamp: 20170216121047Z modifyTimestamp: 20170216121047Z... (3 Replies)
Discussion started by: shanul karim
3 Replies

3. Shell Programming and Scripting

awk RS/ORS error

Hello, I am trying to filter fastq file (in short, every 4 lines to be a record) based on the GC counts (GC-contents) in sequence (i.e. field 2), which is the count % of the G/C chars in the string. The example script is to pick up records with GC contents > 0.6 in the sequence (second field). ... (9 Replies)
Discussion started by: yifangt
9 Replies

4. Shell Programming and Scripting

How to compare current record,with next and previous record in awk without using array?

Hi! all can any one tell me how to compare current record of column with next and previous record in awk without using array my case is like this input.txt 0 32 1 26 2 27 3 34 4 26 5 25 6 24 9 23 0 32 1 28 2 15 3 26 4 24 (7 Replies)
Discussion started by: Dona Clara
7 Replies

5. UNIX for Dummies Questions & Answers

awk: record too long

Hi All , I am getting record too long for the below command . nawk -F\" '{a=a" "$2} END{for(i in a) print i,a }' test|sort|awk '{for(i=1;i<=NF;i++) t=t"\t"$i;if(NF>max)max=NF} END{for(i=1;i<=max;i++)print t }' File test has 850 records ... Please help.. (2 Replies)
Discussion started by: saj
2 Replies

6. UNIX for Dummies Questions & Answers

Awk: print all URL addresses between iframe tags without repeating an already printed URL

Here is what I have so far: find . -name "*php*" -or -name "*htm*" | xargs grep -i iframe | awk -F'"' '/<iframe*/{gsub(/.\*iframe>/,"\"");print $2}' Here is an example content of a PHP or HTM(HTML) file: <iframe src="http://ADDRESS_1/?click=5BBB08\" width=1 height=1... (18 Replies)
Discussion started by: striker4o
18 Replies

7. Shell Programming and Scripting

Trouble getting the next to last record with awk

Hello all, I'm a beginner to shell/ awk script writing, and I'm trying to do something that looks like it shouldn't be (too) hard at all to do, but unfortunately, I can't seem to be able to find the right way to do it with awk. I need to look for the time several processes start & end in a... (5 Replies)
Discussion started by: Muadib
5 Replies

8. Shell Programming and Scripting

awk: record has too many fields

Hi, I'm trying this command - but get this error. Do you guys have any workaround for this? cat tf|sed 's/{//g'|sed 's/,//g'|awk '{for (i=1;i<=NF;i++) {if ($i == "OPTIME") {k = i + 2; print $i,$k}}}' awk: record `2005 Jul 28 17:35:29...' has too many fields record number 15 This is how... (3 Replies)
Discussion started by: chaandana
3 Replies

9. UNIX for Dummies Questions & Answers

Record too long for awk

I am trying to generate a small report with the help of awk. The contents are present in a file whose last line is very long. I can't shorten this line as its generated after a lot of processing. On reading this file awk says record "starting of line ..." too long record number 30 Now... (2 Replies)
Discussion started by: vibhor_agarwali
2 Replies

10. Shell Programming and Scripting

rs and ors in gawk ...????

:D dear members I have a good knowledge of gawk and seem to do quite well with it.. but I have never understood what the use of the rs and ors are for or how they are used.. i am thinking they are for seperating lines and paragraphs but i have absolutely no idea how to make it work, if that is what... (2 Replies)
Discussion started by: moxxx68
2 Replies
Login or Register to Ask a Question