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
# 22  
Old 03-20-2016
Quote:
Originally Posted by RudiC
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?

Both worked! I decide to opt for the shorter version. I think I tried sub (".*/", at one point, but didn't realize I needed a 2nd or even 3rd item in there for it to work. I was so close!

I can't thank you enough for your help. I've told the boss that you have helped me out too because you deserve the credit for helping a noob out, even if you two have no idea who the other is.

Whenever I finish building this out, I'll post my results. It may end up helping someone in a similar situation in the future!
# 23  
Old 03-20-2016
I'm glad it worked out.
Post your attempts on the function so we can help if need be.
# 24  
Old 03-21-2016
Quote:
Originally Posted by RudiC
I'm glad it worked out.
Post your attempts on the function so we can help if need be.
Will do!

---------- Post updated 03-21-16 at 05:38 AM ---------- Previous update was 03-20-16 at 07:59 AM ----------

I've come up with a couple issues while trying to research and make this work. My Google searches are not bringing me the results I want. Either I'm not searching the right terms or I don't really know what I'm trying to do here.

1) Is it possible to create an aliasinside a function that only works when that function is called?

Ex:
Code:
testfunction () {
	alias test="(print hello)"
}

testfunction test
hello

Obviously the example code I put in doesn't work the way I was hoping. The aliasworks outside the function, but doesn't work with the function name followed by the alias.

Code:
clmbn-eng2.eng:~% testfunction 
clmbn-eng2.eng:~% testfunction test
clmbn-eng2.eng:~% test
hello

2) Can an aliasbe named with a hyphen followed by a letter?

Ex:
Code:
alias -h="(print hello)"

The closest thing I could find was creating an alias named - by doing alias -- -="(print hello)"

3) Since trying to aliasan awkrequires a ton of escaping characters, is it possible to put a function inside a function? Can the function inside the function only be called with the function (similar to, but with functions instead of aliases question 1)

4) Is there an easier way to go about what I'm trying to do?

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
}

# 25  
Old 03-21-2016
1) - 3) What's the purpose of making use of aliases for your problem? Although shells (bash) CAN be made expand aliases within non-interactive scripts, the default is not to do it. And, I think it's not the solution for your problem.

4) What do you mean by "easier way"? What would be easier than calling that function with max. one parameter like cver e Within the function, insert a line like
Code:
case $1 in
  ("c") X="";;
  ("e") X=2;;
  ("o") X=3;;
  (*)   X="*";;
  esac

and run the awk like
Code:
awk '...' ~/svn/nw_config_data/*cpe${X}.domain.net/show.version

# 26  
Old 03-22-2016
RudiC, you are just simply awesome!

I looked up that case-esacstatement to learn a little more about it and how it works. This is exactly what I was looking for!

Here's what I threw together real quick based off your suggestion and what I looked up on case-esac

***Note*** redacted actual names of sub-domains since they aren't cpe2 and cpe3.

Code:
cver () {
case $1 in
  ("-c") X="cpe"
  awk '/JUNOS/ && /boot/ {sub ("/show.version", "", FILENAME); sub (".*/", "", FILENAME); print $5 " " FILENAME}' ~/svn/nw_config_data/*cpe.domain.net/show.version | sort
  ;;
  ("-e") X=exxx
  awk '/JUNOS/ && /boot/ {sub ("/show.version", "", FILENAME); sub (".*/", "", FILENAME); print $5 " " FILENAME}' ~/svn/nw_config_data/*exxx.domain.net/show.version | sort
  ;;
  ("-o") X=oxxx
  awk '/JUNOS/ && /boot/ {sub ("/show.version", "", FILENAME); sub (".*/", "", FILENAME); print $5 " " FILENAME}' ~/svn/nw_config_data/*oxxx.domain.net/show.version | sort
  ;;
  (*)   X="*"
  echo "This is the help section."
  ;;
   esac
}

Everything ran smoothly and exactly as expected!

The boss is on vacation this week, so I won't get to show this to him yet. I do have one more item on my list that I need to get locked down, and that's the ability to search for specific software version numbers. I thought I could get around having to program that by telling people to grepfor the version they want by doing something like cver -c | grep "12"but that greps anything with "12" in it whether it's the software version or the device name.

I'm going to toy around with that for a bit and see if I can get it on my own. I know I want to assign it as -vand have it search the first 4 characters from column 5 for a match, but has to work with all three pieces of the cverfunction (-c,-e,-o)that people would run.

Last edited by rwalker; 03-23-2016 at 12:49 AM..
# 27  
Old 03-22-2016
Why do you define the X variable and then don't use it? With the definition that you show, the input path for the awk command would be~/svn/nw_config_data/*${X}.domain.net/show.version, and you would run just one single copy of the awk command AFTER the case statement.

---------- Post updated at 12:54 ---------- Previous update was at 12:52 ----------

And, if you want to grep for something, why don't you supply it as a parameter to the function, and grep it there? Or, even better, supply it to the awk command and have awk output just the desired.
# 28  
Old 03-23-2016
I think I did that because I misunderstood how that part works. I did kind of rush through it earlier as my shift was ending. I see what you're saying now, and I'll give it a go when I go back to work. I didn't bring my work laptop home today.

I haven't put much thought into the grepportion yet. I think supplying it in awkis a good idea and I'll try that.

---------- Post updated 03-23-16 at 05:27 AM ---------- Previous update was 03-22-16 at 08:39 AM ----------

Alright, I'm stuck again. I put my awkin after the casestatement as you had suggested.

Code:
cver () {
case $1 in
  ("-c") X="cpe";;
  ("-e") X=exxx;;
  ("-o") X=oxxx;;
  ("-h"|help) echo "This is the help command";;
  (*)   X="*"
  echo "Unrecognized or incomplete command.\nUse -h for help.\nOptions are -c for cpe, -e for exxx, and -o for oxxx";;
  esac
awk '/JUNOS/ && /boot/ {sub ("/show.version", "", FILENAME); sub (".*/", "", FILENAME); print $5 " " FILENAME}' ~/svn/nw_config_data/*${X}.domain.net/show.version | sort
  }

Gives me this:
Code:
clmbn-eng2.eng:~% cver
Unrecognized or incomplete command.
Use -h for help.
Options are -c for cpe, -e for exxx, and -o for oxxx
cver:9: no matches found: /home/clmbn-eng2/a/rwalker/svn/nw_config_data/**.domain.net/show.version

clmbn-eng2.eng:~% cver -h
This is the help command
cver:9: no matches found: /home/clmbn-eng2/a/rwalker/svn/nw_config_data/**.domain.net/show.version

clmbn-eng2.eng:~% cver help
This is the help command
cver:9: no matches found: /home/clmbn-eng2/a/rwalker/svn/nw_config_data/**.domain.net/show.version

I'm not sure how I can make this not apply * to my awk

I'm assuming in *${X}there's a way to tell it to not apply *as a search since **.domain.netdoesn't exist, but I couldn't figure it out and kept getting bad substitutionmessages.
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