LinuxQuestions.org
Help answer threads with 0 replies.
Home Forums Tutorials Articles Register
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, 12:49 PM   #1
metallica1973
Senior Member
 
Registered: Feb 2003
Location: Washington D.C
Posts: 2,190

Rep: Reputation: 60
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 01:16 PM.
 
Old 12-05-2007, 01:12 PM   #2
schneidz
LQ Guru
 
Registered: May 2005
Location: boston, usa
Distribution: fedora-35
Posts: 5,313

Rep: Reputation: 918Reputation: 918Reputation: 918Reputation: 918Reputation: 918Reputation: 918Reputation: 918Reputation: 918
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, 01:12 PM   #3
pixellany
LQ Veteran
 
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Mint
Posts: 17,809

Rep: Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743
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, 01:13 PM   #4
metallica1973
Senior Member
 
Registered: Feb 2003
Location: Washington D.C
Posts: 2,190

Original Poster
Rep: Reputation: 60
I am using Sun Solaris 10. Is that command 'rev' still valid?
 
Old 12-05-2007, 01:32 PM   #5
schneidz
LQ Guru
 
Registered: May 2005
Location: boston, usa
Distribution: fedora-35
Posts: 5,313

Rep: Reputation: 918Reputation: 918Reputation: 918Reputation: 918Reputation: 918Reputation: 918Reputation: 918Reputation: 918
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, 02:20 PM   #6
ilikejam
Senior Member
 
Registered: Aug 2003
Location: Glasgow
Distribution: Fedora / Solaris
Posts: 3,109

Rep: Reputation: 97
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, 04:50 PM   #7
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Rocky 9.2
Posts: 18,364

Rep: Reputation: 2752Reputation: 2752Reputation: 2752Reputation: 2752Reputation: 2752Reputation: 2752Reputation: 2752Reputation: 2752Reputation: 2752Reputation: 2752Reputation: 2752
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, 04:53 PM   #8
ilikejam
Senior Member
 
Registered: Aug 2003
Location: Glasgow
Distribution: Fedora / Solaris
Posts: 3,109

Rep: Reputation: 97
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, 05:21 PM   #9
unSpawn
Moderator
 
Registered: May 2001
Posts: 29,415
Blog Entries: 55

Rep: Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600
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, 05:26 PM   #10
ilikejam
Senior Member
 
Registered: Aug 2003
Location: Glasgow
Distribution: Fedora / Solaris
Posts: 3,109

Rep: Reputation: 97
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, 05:28 PM   #11
ghostdog74
Senior Member
 
Registered: Aug 2006
Posts: 2,697
Blog Entries: 5

Rep: Reputation: 244Reputation: 244Reputation: 244
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 06:10 PM.
 
Old 12-05-2007, 05:38 PM   #12
unSpawn
Moderator
 
Registered: May 2001
Posts: 29,415
Blog Entries: 55

Rep: Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600
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, 06:26 PM   #13
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Rocky 9.2
Posts: 18,364

Rep: Reputation: 2752Reputation: 2752Reputation: 2752Reputation: 2752Reputation: 2752Reputation: 2752Reputation: 2752Reputation: 2752Reputation: 2752Reputation: 2752Reputation: 2752
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, 07:04 PM   #14
metallica1973
Senior Member
 
Registered: Feb 2003
Location: Washington D.C
Posts: 2,190

Original Poster
Rep: Reputation: 60
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 07:21 PM.
 
Old 12-05-2007, 07:20 PM   #15
ilikejam
Senior Member
 
Registered: Aug 2003
Location: Glasgow
Distribution: Fedora / Solaris
Posts: 3,109

Rep: Reputation: 97
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 07:22 PM.
 
  


Reply



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 09:35 PM
Cut from right to left? LocoMojo Programming 26 03-01-2007 05:14 PM
cut question krock923 Programming 1 10-19-2005 04:03 PM
Ok, maybe I'm not cut out for linux... goosegg Linux - Newbie 5 09-01-2003 03:43 PM
cut-problem dahljor Programming 2 07-08-2003 12:58 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 12:56 AM.

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
Open Source Consulting | Domain Registration