Extract particular number from the command output


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Extract particular number from the command output
# 1  
Old 07-02-2012
Extract particular number from the command output

Hi Folks,

I want to use particular number as a variable output..Please find the below for scenario...

Example 1:-

Code:
Below output i want to use secondary group 9003 as a variable output
$ id -a |awk -NF '{print $3}'
groups=99(local),9003(testadmin)

Else

Code:
I want to use 2006 as a group ID 
$ id -a oracle |awk -NF '{print $3}'
groups=2006(dba)
$


Note:I don't no whether it have secondary ID present or not.If secondary ID present i wanna use or i have to use just group ID...Above group ID number may vary depends upon account.

Thanks,
Susi>SSmilie
# 2  
Old 07-02-2012
How about using the -G option of id:

Code:
$ id -G | awk '{print $NF}'
9003
$ id -G oracle | awk '{print $NF}'
2006

# 3  
Old 07-02-2012
Hi Chubler...

I can't directly use oracle because i am going to create user account.It may vary oracle name...

I will use as a variable in that place and find the details below.

Code:
SIMILOGIN_Profile=`grep -i $similer_profile /etc/passwd|awk -F: '{print $1}'`

secondory_group=`id -a $SIMILOGIN_Profile |awk -NF '{print $3}'`

Thanks,
Susi.S
# 4  
Old 07-02-2012
Code:
secondory_group=`id -G $SIMILOGIN_Profile |awk '{print $NF}'`

# 5  
Old 07-02-2012
id -G will not works in solaris...

Code:
$ id -G
id: illegal option -- G
Usage: id [-ap] [user]
$ uname -a
SunOS 5.10 Generic_147440-06 sun4u sparc SUNW,Sun-Fire-280R
$

Thanks,
Susi.S
# 6  
Old 07-02-2012
How about:

Code:
secondory_group=`id -a $SIMILOGIN_Profile | awk '{print k[split($3,k,"[ ,=(]")-1]}'`

or
Code:
secondory_group=`id -a $SIMILOGIN_Profile | awk '{f=split($3,k,"[ ,=(]"); print k[f-1]}'`

or
Code:
secondory_group=`id -a $SIMILOGIN_Profile | awk -F= '{print $4}' | awk -F'[,(]' '{print $(NF-1)}'`

You may also need to use nawk instead of awk

Last edited by Chubler_XL; 07-02-2012 at 10:38 PM..
# 7  
Old 07-02-2012
Hi Chubler,

I don't no the reason..One server is working and another server is not working....

Code:
$ id -a oracle
uid=7003(oracle) gid=2002(dba) groups=2002(dba)
$ id -a oracle | awk -F= '{print $4}' | awk -F'[,(]' '{print $(NF-1)}'
2002
$ uname -a
Linux test1 2.6.9-67.ELsmp #1 SMP Wed Nov 7 13:56:44 EST 2007 x86_64 x86_64 x86_64 GNU/Linux
$

$ id -a oracle
uid=2006(oracle) gid=2006(dba) groups=2006(dba)
$ id -a oracle | awk -F= '{print $4}' | awk -F'[,(]' '{print $(NF-1)}'
2006(dba)
$ uname -a
SunOS test2 5.10 Generic_147440-06 sun4u sparc SUNW,Sun-Fire-280R
$

Thanks,
Susi.S
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Extract number from string.

Hi I am on Sun os. I have data in the below format and I need to grab the number out from the string. O/p needed: ---------- Post updated at 12:39 PM ---------- Previous update was at 12:32 PM ---------- I tried this but I am getting . at the front (14 Replies)
Discussion started by: dsravanam
14 Replies

2. Shell Programming and Scripting

Insert title as output of command to appended file if no output from command

I am using UNIX to create a script on our system. I have setup my commands to append their output to an outage file. However, some of the commands return no output and so I would like something to take their place. What I need The following command is placed at the prompt: TICLI... (4 Replies)
Discussion started by: jbrass
4 Replies

3. Shell Programming and Scripting

sed command to replace a line in a file using line number from the output of a pipe.

Sed command to replace a line in a file using line number from the output of a pipe. Is it possible to replace a whole line piped from someother command into a file at paritcular line... here is some basic execution flow.. the line number is 412 lineNo=412 Now i have a line... (1 Reply)
Discussion started by: vivek d r
1 Replies

4. Shell Programming and Scripting

Extract the uptime from the output of the uptime command

Hi! I want to extract the uptime from the output of the uptime command. The output: 11:53 up 3:02, 2 users, load averages: 0,32 0,34 0,43 I just need the "3:02" part. How can I do this? Dirk (6 Replies)
Discussion started by: Dirk Einecke
6 Replies

5. Shell Programming and Scripting

Extract Number

I am trying to extract the numbers from the strings. Lakers win 80% of the games 24 numbered Kobe scores 90% from free throw line Chances of Lakers winning championship is 100% I have data like this. and am looking to extract the % 80% 90% 100% (6 Replies)
Discussion started by: grajp002
6 Replies

6. Shell Programming and Scripting

Extract number that comes after a given string

Hi! I have a file that contains non-regular strings like: SCSGTI:N="$4,0,1,4,34622991111-->RemoteSPC: 1111", NWID=1; SCSGTI:N="$4,0,1,4,34622991211-->RemoteSPC: 1211", NWID=1; SCSGTI:N="$4,0,1,4,*-->RemoteSPC: 2112,Sec:RemoteSPC: 2212", NWID=1; SCSGTI:N="$4,10,1,4,34622999213-->RemoteSPC:... (4 Replies)
Discussion started by: Flavius
4 Replies

7. Shell Programming and Scripting

Extract header from top command output

hi, I want to extract and save the cpu(s) information from top command output, but individual cpu statistics separately on a multi-processor machine. In command line, top will show this statistics when we press the switch "1". any ideas? thanks, meharo (3 Replies)
Discussion started by: meharo
3 Replies

8. Shell Programming and Scripting

extract columns from command output

I need to extract information (for example, file owner, directory path, etc). However, the code below does not work? What is wrong? find /usr/local/www/apache22/data/dev/chown_test -ls for i in * do cut -f 1,2 "$i" | echo done (3 Replies)
Discussion started by: montana24
3 Replies

9. UNIX for Dummies Questions & Answers

Command display output on console and simultaneously save the command and its output

Hi folks, Please advise which command/command line shall I run; 1) to display the command and its output on console 2) simultaneous to save the command and its output on a file I tried tee command as follows; $ ps aux | grep mysql | tee /path/to/output.txt It displayed the... (7 Replies)
Discussion started by: satimis
7 Replies

10. Shell Programming and Scripting

how to suppress list number from history command output

i run history command and I want to eliminate the list number. So far this perl script works as long as the list is a exact 3 character long. cat dd | perl -pe 's,\d{3},,' 70 export JAVA_HOME=. 81 export JAVA_HOME=. 82 export JAVA_HOME=`pwd` export JAVA_HOME=`pwd` ... (1 Reply)
Discussion started by: soemac
1 Replies
Login or Register to Ask a Question