The UNIX and Linux Forums  
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.

Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
.
google unix.com



Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
How to extract a column from two different files in AWK? solracq Shell Programming and Scripting 7 04-29-2008 06:21 AM
column extract help cvm Shell Programming and Scripting 1 04-24-2008 04:19 PM
Extract column data from File sudheshnaiyer UNIX for Dummies Questions & Answers 3 10-11-2007 09:52 PM
extract column based on name t27 UNIX for Dummies Questions & Answers 3 08-29-2007 01:04 PM
I need to extract last column of a file and compare the values vukkusila Shell Programming and Scripting 4 08-04-2007 11:21 AM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Bulgarian Greek Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 05-20-2008
selamba_warrior selamba_warrior is offline
Registered User
  
 

Join Date: May 2008
Posts: 12
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
  #2 (permalink)  
Old 05-20-2008
DeCoTwc DeCoTwc is offline
Registered User
  
 

Join Date: Mar 2008
Location: NYC
Posts: 74
Not the most elegant solution... (but this is the first time I've been able give an answer on this forum)

Code:
cat unix |sed 's/\|/ /'|awk '{print $1}'
  #3 (permalink)  
Old 05-21-2008
selamba_warrior selamba_warrior is offline
Registered User
  
 

Join Date: May 2008
Posts: 12
Well it work now. Thank for you help.

Can you explain on your solution given?

why need to use 'sed'?
  #4 (permalink)  
Old 05-21-2008
hercu hercu is offline
Registered User
  
 

Join Date: May 2007
Posts: 26
Try using:

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

otherwise,

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

Tell us if that works for you.
  #5 (permalink)  
Old 05-21-2008
DeCoTwc DeCoTwc is offline
Registered User
  
 

Join Date: Mar 2008
Location: NYC
Posts: 74
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

Code:
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

Code:
cat unix |awk -F\| '{print $1}'
  #6 (permalink)  
Old 05-21-2008
aju_kup aju_kup is offline
Registered User
  
 

Join Date: Jul 2006
Posts: 189
this one should work for u

Code:
awk -F"|" '{print $1}' <filename>
  #7 (permalink)  
Old 05-21-2008
penchal_boddu penchal_boddu is offline
Registered User
  
 

Join Date: Apr 2008
Location: Bangalore
Posts: 127
Using sed,

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

Thanks
Penchal
Closed Thread

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -4. The time now is 11:23 AM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0