nixCraft Linux Forum

nixCraft

Linux Tech Support Forum

cut script

This is a discussion on cut script within the Shell scripting forums, part of the Development/Scripting category; Hi all, I want to write a script that will use the o/p from another utility and act upon a ...


Go Back   nixCraft Linux Forum > Development/Scripting > Shell scripting

Register FAQ Members List Calendar Forgotten your password? Mark Forums Read
  #1 (permalink)  
Old 08-09-2005, 05:08 PM
Member
User
 
Join Date: Jul 2005
Posts: 85
Rep Power: 0
ricc
Default cut script

Hi all,
I want to write a script that will use the o/p from another utility and act upon a certain field from the third line onwards

Lets suppose, the who command o/p contained a few unwanted lines in the beginning. Like this

#who
this is the output of the who commnd
the users cuttently logged in are
root pts/0 Aug 9 14:55 (172.17.0.1)
root pts/1 Aug 9 15:58 (172.17.0.1)

Now suppose, I want to act upon the fist field, i.e. (usernames in this case)

If I write the script as

#for i in `who |cut -f1 -d" "`; do echo "these are the users logged on : $i ";done

This will also output --this and the

But I don't want "this and the". So what shall I do so that it escapes the 1st and 2nd lines and starts acting on the 1st field from the 3rd line onwards till the last one.

Can any one please tell me how to do this.

Thanks in advance for any help.

rc
Reply With Quote
Sponsored Links
  #2 (permalink)  
Old 08-09-2005, 05:18 PM
rockdalinux's Avatar
Contributors
User
 
Join Date: May 2005
Location: Bangalore
My distro: RHEL, HP-UX, Solaris, FreeBSD, Ubuntu
Posts: 581
Rep Power: 7
rockdalinux is a jewel in the rough rockdalinux is a jewel in the rough rockdalinux is a jewel in the rough rockdalinux is a jewel in the rough
Default

Hmm

Quite esay I guess

Code:
for i in $(who |cut -f1 -d" "|grep -vE "(this|the)" ); do echo "these are the users logged on : $i ";done
grep -v will inform not to display the word this or the
__________________
Rocky Jr.
You may have my body & soul, but you will never touch my pride!

If you have knowledge, let others light their candles at it.

Certified to work on HP-UX / Sun Solaris / RedHat
Reply With Quote
  #3 (permalink)  
Old 08-10-2005, 01:09 PM
Member
User
 
Join Date: Jul 2005
Posts: 85
Rep Power: 0
ricc
Default

Thank you Rocky,

I knew you would certainly reply. Thank you for the solution. I also want to know what if the o/p of who is like this
Code:
  
# who
these are the users who are currently logged in
-------------------------------------------------------
ram      :0           Aug 10 11:27 (console)
ram     pts/0        Aug 10 11:57 (192.168.2.50)
Now, the 1st field after cutting contains

these
-------------------------------------------------------
ram
ram

Now tell me how do I isolate the
-------------------------------------------------------

I depend on you rocky. I believe you can help me.

Cheers

rc
Reply With Quote
  #4 (permalink)  
Old 08-10-2005, 04:50 PM
rockdalinux's Avatar
Contributors
User
 
Join Date: May 2005
Location: Bangalore
My distro: RHEL, HP-UX, Solaris, FreeBSD, Ubuntu
Posts: 581
Rep Power: 7
rockdalinux is a jewel in the rough rockdalinux is a jewel in the rough rockdalinux is a jewel in the rough rockdalinux is a jewel in the rough
Default

It is same as above, here is what you need to first one:
Code:
for i in $(who |cut -f1 -d" "|grep -vE "(^-|this|the)" ); do echo "these are the users logged on : $i ";done
__________________
Rocky Jr.
You may have my body & soul, but you will never touch my pride!

If you have knowledge, let others light their candles at it.

Certified to work on HP-UX / Sun Solaris / RedHat
Reply With Quote
  #5 (permalink)  
Old 08-11-2005, 10:59 AM
Member
User
 
Join Date: Jul 2005
Posts: 85
Rep Power: 0
ricc
Default

Thank you Rocky. You are just great. I knew I can depend on you.

Long live Shell scripting and long live Rocky.

rc
Reply With Quote
Reply

Bookmarks


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)

 
Thread Tools
Display Modes

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 On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


All times are GMT +5.5. The time now is 02:04 AM.


Powered by vBulletin® Version 3.7.4 - Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.2.0

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36