Cutting Part of Output | Unix Linux Forums | Shell Programming and Scripting

  Go Back    


Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

Cutting Part of Output

Shell Programming and Scripting


Closed Thread    
 
Thread Tools Search this Thread Display Modes
    #1  
Old 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 11-17-2010
DGPickett DGPickett is offline Forum Advisor  
Registered User
 
Join Date: Oct 2010
Last Activity: 8 July 2014, 12:19 PM EDT
Location: Southern NJ, USA (Nord)
Posts: 4,378
Thanks: 8
Thanked 535 Times in 514 Posts
man cut
Sponsored Links
    #3  
Old 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 11-17-2010
palsevlohit_123's Avatar
palsevlohit_123 palsevlohit_123 is offline
Registered User
 
Join Date: Aug 2008
Last Activity: 2 May 2013, 8:00 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 11-17-2010
vgersh99's Avatar
vgersh99 vgersh99 is online now Forum Advisor  
Forum Advisor
 
Join Date: Feb 2005
Last Activity: 28 July 2014, 9:55 AM EDT
Location: Foxborough, MA
Posts: 7,644
Thanks: 150
Thanked 577 Times in 542 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 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

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 11-17-2010
DGPickett DGPickett is offline Forum Advisor  
Registered User
 
Join Date: Oct 2010
Last Activity: 8 July 2014, 12:19 PM EDT
Location: Southern NJ, USA (Nord)
Posts: 4,378
Thanks: 8
Thanked 535 Times in 514 Posts

Code:
cut -d '.' -f 6

Sponsored Links
Closed Thread

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

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 10:23 AM.