Unix/Linux Go Back    


Shell Programming and Scripting BSD, Linux, and UNIX shell scripting — Post awk, bash, csh, ksh, perl, php, python, sed, sh, shell scripts, and other shell scripting languages questions here.

Cutting Part of Output

Shell Programming and Scripting


Closed    
 
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: 1 February 2016, 3:35 PM EST
Location: Southern NJ, USA (Nord)
Posts: 4,673
Thanks: 8
Thanked 587 Times in 560 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: 9 July 2016, 4:32 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: 6 December 2016, 10:30 AM EST
Location: Foxborough, MA
Posts: 7,988
Thanks: 194
Thanked 698 Times in 654 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: 1 February 2016, 3:35 PM EST
Location: Southern NJ, USA (Nord)
Posts: 4,673
Thanks: 8
Thanked 587 Times in 560 Posts

Code:
cut -d '.' -f 6

Sponsored Links
Closed

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 02:33 PM
How to capture only some part of output when command executed?? vijaysachin Solaris 4 11-17-2009 07:08 AM
Redirecting part of output to stdout Legend986 Shell Programming and Scripting 2 10-17-2008 04:20 PM
Set specific part in command output into variable orit Shell Programming and Scripting 7 08-16-2008 11:34 AM
cutting part of string dhaval_khamar Shell Programming and Scripting 3 07-25-2005 11:18 AM



All times are GMT -4. The time now is 01:55 PM.