Print awk output in same line ,For loop


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Print awk output in same line ,For loop
# 1  
Old 02-10-2016
Print awk output in same line ,For loop

My code is something like below.
Code:
#/bin/bash
for i in `ps -ef | grep pmon | grep -v bash | grep -v grep | grep -v perl | grep -v asm | grep -v MGMT|awk '{print $1" "$8}'`
do
echo $i
ORACLE_SID=`echo $line | awk '{print $2}'`
USERNAME=`echo $line | awk '{print $1}'`
done

=============
But echo $i output comes like
Code:
oradummydb01
ora_pmon_dummydb011
oraxyzdb01
ora_pmon_xyzdb011

is there a way I can get output of awk from for loop in straight line. so I can export ORACLE_SID and Username without redirect to file of executing:
Code:
ORACLE_SID=`ps -ef | grep pmon | awk $8`

Thanks for Help in advance.
Moderator's Comments:
Mod Comment Refusing to properly format sample input, sample output, and code segments after five warnings has resulted in this account being made read-only for three days.

Continued refusal to properly format posts may result in being banned from ever posting again.

Last edited by Don Cragun; 02-10-2016 at 10:54 PM.. Reason: Add CODE and ICODE tags.
# 2  
Old 02-10-2016
You could replace:
Code:
echo $i
ORACLE_SID=`echo $line | awk '{print $2}'`
USERNAME=`echo $line | awk '{print $1}'`

By either one of the following..

Single line (per 'task'):
Code:
echo "$i $line" | awk '{print $1,$2,$3}'

Or with linebreaks:
Code:
echo "$i $line" | awk -v NL="\n" '{print $1 NL $2 NL $3}'

hth
# 3  
Old 02-10-2016
Untested, but might come close:
Code:
#/bin/bash
ps -ef | awk '/pmon/ && ! ( /bash/ || /grep/ || /perl/ || /asm/ || /MGMT/) {print $1,$8}' |
while read USERNAME ORACLE_SID
do	printf 'USERNAME="%s", ORACLE_SID="%s"\n' "$USERNAME" "$ORACLE_SID"
	# Do whatever else you want to do with these two variables.
done

However, if the script you showed us produces the output you showed us, it means that $1 or $8 in the script expands to an empty field (and I would assume that it would be the 8th field that is empty.

The code above assumes that you know that the 1st and 8th fields contain the data you want and that the code you showed us is not exactly what you ran. If that assumption is not correct, we need to see the output produces by ps -ef and we need to know what output you are hoping to extract as a result of running your script. Since line is never set in the script you showed us, the USERNAME and ORACLE_SID shell variables script are probably being set to empty strings (unless line is set to some non-empty string in the environment inherited by your script).

The behavior of awk can also vary from system to system. Please also tell us what operating system you're using so we can better understand what you're trying to do.
# 4  
Old 02-11-2016
With all those exclusions, is "pmon" the right thing to match? And, as the first field in ps's IS the (already known and matched for?) username, do you really need to obtain it from your script?
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Print loop output on same line dynamically

Hi, I am trying to print copy percentage completion dynamically by using the script below, #!/bin/bash dest_size=0 orig_size=`du -sk $sourcefile | awk '{print $1}'` while ; do dest_size=`du -sk $destfile | awk '{print $1}'` coyp_percentage=`echo "scale=2; $dest_size*100/$orig_size"... (4 Replies)
Discussion started by: Sumanthsv
4 Replies

2. UNIX for Beginners Questions & Answers

Output to file print as single line, not separate line

example of problem: when I echo "$e" >> /home/cogiz/file.txt result prints to file as:AA BB CC I need it to save to file as this:AA BB CC I know it's probably something really simple but any help would be greatly appreciated. Thank You. Cogiz (7 Replies)
Discussion started by: cogiz
7 Replies

3. Shell Programming and Scripting

Grep echo awk print all output on one line

Hello, I've been trying to find the answer to this with Google and trying to browse the forums, but I haven't been able to come up with anything. If this has already been answered, please link me to the thread as I can't find it. I've been asked to write a script that pulls a list of our CPE... (51 Replies)
Discussion started by: rwalker
51 Replies

4. Shell Programming and Scripting

(n)awk: print regex search output lines in one line

Hello. I have been looking high and low for the solution for this. I seems there should be a simple answer, but alas. I have a big xml file, and I need to extract certain information from specific items. The information I need can be found between a specific set of tags. let's call them... (2 Replies)
Discussion started by: Tobias-Reiper
2 Replies

5. UNIX for Dummies Questions & Answers

Print each output of loop in new column using awk or shell

I have this output from a loop a11 1,2 3,4 5,6 7,8 12,8 5,4 3,6 a12 10,11 12,13 15,18 20,22 a13 ... (3 Replies)
Discussion started by: maryre89
3 Replies

6. Shell Programming and Scripting

Print for loop variable in output too

Hi, This is my input file cat input chr1:100-200 chr1:220-300 chr1:300-400 Now, I would like to run a program that will take each of the input record for i in `cat input`; do program $i | wc -l;done the output will be something like 10 20 30 But, I would like to print the... (4 Replies)
Discussion started by: jacobs.smith
4 Replies

7. Shell Programming and Scripting

Print in New line in loop

Hi , i want to print the output in line by line while read LINE do echo $LINE | grep UCM | egrep '(Shutdown|Unavailable)' echo $LINE | grep SRBr | egrep '(Shutdown|Unavailable)' echo $LINE | grep SRP| egrep '(Shutdown|Unavailable)' echo $LINE | grep OM | grep JMS|... (7 Replies)
Discussion started by: navsan
7 Replies

8. Shell Programming and Scripting

Awk script to run a sql and print the output to an output file

Hi All, I have around 900 Select Sql's which I would like to run in an awk script and print the output of those sql's in an txt file. Can you anyone pls let me know how do I do it and execute the awk script? Thanks. (4 Replies)
Discussion started by: adept
4 Replies

9. Shell Programming and Scripting

awk help required to group output and print a part of group line and original line

Hi, Need awk help to group and print lines to format the output as shown below INPUT FORMAT set echo on set heading on set spool on /* SCHEMA1 */ CREATE TABLE T1; /* SCHEMA1 */ CREATE TABLE T2; /* SCHEMA1 */ CREATE TABLE T3; /* SCHEMA1 */ CREATE TABLE T4; /* SCHEMA1 */ CREATE TABLE T5;... (5 Replies)
Discussion started by: rajan_san
5 Replies
Login or Register to Ask a Question