1847115 Members
5717 Online
110263 Solutions
New Discussion

Re: Cut first column

 
ivychung2
Frequent Advisor

Cut first column

I have below file , if I want to cut the first column of the file ,

vi test.txt
111:dfsaf
222:fdsfdsa
333:fsafas
444:fdsafdsaf

the output as below,
111
222
333
444

what can I do ? thx.
6 REPLIES 6
Patrice Le Guyader
Respected Contributor

Re: Cut first column

hi,

With the command awk and setting the separator caracter to ":"

cat test.txt|awk -F\: '{print $1}'

Hope this helps
Kenavo
Pat
Good judgement comes with experience. Unfortunately, the experience usually comes from bad judgement.
sysadm_1
Valued Contributor

Re: Cut first column

Hello Ivychung

you can use awk to do this.

awk -F ":" '{print $1}' test.txt

Cheers!!
sysadm
Siddhartha M
Frequent Advisor

Re: Cut first column

Invoke:

cat test.txt | awk -F: '{ print $1}'
111
222
333
444

-Siddhartha
Peter Godron
Honored Contributor

Re: Cut first column

Ivychung2,
please read :
http://forums1.itrc.hp.com/service/forums/helptips.do?#28

You have assigned 0 points to 62 answers !!

"Although assigning points is not mandatory, it is a key component of a strong, interactive community, and it is STRONGLY ENCOURAGED. Others have taken time to help you, so please take a moment to give them credit for their assistance!"
Hein van den Heuvel
Honored Contributor

Re: Cut first column

Hmmmm... would you believe that the 'cut' command can actually do this?!
Check out: man cut

cut -d: -f1 test.txt > output.txt

Cheers,
Hein.

Arturo Galbiati
Esteemed Contributor

Re: Cut first column

Hi,
if the separator will be always ":" you can use:
cut -d":" -f1 test.txt

if the separator will changebut teh size of teh column will be fixed you can use:
cut -c 1-3 test.txt

man cut for more info.
HTH,
Art