Solved

Need to pull out fields using awk or similar

Posted on 2002-07-22
10
277 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
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Suggested Solutions

Let's say you need to move the data of a file system from one partition to another. This generally involves dismounting the file system, backing it up to tapes, and restoring it to a new partition. You may also copy the file system from one place to…
Introduction Regular patching is part of a system administrator's tasks. However, many patches require that the system be in single-user mode before they can be installed. A cluster patch in particular can take quite a while to apply if the machine…
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…
In a previous video, we went over how to export a DynamoDB table into Amazon S3.  In this video, we show how to load the export from S3 into a DynamoDB table.

757 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

23 Experts available now in Live!

Get 1:1 Help Now