Use of "CUT" command.

Hi,

I have 3 fields in a file. For example I have them like this:
1,Santosh, 24

I want to have these 3 values in 3 different variables. How can I do it.

Thanks in advance

Using awk:

first=awk -F "," '{print $1}'
second=awk -F "," '{print $2}'
third=awk -F "," '{print $3}'

Using shell:

#!/usr/bin/ksh

oldIFS=$IFS
IFS=$IFS,
while read first second third; do
        echo $first $second $third
done < test
IFS=$oldIFS
# cat test
1,abc,24
2,def,25
3,pqr,40
# ./test.sh
1 abc 24
2 def 25
3 pqr 40

Hi,

Since my file that has got input values in the format "1,Mani,23" and I dont know how many such inputs are going to be there.I wrote the following script to print How many however values were there in the file:

while read line
do
count=1
more values | awk '{print $count}'
count=`expr $count + 1`
done < values

It gives me an error as following:
awk: Field $() is not correct.

How can print all the values in the file field by field ie...

if inputs are like this
1,Mani,23
2,San,24,
3,Tom,25
..
..
and so on. How can I print the first field,2nd field and the third field seperately using awk in a loop

Thanx in advance

can u give a sample output as well

Hi,
The format of the output should be:
1
2
3

Mani
San
Tom

23
24
25

You cannot use shell variables inside an awk command directly. Read up on awk programming to know how to do this. And since you are so keen on using awk to read the variables, try this:

#!/usr/bin/ksh

awk -F',' '{print $1," ",$2," ",$3}' test|while read first second third; do
   echo $first $second $third
done

Ok, my previous reply was a bit early - didn't get to see your sample output. Try this:

awk -F, '{print $1}' test
awk -F, '{print $2}' test
awk -F, '{print $3}' test
# cat test
1,abc,24
2,def,25
3,pqr,40

in this case file needs to be read and parsed three times,

just a single read and single parse would do,

# !/usr/bin/ksh

first=""
second=""
third=""

while read line
do
first=$first`echo $line | cut -f1 -d","`","
second=$second`echo $line | cut -f2 -d","`","
third=$third`echo $line | cut -f3 -d","`","
done < test

echo $first | tr ',' '\n'
echo $second | tr ',' '\n'
echo $third | tr ',' '\n'

exit 0

$ p4 users | grep tkdas
tkdas <tkdas@myemail.com> (Tusar Kanti Das) accessed 2005/12/09

$ p4 users | grep tkdas | awk '{ print $1 }'
tkdas
.
.
.
and so on ...
And now I want to get the last filed but I dont want to get it like this

$ p4 users | grep tkdas | awk '{ print $7 }'
2005/12/09

Because intermediate fields are not constant always and thats why it may be $6 or $7 or $8 sometime.

But one thing I am sure is that the date filed is the last filed. Any idea how to catch the filed?

Thanks a lot to all in advance
C Saha

if u are sure of the last field and assuming ur last field is delimited by the previous field by a space

p4 users | grep tkdas | sed 's/^.* //g'
p4 users | grep tkdas | awk '{ printf $NF }'

or better yet with just sed

p4 users | sed -n -e 's_^tkdas.* __p'

matrixmadhan & vino - both of you are great !!! Thanks a lot

BTW - What does $NF stands for?

======================================

I am very much interested to learn sed & awk deeply. I always found myself weak in awk & sed. Can you guys refer some good books (available in net in doc/pdf version)? That will be really helpful...

Thanks once again
C Saha

UNIX Forums maintains a site where tutorial links are hosted.

Some Sites Recommended by Our Users

And yet another tutorial is the O'Reilly's bookshelf

Now here I am looking for the filed $8 ( example: asuhan@ixload_build_rh9_linux2_4_asuhan in the first output line below ) but it may be $7, $8 ... $10 anywhere.

Only think I am sure is that - it will come after the string "by" - Please help me to grab it ... Any idea how ???

$ p4 opened -a | grep *locked* 

