LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (http://www.linuxquestions.org/questions/programming-9/)
-   -   Cut Help (http://www.linuxquestions.org/questions/programming-9/cut-help-604731/)

metallica1973 12-05-2007 12:49 PM

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

schneidz 12-05-2007 01:12 PM

i dunno' if there is a simpler way but this works:
Code:

echo S00CBBF.aa.bb.ccc.edu | rev | cut -d . -f 3- | rev

pixellany 12-05-2007 01:12 PM

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.

metallica1973 12-05-2007 01:13 PM

I am using Sun Solaris 10. Is that command 'rev' still valid?

schneidz 12-05-2007 01:32 PM

Quote:

Originally Posted by metallica1973 (Post 2981231)
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]));
}


ilikejam 12-05-2007 02:20 PM

awk for the win.
Code:

awk -F'.' '{for (i=1;i<=NF-2;i++) {if (i<NF-2) printf $i"."; else print $i}}'
Dave

chrism01 12-05-2007 04:50 PM

echo S00CBBF.aa.bb.ccc.edu |cut -d'.' -f1-3

gives
S00CBBF.aa.bb

too easy ;) and shorter / clearer than awk

ilikejam 12-05-2007 04:53 PM

Quote:

Originally Posted by chrism01 (Post 2981417)
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?

unSpawn 12-05-2007 05:21 PM

Quote:

Originally Posted by ilikejam (Post 2981423)
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]}"

ilikejam 12-05-2007 05:26 PM

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

ghostdog74 12-05-2007 05:28 PM

Code:

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

unSpawn 12-05-2007 05:38 PM

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// /.}

chrism01 12-05-2007 06:26 PM

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??

metallica1973 12-05-2007 07:04 PM

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!

ilikejam 12-05-2007 07:20 PM

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


All times are GMT -5. The time now is 05:29 AM.