Solved

Need to pull out fields using awk or similar

Posted on 2002-07-22
10
279 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
Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

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

Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Best way to handle awk return value 6 67
Shell script errors 10 131
restricting types of files/data that could be uploaded to Windows & UNIX servers 5 64
UNIX SCP 5 76
A metadevice consists of one or more devices (slices). It can be expanded by adding slices. Then, it can be grown to fill a larger space while the file system is in use. However, not all UNIX file systems (UFS) can be expanded this way. The conca…
Java performance on Solaris - Managing CPUs There are various resource controls in operating system which directly/indirectly influence the performance of application. one of the most important resource controls is "CPU".   In a multithreaded…
Learn several ways to interact with files and get file information from the bash shell. ls lists the contents of a directory: Using the -a flag displays hidden files: Using the -l flag formats the output in a long list: The file command gives us mor…
This video shows how to set up a shell script to accept a positional parameter when called, pass that to a SQL script, accept the output from the statement back and then manipulate it in the Shell.

785 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