- Community Home
- >
- Servers and Operating Systems
- >
- Operating Systems
- >
- Operating System - HP-UX
- >
- Scripting question
Categories
Company
Local Language
Forums
Discussions
Forums
- Data Protection and Retention
- Entry Storage Systems
- Legacy
- Midrange and Enterprise Storage
- Storage Networking
- HPE Nimble Storage
Discussions
Discussions
Discussions
Forums
Forums
Discussions
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
- BladeSystem Infrastructure and Application Solutions
- Appliance Servers
- Alpha Servers
- BackOffice Products
- Internet Products
- HPE 9000 and HPE e3000 Servers
- Networking
- Netservers
- Secure OS Software for Linux
- Server Management (Insight Manager 7)
- Windows Server 2003
- Operating System - Tru64 Unix
- ProLiant Deployment and Provisioning
- Linux-Based Community / Regional
- Microsoft System Center Integration
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Community
Resources
Forums
Blogs
- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Printer Friendly Page
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО09-28-2010 07:39 AM
тАО09-28-2010 07:39 AM
I have a file that I want to chop up and it's quite straightforward but I can't figure out how to do it in awk.
The text file consists of an FQDN and a OS version number and I want to replicate it so that it's just hostname and OS version. For example:
box1.xyz.com 07.12.00
box2.xyz.com 08.43.01
box3.xyz.co.uk 06.22.02
and I want to just strip that down to:
box1 07.12.00
box2 08.43.01
box3 06.22.02
I've tried catting the file into awk -F ".", etc but I'm braindead today. Can you have awk called within an awk statement?
Thanks for any help.
jackie
Solved! Go to Solution.
- Tags:
- awk
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО09-28-2010 08:04 AM
тАО09-28-2010 08:04 AM
Solutionwhile read -r DL
do
host=$(echo $DL | awk -F. '{print $1}')
part2=$(echo $DL | awk '{print $2}')
echo "$host $part2"
done < file
file is wherever this data is sitting.
SEP
Owner of ISN Corporation
http://isnamerica.com
http://hpuxconsulting.com
Sponsor: http://hpux.ws
Twitter: http://twitter.com/hpuxlinux
Founder http://newdatacloud.com
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО09-28-2010 08:16 AM
тАО09-28-2010 08:16 AM
Re: Scripting question
do
echo ${a%%.*} ${a#* }
done < inputfile
or
awk ' { split($1,a,"."); printf "%s %s\n" ,a[1], $2 ;}'
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО09-28-2010 09:04 AM
тАО09-28-2010 09:04 AM
Re: Scripting question
You can simplify by reading two parms:
while read host_ip version; do
echo ${host_ip%%.*} ${version}
done < inputfile
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО09-28-2010 11:57 AM
тАО09-28-2010 11:57 AM
Re: Scripting question
( Never cat a file into awk. A waste of time! :-)
The problem with specifying a period as field separator is that whitepsace is no longer a separator. So you need to speciy both.
And then the problem become that there is a variable number of fields, which you can resolve by printing relative to the end.
The awk solution then becomes:
$ awk -F'[. ]' '{print $1,$(NF-2) "." $(NF-1) "." $NF}' tmp.txt
box1 07.12.00
box2 08.43.01
box3 06.22.02
But it is easier to just replace all non-spaces after the first period with nothing:
$ awk '{sub (/\.[^ ]+/,""); print}' tmp.txt
box1 07.12.00
box2 08.43.01
box3 06.22.02
using perl...
$ perl -pe 's/\.[^ ]+//' tmp.txt
box1 07.12.00
box2 08.43.01
box3 06.22.02
or replace everything from the first periods
through the first space, by a space:
$ perl -pe 's/\..*? / /' tmp.txt
Enjoy.
Hein
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО09-29-2010 03:12 AM
тАО09-29-2010 03:12 AM
Re: Scripting question
sed 's/\([^.]*\).* \([^ ]*\)/\1 \2/p'
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО09-29-2010 05:11 AM
тАО09-29-2010 05:11 AM
Re: Scripting question
Dave, the sed one didn't work but I guess I can tweak it.