Solved

Need to pull out fields using awk or similar

Posted on 2002-07-22
10
282 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 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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

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 find files with the shell using the find and locate commands. Use locate to find a needle in a haystack.: With locate, check if the file still exists.: Use find to get the actual location of the file.:
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.

738 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