1834020 Members
3136 Online
110063 Solutions
New Discussion

separate out the column

 
hanyyu1
Advisor

separate out the column

I know "awk -F:" will separate out the column by ":" , now if I what to separate out the column by space also , what can I do ?


for example :

#ps -ef |grep telnet
root 10159 702 0 15:45 ? 00:00:00 in.telnetd: 192.168.0.1

how to separate out the column so that the column as below,

column 1 --> root
column 2 --> 10159
column 3 --> 702
column 4 --> 0
column 5 --> 15
column 6 --> 45
column 7 --> ?
column 8 --> 00
column 9 --> 00
column 10 --> 00
column 11 --> in.telnetd
column 12 --> 192.168.0.1

|awk {print $12} then output the result is 192.168.0.1
11 REPLIES 11
RAC_1
Honored Contributor

Re: separate out the column

ps -ef |grep telnet | awk -f " " '{print $whatyouwant}'
There is no substitute to HARDWORK
Arunvijai_4
Honored Contributor

Re: separate out the column

ps -ef|grep -i telnet |awk 'BEGIN { FS = "[ \t\n]+" }{ print $x }'

-Arun
"A ship in the harbor is safe, but that is not what ships are built for"
hanyyu1
Advisor

Re: separate out the column

thx replies ,

may be my question is not clear , what I want is to separate the column , to let the column is separate by : and space at the same time like my above example , is it possible ? thx
RAC_1
Honored Contributor

Re: separate out the column

Seperate it by space first and then by :
ps -ef|grep telnet | awk -F " " '{print $whatyouwant}'|awk -F : ' {print $whatyouwant}'
There is no substitute to HARDWORK
Muthukumar_5
Honored Contributor

Re: separate out the column

First split with -F" " that is default one. Then split with split() in awk as,

echo "root 10159 702 0 15:45 ? 00:00:00 in.telnetd: 192.168.0.1" | awk '{ split($5,a,":");split($7,b,":");print $1,$2,$3,$4,a[1],a[2],$6,b[1],b[2],b[3],$8,$9,$10,$11,$12; }'

-Muthu
Easy to suggest when don't know about the problem!
Muthukumar_5
Honored Contributor

Re: separate out the column

Another way as,

# ps -ef | grep 'telnet' | perl -an -F/:/ -e 'BEGIN{$,=" ";}{print @F;}'

-Muthu
Easy to suggest when don't know about the problem!
Arunvijai_4
Honored Contributor

Re: separate out the column

ps -ef | grep "telnet"|awk -F " " '{print $x }'|awk -F : ' {print $x}'

-Arun
"A ship in the harbor is safe, but that is not what ships are built for"
Rodney Hills
Honored Contributor

Re: separate out the column

The -F option of awk allows for a regular expression, so you could use-

ps -ef | grep telnet | awk -F"[ :]" '{print $12}'

HTH

-- Rod Hills
There be dragons...
Peter Godron
Honored Contributor

Re: separate out the column

echo "root 10159 702 0 15:45 ? 00:00:00 in.telnetd: 192.168.0.1" | awk '{ print $1,"\n",$2,"\n",$3,"\n",$4,"\n",$5,"\n",$6,"\n",$7,"
\n",$8,"\n",$9,"\n",$10,"\n",$11,"\n",$12; }'
hanyyu1
Advisor

Re: separate out the column

thx replies,

I tried the below script , but it will separate the column by space THEN seperate by : , if I want it will seperate by both space and : , what can I do ? thx


ps -ef | grep "telnet"|awk -F " " '{print $x }'|awk -F : ' {print $x}'

Muthukumar_5
Honored Contributor

Re: separate out the column

You can not use two delimiters with -F option awk at one time. Select one delimiter and split it again with the next delimiter.

-Muthu

Easy to suggest when don't know about the problem!