Issues with executing awk


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Issues with executing awk
# 1  
Old 08-24-2015
Issues with executing awk

I am piping some output to awk and would like to print fields $1 $2 and $3 $4 only if they exist.

Note the awk begins with awk '{print $NF " " since I want the last field printed first.

Last edited by Don Cragun; 08-24-2015 at 06:52 PM.. Reason: Add ICODE tags.
# 2  
Old 08-24-2015
It would be a LOT easier for us to help us if you would:
  1. Show us your entire awk script instead of just the 19 characters of it.
  2. Show us sample input.
  3. Show us the (exact) output you want to produce from that sample input.
  4. Tell us what operating system you're using.
  5. Tell us what shell you're using.
# 3  
Old 08-24-2015
Thank you Don!
# 4  
Old 08-25-2015
RedHat

Don, thank you for the feedback; I will try to revise my question.

OS: RHEL
Shell: bash

Code:
DNSname=test1.test.com

InstancePort=8080
ListenerPort=80

InstancePort=8082
ListenerPort=443

The output above is in a file called dns.out
The following is what I would like it to display

Code:
test1.test.com  80 > 8080  443>8081

So port 80 is forwarded to 8080 and 443 is forwarded to 8082

However, within the same query, within my loop I might have a DNSname that has only one listener and Loadbalance port

ie.
test2.test.com. 80 > 8082

So I was trying to use a combination of grep, awk, tr, paste to produce the output

I hope this is clear.
Moderator's Comments:
Mod Comment Please use CODE and ICODE tags as required by forum rules. Do not depend on moderators and administrators to clean up your posts for you.









Last edited by Don Cragun; 08-25-2015 at 01:38 AM.. Reason: Add CODE and ICODE tags, fix typos.
# 5  
Old 08-25-2015
Quote:
Originally Posted by motdman
Don, thank you for the feedback; I will try to revise my question.

OS: RHEL
Shell: bash

Code:
DNSname=test1.test.com

InstancePort=8080
ListenerPort=80

InstancePort=8082
ListenerPort=443

The output above is in a file called dns.out
The following is what I would like it to display

Code:
test1.test.com  80 > 8080  443>8081

So port 80 is forwarded to 8080 and 443 is forwarded to 8082

However, within the same query, within my loop I might have a DNSname that has only one listener and Loadbalance port

ie.
test2.test.com. 80 > 8082

So I was trying to use a combination of grep, awk, tr, paste to produce the output

I hope this is clear.
Moderator's Comments:
Mod Comment Please use CODE and ICODE tags as required by forum rules. Do not depend on moderators and administrators to clean up your posts for you.
I don't understand where the 8081 comes from in the output when the input had 8082, and I don't understand why you sometimes have spaces around the > in your output and sometimes do not have spaces.

Assuming that you want to copy various portions of you input file to the input (instead of making up seemingly random numbers) and that you don't really want spaces around the greater than signs, you could try something like:
Code:
awk -F'=' '
/DNSname=/ {		printf("%s", $2) }
/InstancePort=/ {	IP = $2 }
/ListenerPort=/ {	printf(" %s>%s", $2, IP) }
END {			print "" }' dns.out

which, with your sample input, produces the output:
Code:
test1.test.com 80>8080 443>8082

If someone wants to try this script on a Solaris/SunOS system, change awk to /usr/xpg4/bin/awk or nawk.
This User Gave Thanks to Don Cragun For This Post:
# 6  
Old 08-25-2015
Also to add to Don's reply, where has the red _full_stop_......
test2.test.com. 80 > 8082
......come from?

These are probably typos but be careful as it throws the rest of us...
This User Gave Thanks to wisecracker For This Post:
# 7  
Old 08-25-2015
Sorry for the confusion as I try to find my way around, it was not deliberate.
The text you put in code tags is not code, not variables, but rather text within a file I would like to query with grep, awk, etc. Thank you
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Advanced & Expert Users

Executing if dynamic conditions in awk

Hi All, I got struck at the below point where i am unable to get the desired output after forming the dynamic conditions.Below is the design. 1. We are getting inputs from the shell arguments and storing in a variable like below. CONDITIONS="1=CT,2=US_10,3=CT_US_10" 2. After this i am... (14 Replies)
Discussion started by: cskumar
14 Replies

2. Shell Programming and Scripting

Query on executing awk using SSH

Hi All, Im trying to find the count of process running on remote server using SSH. Below command dosen't work. ssh -q user@host "ps -ef | grep "pattern" | grep -v 'grep' | awk '{print $2}'|wc -l" But below command works. ssh -q user@host "ps -ef | grep "pattern" | grep -v... (1 Reply)
Discussion started by: Girish19
1 Replies

3. Shell Programming and Scripting

Help with executing awk and While loop

Hi All, I have a file say, sample.txt Source Name: xxx Department|Revenue 1001|3252 1002|3345 I am using the above file in one of my script. I need to read from Line 3 of the above the file and do some process. My script has a code: awk 'NR > 2' sample.txt | while read Dep; do... (9 Replies)
Discussion started by: machomaddy
9 Replies

4. Shell Programming and Scripting

Issues with awk and tcsh

Hello experts, I have two files which I'm uploading. One is an awk script and other file acts as an input to the script via positional parameter. awk -f intlmenu.awk jobsq.txt This run fine in C shell on SCO OpenServer Release 5.0.7. When I run it on Solaris 10 ( tcsh shell ) I get... (2 Replies)
Discussion started by: maverick_here
2 Replies

5. Shell Programming and Scripting

Awk OFS issues

Hi, Could anyone tell me what Im doing wrong here any help will be much appreciated #!/bin/bash ls -ltr /export/home/tjmoore > /export/home/tjmoore/log100 awk -F " " /export/home/tjmoore/log100 'BEGIN {OFS="\t";} {print $1,$2,$3,$4,$5, $6,$7,$8,$9;}' > /export/home/tjmoore/log1001 I... (9 Replies)
Discussion started by: 02JayJay02
9 Replies

6. Shell Programming and Scripting

Awk OFS issues

Hi Im trying to tidy up the output of a who command when it writes to a log, everything I've tried doesnt seem to work though, any help would be massively appreciated. Im using the awk command to set the OFS as tab. #!/bin/bash who >> /export/home/tjmoore/logusers awk -F 'BEGIN... (3 Replies)
Discussion started by: 02JayJay02
3 Replies

7. UNIX for Dummies Questions & Answers

Awk Performance Issues

Hi All, I'm facing an issue in my awk script. The script is processing a large text file having the details of a number of persons, each person's details being written from 100 to 250 tags as given below: 100 START| 101klklk| ... 245 opr| 246 55| 250 END| 100 START| ... 245 pp| 246... (4 Replies)
Discussion started by: pgp_acc1
4 Replies

8. AIX

Issues with AWK

Hi there I have written a ksh script on a Red Hat OS and the following extract works. awk '{if (NR != 1) {print $rec1_field }}' $file1 >> combined When I run the same script on an AIX OS, I get the following error. awk: 0602-562 Field $() is not correct. The input line number is 2. The... (12 Replies)
Discussion started by: alanp
12 Replies

9. Shell Programming and Scripting

Awk output issues.

I have the follwing code: awk '{print $1}' HITS #Searches HITS file column one. Column one is filenames awk '{print $2}' HITS | sort -n | wc -l #Searches HITS file and sorts numerically and outputs line count. column 2 is IP addresses awk... (4 Replies)
Discussion started by: amatuer_lee_3
4 Replies

10. Shell Programming and Scripting

Remotely executing awk command

ssh user@machine awk '{ split ($1,ar,"!");print ar}' samp >samp1 Error: Unmatched '. However on <machine> awk '{ split ($1,ar,"!");print ar}' samp >samp1 executes successfully. Any suggestions. (1 Reply)
Discussion started by: bishweshwar
1 Replies
Login or Register to Ask a Question