Grep echo awk print all output on one line


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Grep echo awk print all output on one line
# 1  
Old 03-15-2016
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 then have the script dig through the show.version file to pull a specific entry from there and display it all on one line.

This is the code I've come up with, but the display is all wrong. Note: I am no programmer/scripter, this is something new I'm learning for a recent promotion.

Code:
for CPE_DEVICE in `ls -d *cpe.domain.net`; do; echo $CPE_DEVICE; grep JUNOS $CPE_DEVICE/show.version | grep boot | awk '{print $5}'; done

My output comes out like this

Code:
device1-r0.cpe.domain.net/
[10.4R6.5]
device2-e0.cpe.domain.net/
device3-r0.cpe.domain.net/
[11.4R7.5]

I decided to dissect my code to see where the fault could be. I found that this half works fine in displaying the CPE devices on their own line each

Code:
for CPE_DEVICE in `ls -d *cpe.domain.net`; do; echo $CPE_DEVICE;

I'm not sure what I've done wrong in the second half of my code, but my output should look like this

Code:
device1-r0.cpe.domain.net/[10.4R6.5]
device2-e0.cpe.domain.net/[11.4R7.5]
device3-r0.cpe.domain.net/[11.4R7.5]


Last edited by rwalker; 03-15-2016 at 08:07 AM..
# 2  
Old 03-15-2016
It's not that easy for us to tell what output comes from where. Did you try to stop echo from printing a <newline> char? On some systems (you fail to mention yours), this can be done with the -n option. Or, use printf with an adequate format string.

Please be aware that your entire pipe could probably be replaced by one single awk command.
# 3  
Old 03-15-2016
Quote:
Originally Posted by RudiC
It's not that easy for us to tell what output comes from where. Did you try to stop echo from printing a <newline> char? On some systems (you fail to mention yours), this can be done with the -n option. Or, use printf with an adequate format string.

Please be aware that your entire pipe could probably be replaced by one single awk command.
Programming in ZSH, everything we use is based on FreeBSD.

This line gives me all my CPE in a list just fine.

Code:
for CPE_DEVICE in `ls -d *cpe.domain.net`; do; echo $CPE_DEVICE;

This half is supposed to pull the version running for each CPE and display that next to the CPE name.

Code:
grep JUNOS $CPE_DEVICE/show.version | grep boot | awk '{print $5}'; done


I tried using -n with echo and it didn't help. I don't really know anything about this scripting stuff, so all of this is new to me.

After some research I was thinking that maybe a single awk command would work, but I'm not quite there yet in figuring it out.
# 4  
Old 03-15-2016
Read man echo to find out how to suppress the <newline> char. Or use printf if your zsh provides it.

What would be the result of
Code:
awk '/JUNOS/ && /boot/ {sub (/[^/]*$/, "", FILENAME); print FILENAME $5}' *cpe.domain.net/show.version

?
# 5  
Old 03-15-2016
Quote:
Originally Posted by RudiC
Read man echo to find out how to suppress the <newline> char. Or use printf if your zsh provides it.

What would be the result of
Code:
awk '/JUNOS/ && /boot/ {sub (/[^/]*$/, "", FILENAME); print FILENAME $5}' *cpe.domain.net/show.version

?
I tried echo -n but it still gives me the output wrong. It puts a lot of the devices and their software version on the same line, but I still get lines where multiple devices show together on one line with one software version.

The output comes out like this with echo -n added.

Code:
device1-e0.cpe.domain.net/[11.4R8.5]
device2-e1.cpe.domain.net/device3-e0.cpe.domain.net/[11.4R10.3]
device4-e1.cpe.domain.net/[11.4R10.3]


When I ran what you tried with the proper information filled (filename, domain) in I get this.

