how to extract a data from a column?

Hi All,

Consider the below column, say this is the 4th column in a file

PROV_STATS:::919900546978::Nokia 6600
PROV_STATS:::919900546978::Nokia 6600
PROV_STATS:::919900546978::Nokia 6600

I wanted to extract only 919900546978 from the 4 th cloumn using unix scripting?

Kindly help

cut -d: -f4 file

thanks era...here are few more questions
2008-06-16 PROV_STATS:::919900546978::Nokia 6600: a e
2008-06-16 PROV_STATS:::919900546978::Nokia 6600: b f
2008-06-16 PROV_STATS:::919900546978::Nokia 6600: c g

the above file has 4 columns i want the output to be seperated with "|"

|2008-06-16|PROV_STATS|919900546978|Nokia|6600|a|e|

Please suggest

If you simply want to replace all runs of spaces and colons with a pipe character and add it at beginning and end of line, something like

sed 's/^/|/;s/$/|/;s/[: ][: ]*/|/g' file >newfile

It's not entirely obvious from your example how to get from input to output, but google for similar problems; there are lots of threads about this type of problem.

awk 'BEGIN{FS="[ :]+"}{$1=$1}1' OFS="|" file

There's still the separators at beginning and end of line, but other than that, the awk and sed solutions are basically identical.

I'm new to unix , Im here to learn from the experts , Here is my doubt
this is a sinlge line.

2008-05-24 04:11:57,008 [http-8080-Processor23] INFO com.kodiak.agc.ProvisionAGC - PROV_STATS:::919740012846::Nokia 6600::::[Ljava.lang.String;@9cbd4b

I want the output to be like below format, i tried that above things but im not able to extract the data's wht i needed ,

|2008-05-24|04:11:57|919740012846|Nokia 6600|

Please help!!!

Are you going to throw in more hidden requirements if we solve this one?

awk '{ gsub(/,.*/, "", $2); split ($0, a, /:+/); print "|" $1 "|" $2 "|" a[4] "|" a[5] "|" }' file >newfile

There are very old awk versions which might not be able to work with this script; if it doesn't work, see if you have nawk or mawk or gawk or XPG4 awk on your system.

(Actually with my copy of mawk, the last field was wrong when I set OFS="|" so this script is a bit of a workaround.)

Suppose data is available in file "abc.txt" with several lines in the same format as the line you have mentioned above . Loop works fine unless you have "!" in your data .

for i in `cat abc.txt | sed 's: :!:g' `
do
line=`echo $i | sed 's:!: :g'`
d=`echo $line|cut -d" " -f1`
t=`echo $line |cut -d"," -f1 | cut -d" " -f2`
num=`echo $line|cut -d":" -f6`
model=`echo $line | cut -d":" -f8`
echo "|$d|$t|$num|$model|"
done

|2008-05-24|04:11:57|919740012846|Nokia 6600|

this is not the best way ...........but it works :wink: