Solved

Need to pull out fields using awk or similar

Posted on 2002-07-22
10
278 Views
Last Modified: 2010-04-21
From the following output, I need to pull out the field to the left of the "1" - e.g. 'hostnameb'. Is there a simple awk statement or similar that I can use to do this?

AnSG   SystemList    global   hostnamea 2 hostnameb 1

The field will not necessarily be second from the end - but always the first field to the left of the "1". Shortest way gets the points.
0
Comment
Question by:jonke
10 Comments
 
LVL 51

Accepted Solution

by:
ahoffmann earned 100 total points
ID: 7170433
echo "AnSG  SystemList  global hostnamea 2 hostnameb 1"|perl -pe 's#.*\s+([^\s]*)\s+1\s+.*#\1#'
0
 
LVL 1

Expert Comment

by:Techno__Mage
ID: 7171046
The following will work, but only if it is always the 6th item in the output :  

echo "AnSG   SystemList    global   hostnamea 2 hostnameb 1" | awk {'print $6'}
0
 
LVL 84

Expert Comment

by:ozo
ID: 7171055
echo "AnSG SystemList global hostnamea 2 hostnameb 1"|perl -pe 's#.*\b(\w+)\s+1\b.*#$1#'
0
 
LVL 51

Expert Comment

by:ahoffmann
ID: 7171283
ozo is always a few characters shorter ;-)
Anyway, this solution fails (as mine does too) on some special patterns:
  AnSG SystemList global hostnamea 2 hostnameb 1x
I'm wondering 'cauase \b should do the trick
0
 
LVL 84

Expert Comment

by:ozo
ID: 7171314
What does jonke want to do with pattterns like 1x ?
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 51

Expert Comment

by:ahoffmann
ID: 7171359
> .. want to do with pattterns like 1x
don't know, but tread the question:

  The field will not necessarily be second from the end - but always the first field to the left of the "1"

and this example:

  foo bar global host1name 1 hostnameb 1x host 1 bla bla

jonke, could you please clarify
0
 
LVL 2

Author Comment

by:jonke
ID: 7171584

To clarify, I have  list of hosts and associated id's
upto a maximum of 32. They are not necessarily in order.

e.g. host1a 2 host4x 1 anotherhost 3

I need to pick out the primary host - which will be the hostname to the left of the standalone '1'. Can you see any potential probs using this across two different OS's? This script will be running on both Sun and HP machines.
0
 
LVL 6

Expert Comment

by:bira
ID: 7196494
set -A arr `echo "AnSG   SystemList    global   hostnamea 2 hostnameb 1"`
i=0
host=x
while [ "$host" <>  "" ] ;
do
host_before=$host
host=${arr[$i]}
       if [ $host = 1 ] ; then
               echo "Primary Host:" $host_before
          exit
       fi
i=`expr $i + 1`
done
0
 
LVL 20

Expert Comment

by:tfewster
ID: 8048910
No comment has been added lately, so it's time to clean up this Topic Area.
I will leave a recommendation for this question in the Cleanup topic area as follows:

- Answered by ahoffmann

Please leave any comments here within the next 7 days

PLEASE DO NOT ACCEPT THIS COMMENT AS AN ANSWER !

tfewster
Cleanup Volunteer
0
 

Expert Comment

by:SpideyMod
ID: 8096451
per recommendation

SpideyMod
Community Support Moderator @Experts Exchange
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Help stop mail server from sending spam. 27 122
Write an app 10 66
How to Insert a File Using Text Editor 9 79
Install MySQL 5.6 and PHP on Centos Linux 6 99
I promised to write further about my project, and here I am.  First, I needed to setup the Primary Server.  You can read how in this article: Setup FreeBSD Server with full HDD encryption (http://www.experts-exchange.com/OS/Unix/BSD/FreeBSD/A_3660-S…
Why Shell Scripting? Shell scripting is a powerful method of accessing UNIX systems and it is very flexible. Shell scripts are required when we want to execute a sequence of commands in Unix flavored operating systems. “Shell” is the command line i…
Learn how to get help with Linux/Unix bash shell commands. Use help to read help documents for built in bash shell commands.: Use man to interface with the online reference manuals for shell commands.: Use man to search man pages for unknown command…
Learn how to navigate the file tree with the shell. Use pwd to print the current working directory: Use ls to list a directory's contents: Use cd to change to a new directory: Use wildcards instead of typing out long directory names: Use ../ to move…

914 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now