Unix/Linux Go Back    


Shell Programming and Scripting Unix shell scripting - KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and shell scripts and shell scripting languages here.

Cutting Part of Output

Shell Programming and Scripting


Closed Linux or Unix Question    
 
Thread Tools Search this Thread Display Modes
    #1  
Old Unix and Linux 11-17-2010
lee.n.doan lee.n.doan is offline
Registered User
 
Join Date: Nov 2010
Last Activity: 29 January 2014, 8:37 AM EST
Posts: 9
Thanks: 6
Thanked 0 Times in 0 Posts
Cutting Part of Output

Hello all

I'm using bourne shell and need to figure out how to cut out a specific portion of some output. For example, my output from my command is:

12.12.52.125.in-addr.arpa name = hostname.domain.main.gov


I need to get just the "hostname.domain.main.gov" part. What I'm trying to do is make files named the "domain" part and putting the hostnames within them. I'm having a bit of trouble cutting out the "hostname" and "domain" part specifically though...

Any suggestions?

Thanks in advance, folks
Sponsored Links
    #2  
Old Unix and Linux 11-17-2010
DGPickett DGPickett is offline Forum Advisor  
Registered User
 
Join Date: Oct 2010
Last Activity: 17 February 2015, 1:56 PM EST
Location: Southern NJ, USA (Nord)
Posts: 4,671
Thanks: 8
Thanked 586 Times in 559 Posts
man cut
Sponsored Links
    #3  
Old Unix and Linux 11-17-2010
lee.n.doan lee.n.doan is offline
Registered User
 
Join Date: Nov 2010
Last Activity: 29 January 2014, 8:37 AM EST
Posts: 9
Thanks: 6
Thanked 0 Times in 0 Posts
So if I used -d".", how could I get, say the 5th or 6th part?
    #4  
Old Unix and Linux 11-17-2010
palsevlohit_123's Unix or Linux Image
palsevlohit_123 palsevlohit_123 is offline
Registered User
 
Join Date: Aug 2008
Last Activity: 5 June 2015, 9:15 AM EDT
Location: India-Chennai
Posts: 132
Thanks: 0
Thanked 7 Times in 7 Posts

Code:
echo "12.12.52.125.in-addr.arpa name = hostname.domain.main.gov" | awk -F"=" 
'{print $2}'

The Following User Says Thank You to palsevlohit_123 For This Useful Post:
lee.n.doan (11-17-2010)
Sponsored Links
    #5  
Old Unix and Linux 11-17-2010
vgersh99's Unix or Linux Image
vgersh99 vgersh99 is offline Forum Advisor  
Forum Advisor
 
Join Date: Feb 2005
Last Activity: 28 July 2015, 8:02 PM EDT
Location: Foxborough, MA
Posts: 7,843
Thanks: 175
Thanked 658 Times in 615 Posts

Code:
echo "12.12.52.125.in-addr.arpa name = hostname.domain.main.gov" | nawk -F'[ =.]' '{close(out);out=$(NF-2);print $(NF-3)>>out}'

The Following User Says Thank You to vgersh99 For This Useful Post:
lee.n.doan (11-17-2010)
Sponsored Links
    #6  
Old Unix and Linux 11-17-2010
lee.n.doan lee.n.doan is offline
Registered User
 
Join Date: Nov 2010
Last Activity: 29 January 2014, 8:37 AM EST
Posts: 9
Thanks: 6
Thanked 0 Times in 0 Posts
Was confused at the nawk for a sec then realized I should be using gawk Linux

Thanks for all your help guys. I really want to understand why these work though, so could someone explain the '{close(out);out=$(NF-2);print $(NF-3)>>out}' part if its not too much of a hassle?

So gawk -F is using "=" as a delimiter and then splitting up the output that way? then I assume the parts are assigned to $# variables then the $(NF-2 or 3) is getting the 'hostname' and 'domain' parts?
Sponsored Links
    #7  
Old Unix and Linux 11-17-2010
DGPickett DGPickett is offline Forum Advisor  
Registered User
 
Join Date: Oct 2010
Last Activity: 17 February 2015, 1:56 PM EST
Location: Southern NJ, USA (Nord)
Posts: 4,671
Thanks: 8
Thanked 586 Times in 559 Posts

Code:
cut -d '.' -f 6

Sponsored Links
Closed Linux or Unix Question

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Linux More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
need a part of output data as output nitinrp1 Shell Programming and Scripting 8 03-30-2010 01:33 PM
How to capture only some part of output when command executed?? vijaysachin Solaris 4 11-17-2009 06:08 AM
Redirecting part of output to stdout Legend986 Shell Programming and Scripting 2 10-17-2008 03:20 PM
Set specific part in command output into variable orit Shell Programming and Scripting 7 08-16-2008 10:34 AM
cutting part of string dhaval_khamar Shell Programming and Scripting 3 07-25-2005 10:18 AM



All times are GMT -4. The time now is 08:36 PM.