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
# 15  
Old 03-16-2016
Quote:
Originally Posted by RudiC
Using the default field separator, there's just two fields in your file, so why use -k5 for sorting?
As you don't show the directory structure, and seem to keep changing targets, I'd suggest to play around with the sub regexes until it fits what you need. Also, I still don't understand why you keep the space in the regex making it pointless.
For your function, why don't you cd into the correct directory at the beginning?
I used sort -k5 because I need it to sort by the 5th column from my print $5, which it does. Is there a better way of doing it?

Initially, I was doing cd into the directory and running the function there and that's fine, everything displays properly (with the function edited of course to remove what I added). What I would like to do is be able to run my function from any directory as I have a few different job functions, and it's easier on me to stay in my main directory where I do a lot of my work. Eventually, I'll have everything I need automated into an alias or function so I don't have to jump around at all.

I added the space in print $5 " " FILENAME to make the output look the way I want it. I like this [10.0R4.7] device1-r0.cpe.domain.net over this [10.0R4.7]device1-r0.cpe.domain.net I just like how that looks.


Here is where I'm at now
Code:
cpever () {
	awk '/JUNOS/ && /boot/ {sub ("show.version", "", FILENAME); print $5 " " FILENAME}' ~/svn/nw_config_data/*cpe.domain.net/show.version | sort -k5
}

Which gives me the output of:
[10.0R4.7] /home/clmbn-eng2/a/rwalker/svn/nw_config_data/device-r0.cpe.domain.net/

A little more tinkering and I should get it.
# 16  
Old 03-16-2016
Using a key outside the range makes sort ignore it and use the entire line for a key which by accident is what you want.
print $5 will print one single field which in the output is field 1 (becoming the default key, then).
You can use another sub (...) to remove the leading path from the file name.

---------- Post updated at 11:09 ---------- Previous update was at 11:08 ----------

You could cd to the working dir, and cd back to the original dir when finished.
# 17  
Old 03-16-2016
Quote:
Originally Posted by RudiC
Using a key outside the range makes sort ignore it and use the entire line for a key which by accident is what you want.
print $5 will print one single field which in the output is field 1 (becoming the default key, then).
You can use another sub (...) to remove the leading path from the file name.

---------- Post updated at 11:09 ---------- Previous update was at 11:08 ----------

You could cd to the working dir, and cd back to the original dir when finished.
So, I should have used sort -g instead? I tested with that and it worked too.

I know I can cd in and out as I need to. It's not that big of a deal, it's just something I wanted to accomplish with a function.
# 18  
Old 03-16-2016
You can cd in and out in a function.

---------- Post updated at 11:30 ---------- Previous update was at 11:29 ----------

Why -g ?
# 19  
Old 03-16-2016
Quote:
Originally Posted by RudiC
You can cd in and out in a function.

---------- Post updated at 11:30 ---------- Previous update was at 11:29 ----------

Why -g ?
-g because it sorts numerically, which I need based on the software version number. Older software versions need to be at the top of my list, which -g has also accomplished.

If this also works by accident, I'm fine with that.

---------- Post updated at 07:12 AM ---------- Previous update was at 06:37 AM ----------

I added cd to my function to jump into that directory, run the script, then cd back to where I want to be and everything is exactly as I need it.

You've taught me a lot in the last day or so, RudiC, whether it may appear that way or not haha!
# 20  
Old 03-20-2016
Came back to playing with this. I found that just a plan old sortdoes the sort as I need. RudiC, I think you were trying to point me in that direction previously with your questions about why use -k5and-g

After presenting this to the boss, he really liked it and wants to get it implemented in the system for everyone to use, but not without a few changes. This brings me back to a previous problem and introduced new issues (of course, new features = new problems to debug).

What I have currently is:
Code:
cpever () {
	awk '/JUNOS/ && /boot/ {sub ("/show.version", "", FILENAME); print $5 " " FILENAME}' ~/svn/nw_config_data/*cpe.domain.net/show.version | sort
}

Which then outputs:
[10.0R4.7] /home/clmbn-eng2/a/rwalker/svn/nw_config_data/device-r0.cpe.domain.net

I've been trying to strip this out out /home/clmbn-eng2/a/rwalker/svn/nw_config_data/so that my output looks like this instead [10.0R4.7] device-r0.cpe.domain.netbut I can't get it right. To test it I replaced "/show.version"with the full path, and that got rid of it, but left the /show.version.

RudiC, you had mentioned another sub(...)but I failed horribly at implementing it. I tried setting it up like this:
Code:
cpever () {
	awk '/JUNOS/ && /boot/ {sub ("/show.version", "", FILENAME); sub ("/home/clmbn-eng2/a/rwalker/svn/nw_config_data/"); print $5 " " FILENAME}' ~/svn/nw_config_data/*cpe.domain.net/show.version | sort
}

but I get this error, and I just am not sure how to do it right
Code:
awk: syntax error at source line 1
 context is
        /JUNOS/ && /boot/ {sub ("/show.version", "", FILENAME); sub >>>  ("/home/clmbn-eng2/a/rwalker/svn/nw_config_data/") <<< 
awk: illegal statement at source line 1

Notice that in the path after the /a/is my username. If I want this to work for everyone, I can't have my username there. So, I tried setting it up like this "/home/clmbn-eng2/a/*/svn/nw_config_data/" to use the *as a wildcard, but that doesn't work and just shows me the full path again. I checked the /home/clmbn-eng2/a/directory and I see all the different users there, so I'm wondering if maybe in this instance *doesn't work as a wildcard search? If that's the case, how do I wildcard it to make this command friendly for everyone?

Once I sort out how to wildcard that part of the directory, and remove the filepath up to the device name AND the "/show.version"I can move on to what the boss wants next. He wants one command that contains all 3 cpe subdomains we have that can be hit with a variable like -c, -e, or -o. I'm still researching how to do all the different variables and stuff with awk, so for now I have this setup as a starting point for after I learn some more and figure out the multiple subs to strip out stuff I don't want to show:

Code:
cver () {
	#Need a help defined by -h which would show the different variables available to the user. Also suggest grep | "xx" to find specific versions
	#cver without any variables should kick back -h message, if possible, to user that they need to define a variable such as -c, -e, -o
	#cver without those variables is no fun having everything shown to you at once	
	awk '/JUNOS/ && /boot/ {sub ("/show.version", "", FILENAME); print $5 " " FILENAME}' ~/svn/nw_config_data/*cpe.domain.net/show.version | sort
	#I want cpe to be accessed by -c
	#Example: cver -c
	#
	awk '/JUNOS/ && /boot/ {sub ("/show.version", "", FILENAME); print $5 " " FILENAME}' ~/svn/nw_config_data/*cpe2.domain.net/show.version | sort
	#I want cpe2 to be accessed by -e
	#Example: cver -e
	#
	awk '/JUNOS/ && /boot/ {sub ("/show.version", "", FILENAME); print $5 " " FILENAME}' ~/svn/nw_config_data/*cpe3.domain.net/show.version | sort
	#I want cpe3 to be accessed by -o
	#Example: cver -o
}

# 21  
Old 03-20-2016
awk's sub function needs two or three parameters; you supply but one.
awk uses regexes, not (shell) patterns; the latter uses * for a wildcard char, the former [ICODE] . [/ICODE, optionally followed by a repetition char, one of which would be * . But, beware of false matches - e.g. a / would match as well.

I'd propose to consult man awk.

However, try this amended version of your command:
Code:
awk '/JUNOS/ && /boot/ {sub ("/show.version", "", FILENAME); sub ("/home/clmbn-eng2/a/[^/]*/svn/nw_config_data/", "", FILENAME); print $5 " " FILENAME}' ~/svn/nw_config_data/*cpe.domain.net/show.version | sort

---------- Post updated at 12:38 ---------- Previous update was at 12:33 ----------

or a shortcut version:
Code:
awk '/JUNOS/ && /boot/ {sub ("/show.version", "", FILENAME); sub (".*/", "", FILENAME); print $5 " " FILENAME}' ~/svn/nw_config_data/*cpe.domain.net/show.version | sort

---------- Post updated at 12:42 ---------- Previous update was at 12:38 ----------

And , to your boss' wishes: how familiar are you with shell functions and shell parameters? And the case ... esac construct?
This User Gave Thanks to RudiC For This Post:
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