Code:
awk: extra ] at source line 1
 context is
        /JUNOS/ && /boot/ {sub >>>  (/[^/] <<< 
awk: syntax error at source line 1
awk: illegal statement at source line 1
        extra ]



The directory I'm running this in has a directory for each CPE device. So this script here pulls a list of all the devices and puts them in a nice neat list for me, no problems.

Code:
for CPE_DEVICE in `ls -d *cpe.domain.net`; do; echo $CPE_DEVICE;

The later half of my code is supposed to pull the show.version information, but GREPing out the word boot and printing the 5th column which is the actual software version. It does do that, however the output isn't where it should be.

Code:
grep JUNOS $CPE_DEVICE/show.version | grep boot | awk '{print $5}'; done

The end result should look like this, where the first half the script pulled each CPE device and the second half pulls the version

Code:
device1-r0.cpe.domain.net/[10.4R6.5]
device2-e0.cpe.domain.net/[11.4R7.5]
device3-r0.cpe.domain.net/[11.4R7.5]

I apologize if I'm not explaining properly or just talking in circles here.
# 6  
Old 03-15-2016
Seems to be an awk version problem. Try to escape the / within the square brackets, like [^\/]*.
# 7  
Old 03-15-2016
Quote:
Originally Posted by RudiC
Seems to be an awk version problem. Try to escape the / within the square brackets, like [^\/]*.
Alright, that allowed the script to run, however, it only gave me a list of the software versions without the device names.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Echo print on same line while loop using variable

Currently using below script but echo it print the output in two line. Input file all-vm-final-2.txt CEALA08893 SDDC_SCUN DS_SIO_Workload_SAPUI_UAT_01 4 CEALA09546 SDDC_SCUN DS-SIO-PD5_Workload_UAT_SP1_Flash_07 4 CEALA09702 SDDC_SCUN DS-VSAN-RMP-WORKLOAD01 4 DEALA08762 SDDC_LDC... (3 Replies)
Discussion started by: ranjancom2000
3 Replies

2. 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

3. Shell Programming and Scripting

Print awk output in same line ,For loop

My code is something like below. #/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... (3 Replies)
Discussion started by: tapia
3 Replies

4. Shell Programming and Scripting

Echo printing a line in 2 lines; expected to print in one line

Dear All, fileName: therm.txt nc3h7o2h 7/27/98 thermc 3h 8o 2 0g 300.000 5000.000 1390.000 41 1.47017550e+01 1.71731699e-02-5.91205329e-06 9.21842570e-10-5.36438880e-14 2 -2.99988556e+04-4.93387892e+01 2.34710908e+00 4.34517484e-02-2.65357553e-05 3 ... (7 Replies)
Discussion started by: linuxUser_
7 Replies

5. Shell Programming and Scripting

Use less pipe for grep or awk sed to print the line not include xx yy zz

cat file |grep -v "xx" | grep -v "yy" |grep -v "zz" (3 Replies)
Discussion started by: yanglei_fage
3 Replies

6. Shell Programming and Scripting

Print (echo) variable in a single line

Hi, I have written this code ------------------------------------------------ # !/bin/ksh i=0 while do j=$i while do echo -e $j #printf "%d",$j j=`expr $j - 1` done echo i=`expr $i + 1` done ---------------------------------------------------- The ouput which... (2 Replies)
Discussion started by: rac
2 Replies

7. UNIX for Dummies Questions & Answers

Grep /Awk letters X - X in every line and print it as a mac address

hey i m kinda new to this so i will appreciate any help , i have this list of values: pwwn = 0x50012482009cd7a7 nwwn=0x50012482009cd7a6 port_id = 0x280200 pwwn = 0x5001248201bcd7a7 nwwn=0x5001248201bcd7a6 port_id = 0x280300 pwwn = 0x50012482009c51ad nwwn=0x50012482009c51ac port_id =... (4 Replies)
Discussion started by: boaz733
4 Replies

8. Shell Programming and Scripting

Awk+Grep Input file needs to match a column and print the entire line

I'm having problems since few days ago, and i'm not able to make it works with a simple awk+grep script (or other way to do this). For example, i have a input file1.txt: cat inputfile1.txt 218299910417 1172051195 1172070231 1172073514 1183135117 1183135118 1183135119 1281440202 ... (3 Replies)
Discussion started by: poliver
3 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

10. UNIX for Dummies Questions & Answers

How do I output or echo NONE if grep does not find anything?

I am performing a grep command and I need to know how to echo "NONE" or "0" to my file if grep does not find what i am looking for. echo What i found >> My_File grep "SOMETHING" >> My_File I am sure this is easy, I am sort of new at this! Thanks (2 Replies)
Discussion started by: jojojmac5
2 Replies
Login or Register to Ask a Question

Featured Tech Videos