//ixweb/main/protocols/mgcp/pcpu/mgcpServer/scripts/stop.sh#4 - edit change 118970 (text) by asuhan@ixload_build_rh9_linux2_4_asuhan *locked*
//packages/av_codecs/main/g7231/g7231.vcproj#2 - edit default change (text) by rjongbloed@rjongbloed-ixmobile-vt-main *locked*
//packages/av_codecs/main/gsm_amr/gsm_amr.vcproj#2 - edit default change (text) by rjongbloed@rjongbloed-ixmobile-vt-main *locked*
//packages/av_codecs/main/h26x/h26x.vcproj#2 - edit default change (text) by rjongbloed@rjongbloed-ixmobile-vt-main *locked*
//packages/h324stack/main/common/commapi/src/win32/BtISDN/btisdn.vcproj#3 - edit default change (text) by rjongbloed@rjongbloed-ixmobile-vt-main *locked*
//packages/h324stack/main/common/commapi/src/win32/BtISDN/trilliumwrapper.vcproj#2 - edit default change (text) by rjongbloed@rjongbloed-ixmobile-vt-main *locked*
//packages/h324stack/main/common/commapi/src/win32/commapi.vcproj#2 - edit default change (text) by rjongbloed@rjongbloed-ixmobile-vt-main *locked*
//packages/h324stack/main/common/coreapi/src/win32/coreapi.vcproj#3 - edit default change (text) by rjongbloed@rjongbloed-ixmobile-vt-main *locked*
//packages/h324stack/main/common/mediadevapi/src/mediadevapi.vcproj#2 - edit default change (text) by rjongbloed@rjongbloed-ixmobile-vt-main *locked*
//packages/h324stack/main/common/tools/DNLicense/src/dnlicense.vcproj#3 - edit default change (text) by rjongbloed@rjongbloed-ixmobile-vt-main *locked*
//packages/h324stack/main/h32x/src/answerfast/answerfast.vcproj#2 - edit default change (text) by rjongbloed@rjongbloed-ixmobile-vt-main *locked*
//packages/h324stack/main/h32x/src/h323.vcproj#2 - edit default change (text) by rjongbloed@rjongbloed-ixmobile-vt-main *locked*
//packages/h324stack/main/h32x/src/h324.vcproj#3 - edit default change (text) by rjongbloed@rjongbloed-ixmobile-vt-main *locked*
//packages/h324stack/main/h32x/src/h32x.vcproj#3 - edit default change (text) by rjongbloed@rjongbloed-ixmobile-vt-main *locked*
//packages/h324stack/main/h32x/src/h32xapp.vcproj#3 - edit default change (text) by rjongbloed@rjongbloed-ixmobile-vt-main *locked*
//protocols/4.10/shared/include/IxTCLProtocolAPI.h#1 - edit change 86826 (text) by jholland@jake-win2 *locked*
//protocols/4.10/shared/include/ProtocolChassisCfgAPI.h#1 - edit change 86826 (text) by jholland@jake-win2 *locked*
//protocols/4.10/shared/include/ProtocolChassisSMAPI.h#1 - edit change 86826 (text) by jholland@jake-win2 *locked*
//protocols/4.10/shared/include/ProtocolFrameAPI.h#1 - edit change 86826 (text) by jholland@jake-win2 *locked*
//protocols/4.10/shared/include/ProtocolGUIAPI.h#3 - edit change 86826 (text) by jholland@jake-win2 *locked*
//protocols/4.10/shared/include/ProtocolStatAPI.h#1 - edit change 86826 (text) by jholland@jake-win2 *locked*
//protocols/4.10/shared/include/ProtocolsAPI.h#1 - edit change 86826 (text) by jholland@jake-win2 *locked*
//protocols/main/shared/include/IxTCLProtocolAPI.h#1 - edit change 86826 (text) by jholland@jake-win2 *locked*
//protocols/main/shared/include/ProtocolChassisCfgAPI.h#2 - edit change 86826 (text) by jholland@jake-win2 *locked*
//protocols/main/shared/include/ProtocolChassisSMAPI.h#1 - edit change 86826 (text) by jholland@jake-win2 *locked*
//protocols/main/shared/include/ProtocolFrameAPI.h#1 - edit change 86826 (text) by jholland@jake-win2 *locked*
//protocols/main/shared/include/ProtocolGUIAPI.h#3 - edit change 86826 (text) by jholland@jake-win2 *locked*
//protocols/main/shared/include/ProtocolStatAPI.h#1 - edit change 86826 (text) by jholland@jake-win2 *locked*
//protocols/main/shared/include/ProtocolsAPI.h#2 - edit change 86826 (text) by jholland@jake-win2 *locked*
//protocols/regression-suites/test-cases/ospf/ethernet/test.ospf_drbdr_adj.tcl#1 - add change 67377 (text) by ysun@ysun-linux1 *locked*
//protocols/regression-suites/test-cases/pim/bugs/ethernet/ethernet-pimsm-bug-39721.log#1 - add change 130055 (text) by adhar@adhar_testharness1 *locked*
//protocols/regression-suites/test-cases/pim/bugs/ethernet/temp.ethernet-pimsm-bug-39721.1.tcl#1 - add change 130055 (text) by adhar@adhar_testharness1 *locked*
//protocols/regression-suites/test-cases/pim/bugs/ethernet/temp.ethernet-pimsm-bug-39721.tcl#1 - add change 130055 (text) by adhar@adhar_testharness1 *locked*
//protocols/regression-suites/test-cases/stp/ethernet/test.update-topology-change.tcl#1 - edit change 130055 (xtext) by adhar@adhar_testharness1 *locked*

p4 opened -a | sed -n -e 's_.*by \(.*\) .locked. *$_\1_p'

wow !!! amazing .. thanks Vino

another solution...as you said date comes last....and format is 2006/02/06
then

P4 users | grep tkdas | awk '{ print substr($0,length($0)-9,10) }' 

Thank you so much ...

$ p4 users | grep "\<tkdas\>"
tkdas <tkdas@myemail.com> (Tusar Kanti Das) accessed 2005/12/09

$ p4 users | grep "\<li\>"
li <tli@myemail.com> (Thomas Li) accessed 2006/02/08

Buddy - any idea, how can I grab the name filed above case, as qith awk $3 and $4 will not give the name. But the name always comes within the bracket, like this "(Name XXX)" .

Show us what you tried.