LinuxQuestions.org
Register a domain and help support LQ
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 01-11-2013, 11:08 AM   #1
antriksh
Member
 
Registered: Aug 2010
Location: In my world
Distribution: Oracle Linux 6
Posts: 81

Rep: Reputation: 0
Question How to truncate a command output?


I am writing a script which kills the terminal from where it has been called if doesn't satisfy a particular condition say user is not root.

I am going to use command
Code:
pgrep -t pts/1
which will give me the process id of the shell pts/1. now pts/1 is variable. Terminal name we can obtain from tty command but tty gives output like /dev/pts/1. Now i need to extract the pts/1 from this output.

can anyone help me extracting pts/1 from /dev/pts/1?
 
Old 01-11-2013, 11:10 AM   #2
schneidz
LQ Guru
 
Registered: May 2005
Location: boston, usa
Distribution: fc-15/ fc-20-live-usb/ aix
Posts: 5,051

Rep: Reputation: 852Reputation: 852Reputation: 852Reputation: 852Reputation: 852Reputation: 852Reputation: 852
cut mite help but awk mite be better (also sed mite work too).
many ways to skin this cat.

Last edited by schneidz; 01-11-2013 at 11:25 AM.
 
Old 01-11-2013, 11:13 AM   #3
antriksh
Member
 
Registered: Aug 2010
Location: In my world
Distribution: Oracle Linux 6
Posts: 81

Original Poster
Rep: Reputation: 0
Hey schneidz..thanks for your reply. I am new to scripting.can you give me the command for that?
 
Old 01-11-2013, 11:21 AM   #4
shivaa
Senior Member
 
Registered: Jul 2012
Location: Grenoble, Fr.
Distribution: Sun Solaris, RHEL, Ubuntu, Debian 6.0
Posts: 1,800
Blog Entries: 4

Rep: Reputation: 286Reputation: 286Reputation: 286
Correct command can be given after seeing the sample output, but in the meantime, you can try with following,

Let's say, output of command is simply, /dev/pts/1, then invoke:-
Code:
cmd_output | sed 's/\/dev//'
OR, you can,
Code:
cmd_output | gawk '{gsub(/\/dev/,"",$0); print $0}'
 
Old 01-11-2013, 12:08 PM   #5
antriksh
Member
 
Registered: Aug 2010
Location: In my world
Distribution: Oracle Linux 6
Posts: 81

Original Poster
Rep: Reputation: 0
Thumbs up

Code:
x=/dev/pts/1
echo "${x#/dev/}"
this worked.
 
Old 01-11-2013, 12:47 PM   #6
shivaa
Senior Member
 
Registered: Jul 2012
Location: Grenoble, Fr.
Distribution: Sun Solaris, RHEL, Ubuntu, Debian 6.0
Posts: 1,800
Blog Entries: 4

Rep: Reputation: 286Reputation: 286Reputation: 286
Quote:
Originally Posted by antriksh View Post
Code:
x=/dev/pts/1
echo "${x#/dev/}"
I didn't understnad what was the actual problem and what did you want? What's need of first saving the command output i.e. /dev/pts/1 in x and then displaying it using an echo statment. Instead, you could simply filter the command output with either sed or awk like what's suggested above.
Code:
cmd_output | gawk '{gsub(/\/dev/,"",$0); print $0}'
cmd_output | sed 's/\/dev//'
 
Old 01-11-2013, 01:20 PM   #7
antriksh
Member
 
Registered: Aug 2010
Location: In my world
Distribution: Oracle Linux 6
Posts: 81

Original Poster
Rep: Reputation: 0
@shivaa:
Code:
tty | sed 's/\/dev//'
this one is giving me output as /pts/0 while i wanted was pts/0.
 
Old 01-11-2013, 01:29 PM   #8
shivaa
Senior Member
 
Registered: Jul 2012
Location: Grenoble, Fr.
Distribution: Sun Solaris, RHEL, Ubuntu, Debian 6.0
Posts: 1,800
Blog Entries: 4

Rep: Reputation: 286Reputation: 286Reputation: 286
Quote:
Originally Posted by antriksh View Post
@shivaa:
Code:
tty | sed 's/\/dev//'
this one is giving me output as /pts/0 while i wanted was pts/0.
In that case, just modify the cmd, as:
Code:
tty | sed -e 's/\/dev\///'
OR,
tty | gawk '{gsub(/\/dev\//,"",$0); print $0}'
 
Old 01-11-2013, 01:40 PM   #9
antriksh
Member
 
Registered: Aug 2010
Location: In my world
Distribution: Oracle Linux 6
Posts: 81

Original Poster
Rep: Reputation: 0
Thanks shivaa..last one also worked..

Last edited by antriksh; 01-11-2013 at 01:41 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
[SOLVED] How to truncate mailq command output shivaa Linux - Newbie 1 09-20-2012 02:45 PM
Requests to my ISP server are output in the middle of output by any command I run. stf92 Slackware 2 07-10-2012 09:38 AM
[SOLVED] truncate shell command puth Programming 5 03-18-2010 11:49 AM
How to redirect the output of exec command output in perl? sudhap85 Linux - Newbie 3 08-28-2009 04:35 PM
truncate command butthead Linux - General 1 05-14-2002 04:40 AM


All times are GMT -5. The time now is 05:20 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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration