Help with `Who am i` and `cut`


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Help with `Who am i` and `cut`
# 1  
Old 09-18-2008
Help with `Who am i` and `cut`

In this command I get:
> who am i
ops pts/69 Sep 18 11:14 (spfea29)

In this command I get:
> export IP_ADDR=`who am i|cut -d\(-f2|cut -d\) -f1`
> echo $IP_ADDR
spfea29

Can someone walk me through that `cut` code where its retrieving 'spfea29'.
I would think there would be an easier way of getting this. Thanks.
# 2  
Old 09-18-2008
Code:
cut -d\(    this marks the delimiter as (
-f2           this indicates that of the fields that are separated by the (, grab the second field, and discard the remaining

|              once this is done, take the results of the previous cut and prepare to do more magic to it.

cut -d\)    this marks the delimiter as )
-f1           this indicates that of the fields that are separated by the ), grab the FIRST field, and discard the remaining


Last edited by avronius; 09-18-2008 at 12:47 PM.. Reason: Replace word "previous" in last line with FIRST
# 3  
Old 09-18-2008
Tools

Quote:
> who am i
ops pts/69 Sep 18 11:14 (spfea29)

In this command I get:
> export IP_ADDR=`who am i|cut -d\(-f2|cut -d\) -f1`
> echo $IP_ADDR
spfea29
who am i returns the string you see
cut using a delimiter of (
note the \ before the ( to tell unix that the character should not be interpreted
the -f2 says to take the 2nd field; what is after the (
then using a delimiter of )
again utilizing the \ so as not to interpret the )
this time saying take field 1; everything before the )

Here is another way of doing it
IPX=`who am i | cut -d"(" -f2 | cut -d")" -f1`
or
IPX=$(who am i | cut -d"(" -f2 | cut -d")" -f1)
# 4  
Old 09-18-2008
From cut's man page:
Code:
       -d, --delimiter=DELIM
              use DELIM instead of TAB for field delimiter

       -f, --fields=LIST
              output only these fields;  also print any line that contains  no
              delimiter character, unless the -s option is specified

You have three operations going on here, separated by the pipe. 'who am i' is piped to 'cut -d\( -f2' which breaks the string on the (, and pulls the 2nd field. That is piped to 'cut -d\) -f1' which breaks the string on the ) and pulls out the first field.

Code:
1) who am i                  produces    ops    pts/69      Sep 18 11:14     (spfea29)     
2) cut -d\( -f2              produces    spfea29)     
3) cut -d\) -f1              produces    spfea29

# 5  
Old 09-18-2008
Thanks guys, great explanation. Did not think you would have to be so specific in pointing out everything to Unix. I would think after the ( and telling it to discard the remaining, you wouldnt have to code the ) part. As for the \, I was wondering what that meant. What would happen if you didnt include the \ ? :/
# 6  
Old 09-18-2008
Try running it at the command line and find out!

The \ is a quote - tells the shell to literally use the next character, instead of using it for whatever it would normally use that character (the parenthesis) for.
# 7  
Old 09-18-2008
There is another way, but I can't say it is easier to understand, but it uses only one pipe, so one fork less.

Here it is,

IPX=` who am i | sed -e 's/^.*(\(.*\)).*/\1/'

The trick is to use sed and subsitute everything until the right ( and after the ) and replace it by the text in between.
 
Login or Register to Ask a Question

Previous Thread | Next Thread

3 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Using :<<cut / cut to comment out block of bash script

I am using : << cut / cut to comment out block of code. Works fine on few lines of script, then it gives me this cryptic error when I try to comment out about 80 lines. The "warning " is at last line of script. done < results 169 echo "END read all positioning parameters" 170... (8 Replies)
Discussion started by: annacreek
8 Replies

2. UNIX for Beginners Questions & Answers

Cut command: can't make it cut fields

I'm a complete beginner in UNIX (and not a computer science student either), just undergoing a tutoring course. Trying to replicate the instructions on my own I directed output of the ls listing command (lists all files of my home directory ) to My_dir.tsv file (see the screenshot) to make use of... (9 Replies)
Discussion started by: scrutinizerix
9 Replies

3. Shell Programming and Scripting

Cut Command error cut: Bad range

Hi Can anyone what I am doing wrong while using cut command. for f in *.log do logfilename=$f Log "Log file Name: $logfilename" logfile1=`basename $logfilename .log` flength=${#logfile1} Log "file length $flength" from_length=$(($flength - 15)) Log "from... (2 Replies)
Discussion started by: dgmm
2 Replies
Login or Register to Ask a Question