Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Need to pull out fields using awk or similar

Posted on 2002-07-22
10
Medium Priority
?
284 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
10 Comments
 
LVL 51

Accepted Solution

by:
ahoffmann earned 400 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
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
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
 
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 21

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

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

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

When you do backups in the Solaris Operating System, the file system must be inactive. Otherwise, the output may be inconsistent. A file system is inactive when it's unmounted or it's write-locked by the operating system. Although the fssnap utility…
Using libpcap/Jpcap to capture and send packets on Solaris version (10/11) Library used: 1.      Libpcap (http://www.tcpdump.org) Version 1.2 2.      Jpcap(http://netresearch.ics.uci.edu/kfujii/Jpcap/doc/index.html) Version 0.6 Prerequisite: 1.      GCC …
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.
Suggested Courses

705 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