LinuxQuestions.org
Register a domain and help support LQ
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices



Reply
 
Search this Thread
Old 12-05-2007, 01:49 PM   #1
metallica1973
Senior Member
 
Registered: Feb 2003
Location: Washington D.C
Posts: 2,121

Rep: Reputation: 58
Cut Help using Solaris 10


How can use the cut command to cut the last two octets or hostname from a string like for example:

PHP Code:
S00CBBF.aa.bb.ccc.edu 
To

PHP Code:
S00CBBF.aa.bb 
Thanks

Last edited by metallica1973; 12-05-2007 at 02:16 PM.
 
Old 12-05-2007, 02:12 PM   #2
schneidz
Senior Member
 
Registered: May 2005
Location: boston, usa
Distribution: fc-15/ fc-20-live-usb/ aix
Posts: 4,203

Rep: Reputation: 643Reputation: 643Reputation: 643Reputation: 643Reputation: 643Reputation: 643
i dunno' if there is a simpler way but this works:
Code:
echo S00CBBF.aa.bb.ccc.edu | rev | cut -d . -f 3- | rev
 
Old 12-05-2007, 02:12 PM   #3
pixellany
LQ Veteran
 
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Arch/XFCE
Posts: 17,802

Rep: Reputation: 729Reputation: 729Reputation: 729Reputation: 729Reputation: 729Reputation: 729Reputation: 729
Why not SED. This is crude, but works:

sed 's/\....\....$//' filename

Translation: find a literal ".", any 3 characters, another literal ".", 3 more characters, and then the end of the line. Replace by an empty string.
 
Old 12-05-2007, 02:13 PM   #4
metallica1973
Senior Member
 
Registered: Feb 2003
Location: Washington D.C
Posts: 2,121

Original Poster
Rep: Reputation: 58
I am using Sun Solaris 10. Is that command 'rev' still valid?
 
Old 12-05-2007, 02:32 PM   #5
schneidz
Senior Member
 
Registered: May 2005
Location: boston, usa
Distribution: fc-15/ fc-20-live-usb/ aix
Posts: 4,203

Rep: Reputation: 643Reputation: 643Reputation: 643Reputation: 643Reputation: 643Reputation: 643
Quote:
Originally Posted by metallica1973 View Post
I am using Sun Solaris 10. Is that command 'rev' still valid?
i guess not:
Code:
Sun Microsystems Inc.   SunOS 5.8       Generic Patch   February 2004
schneidz@lq:/temp> echo hello-world | rev
ksh: rev:  not found
try compiling yourself one of these:
Code:
schneidz@lq:/temp> cat ssh-rev.c
#include <string.h>
#include <stdio.h>


char *str_rev(char *s)
    {
    char *p=s;
    char *q =s;
    char swap;
    if (*s)
    {
        q=strchr(q,'\0');
        while (--q > p)
        {
            swap = *q;
            *q = *p;
            *p = swap;
            ++p;
        }
    }
    return s;
}

main(int argc, char * argv[])
{
 char * loc1;
 char * loc2;
 int locdiff, argv2strlen;
 int pos = 0;

  argv[1] = str_rev(argv[1]);
  loc1 = strchr(argv[1], argv[1][0]);
  loc2 = strchr(argv[1], '/');
  locdiff = loc2 - loc1;
  argv[1][locdiff] = '\0';
  printf(" %s \n", str_rev(argv[1]));
}
 
Old 12-05-2007, 03:20 PM   #6
ilikejam
Senior Member
 
Registered: Aug 2003
Location: Glasgow
Distribution: Fedora / Solaris
Posts: 3,109

Rep: Reputation: 96
awk for the win.
Code:
awk -F'.' '{for (i=1;i<=NF-2;i++) {if (i<NF-2) printf $i"."; else print $i}}'
Dave
 
Old 12-05-2007, 05:50 PM   #7
chrism01
Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Centos 6.6, Centos 5.10
Posts: 16,324

Rep: Reputation: 2041Reputation: 2041Reputation: 2041Reputation: 2041Reputation: 2041Reputation: 2041Reputation: 2041Reputation: 2041Reputation: 2041Reputation: 2041Reputation: 2041
echo S00CBBF.aa.bb.ccc.edu |cut -d'.' -f1-3

gives
S00CBBF.aa.bb

too easy and shorter / clearer than awk
 
Old 12-05-2007, 05:53 PM   #8
ilikejam
Senior Member
 
Registered: Aug 2003
Location: Glasgow
Distribution: Fedora / Solaris
Posts: 3,109

Rep: Reputation: 96
Quote:
Originally Posted by chrism01 View Post
echo S00CBBF.aa.bb.ccc.edu |cut -d'.' -f1-3

gives
S00CBBF.aa.bb

too easy and shorter / clearer than awk
Bzzzzt. The OP wanted rid of the last two fields. What if you don't know in advance how many fields there are in the string?
 
Old 12-05-2007, 06:21 PM   #9
unSpawn
Moderator
 
Registered: May 2001
Posts: 27,725
Blog Entries: 54

