How to extract only first column from the file

Dear All,

I have a file name pointer.unl. It's contains the information below:

O|A|4560333089|PBS|AU1|01/04/2003|30/04/2006|D|IGCD|
O|A|4562222089|PBN|AU1|01/02/2006|31/01/2008|D|04065432|
O|A|3454d00089|PKR|AU1|01/03/2008||R|sdcdc|

I only need to extract first column only which is the "O" column.

I tried command below;

1-awk '{print $1}' filename

  • but it display all the column

2-awk 'Begin {OFS="|"} {print $1}' filename

  • still showing all the column

3- awk -F| '{print $1}' filename > outfilename

  • still display all the file comlum

Appreciate if somebody can assist me.

Rgrds

Shahril

Not the most elegant solution... (but this is the first time I've been able give an answer on this forum)

cat unix |sed 's/\|/ /'|awk '{print $1}'

Well it work now. Thank for you help.

Can you explain on your solution given?

why need to use 'sed'?

Try using:

awk -F\| '{print $1}'

otherwise,

awk -F"|" '{print $1}'

Tell us if that works for you.

Well I don't know why the awk statement isn't working, I'm hoping someone will enlighten us both on that matter, but the sed statement is removing the | from each line. It would work just as well to use

cat unix |tr "|" " "|awk '{print $1}'

this also replaces all of the |'s with whitespace

I think that if you escape (put a \) in front of the | in your original awk statement that will work as well

cat unix |awk -F\| '{print $1}'
awk -F"|" '{print $1}' <filename>

I think this should work:

cat unix | awk -F\| '{print $1}'

..... reason is "|" being an alpha numeric character (like *,?,%,^,$) would require a "\" before that to tell the Kernel to not use "|" as a function..

Using sed,

sed 's/|.*$//' filename

Thanks
Penchal

Hercu,

Yup the command is working fine. I miss the -F"|" that why it's not work.

thanks everyone.

Hercu and DecoTwc

Yup the command is working fine. I miss the -F"|" that why it's not work.

thanks everyone.

anytime!

you can also use cut comamnd to get the same reuklt. I guess the command would be:

cat unit | cut -d"|" -f0

Cheers

Why do you guys want to use sed or awk when a simple cut(1) will do ?

cut -d '|' -f 1 file