Rep: Reputation: 2971Reputation: 2971Reputation: 2971Reputation: 2971Reputation: 2971Reputation: 2971Reputation: 2971Reputation: 2971Reputation: 2971Reputation: 2971Reputation: 2971
Quote:
Originally Posted by ilikejam View Post
Bzzzzt. The OP wanted rid of the last two fields. What if you don't know in advance how many fields there are in the string?
OK, BASH prolly isn't what you're looking for but w/o 'cut': i="S00CBBF.aa.bb.ccc.edu"; i=(${i//./ }); echo "${i[0]}.${i[1]}"
 
Old 12-05-2007, 06:26 PM   #10
ilikejam
Senior Member
 
Registered: Aug 2003
Location: Glasgow
Distribution: Fedora / Solaris
Posts: 3,109

Rep: Reputation: 96
No joy, I'm afraid:
Code:
[0 dave@cronus ~]$ i="S00CBBF.aa.bb.ccc.edu"; i=(${i//./ }); echo "${i[0]}.${i[1]}"
S00CBBF.aa
Should give: S00CBBF.aa.bb , and you'd still have to know in advance how many fields there are in any case.

awk's still in front. Go awk! etc.

Anyone got any perl?

Dave
 
Old 12-05-2007, 06:28 PM   #11
ghostdog74
Senior Member
 
Registered: Aug 2006
Posts: 2,697
Blog Entries: 5

Rep: Reputation: 241Reputation: 241Reputation: 241
Code:
# echo S00CBBF.aa.bb.ccc.edu | nawk 'gsub(/\.[a-zA-Z0-9]+\.[a-zA-Z0-9]+$/,"")'

Last edited by ghostdog74; 12-05-2007 at 07:10 PM.
 
Old 12-05-2007, 06:38 PM   #12
unSpawn
Moderator
 
Registered: May 2001
Posts: 27,725
Blog Entries: 54

Rep: Reputation: 2971Reputation: 2971Reputation: 2971Reputation: 2971Reputation: 2971Reputation: 2971Reputation: 2971Reputation: 2971Reputation: 2971Reputation: 2971Reputation: 2971
heh. way too much work: i="S00CBBF.aa.bb.ccc.edu"; i=(${i//./ }); n=${#i[@]}; unset i[$[$n-1]] i[$[$n-2]]; i=${i[*]}; echo ${i// /.}
 
Old 12-05-2007, 07:26 PM   #13
chrism01
Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Centos 6.6, Centos 5.10
Posts: 16,324

Rep: Reputation: 2041Reputation: 2041Reputation: 2041Reputation: 2041Reputation: 2041Reputation: 2041Reputation: 2041Reputation: 2041Reputation: 2041Reputation: 2041Reputation: 2041
Well, given the OP's example input, 'removing the last 2 fields' and 'keeping the first 3' both give his example answer. The qn is, what does he REALLY need??
 
Old 12-05-2007, 08:04 PM   #14
metallica1973
Senior Member
 
Registered: Feb 2003
Location: Washington D.C
Posts: 2,121

Original Poster
Rep: Reputation: 58
Everyone please read my original post so that all of you can understand my situation.

http://www.linuxquestions.org/questi...-shell-604725/

The output will always be in this format:

PHP Code:
S00F3456.aa.bb.cc.dd.ee.fff.edu 
or


PHP Code:
S00F3456.aa.bb.cc.dd.fff.edu 
We use unix for DNS and we have to use the fully qualified name so what I want is from the output above, logic that can distinguish different fields in the output and remove the last 2 octects, always. so I would need an output like


PHP Code:
S00F3456.bb.cc.dd 
or

PHP Code:
S00F3456.bb.cc.dd.ee 
the last will need to truncated no matter how long the output is!

Last edited by metallica1973; 12-05-2007 at 08:21 PM.
 
Old 12-05-2007, 08:20 PM   #15
ilikejam
Senior Member
 
Registered: Aug 2003
Location: Glasgow
Distribution: Fedora / Solaris
Posts: 3,109

Rep: Reputation: 96
So, basically, you need a function which will remove the last two 'between-dots' fields from your hostname string.

In that case,
Code:
awk -F'.' '{for (i=1;i<=NF-2;i++) {if (i<NF-2) printf $i"."; else print $i}}'
will do it, e.g.
Code:
[0 dave@cronus ~]$ echo S00F3456.aa.bb.ccc.edu | awk -F'.' '{for (i=1;i<=NF-2;i++) {if (i<NF-2) printf $i"."; else print $i}}'
S00F3456.aa.bb
[0 dave@cronus ~]$ echo S00F3456.bb.ccc.edu | awk -F'.' '{for (i=1;i<=NF-2;i++) {if (i<NF-2) printf $i"."; else print $i}}'
S00F3456.bb
[0 dave@cronus ~]$ echo S00F3456.aa.bb.xyz.abc.ccc.edu | awk -F'.' '{for (i=1;i<=NF-2;i++) {if (i<NF-2) printf $i"."; else print $i}}'
S00F3456.aa.bb.xyz.abc
Dave

Last edited by ilikejam; 12-05-2007 at 08:22 PM.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
how to cut an image ? hectorDUQUE Fedora 1 04-28-2007 10:35 PM
Cut from right to left? LocoMojo Programming 26 03-01-2007 06:14 PM
cut question krock923 Programming 1 10-19-2005 05:03 PM
Ok, maybe I'm not cut out for linux... goosegg Linux - Newbie 5 09-01-2003 04:43 PM
cut-problem dahljor Programming 2 07-08-2003 01:58 PM


All times are GMT -5. The time now is 10:54 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration