The UNIX and Linux Forums  

Go Back   The UNIX and Linux Forums > Top Forums > UNIX for Dummies Questions & Answers
.
google unix.com



UNIX for Dummies Questions & Answers If you're not sure where to post a UNIX or Linux question, post it here. All UNIX and Linux newbies welcome !!

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
need to extract field of characters in a line jansat Shell Programming and Scripting 3 05-22-2008 10:44 AM
How to extract field from variable more efective pp56825 Shell Programming and Scripting 11 01-19-2008 10:34 AM
logic needed to extract lines ganapati UNIX for Dummies Questions & Answers 2 01-10-2008 07:18 AM
Extract Part of string from 3rd field $3 using AWK stakuri Shell Programming and Scripting 4 10-10-2007 01:28 PM
Trying to extract a field from /etc/passwd file.. xBuRnTx UNIX for Dummies Questions & Answers 2 10-05-2005 03:45 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 01-27-2009
ChicagoBlues ChicagoBlues is offline
Registered User
  
 

Join Date: Jan 2008
Location: Chicago
Posts: 77
extract a field by logic

below are the contents of file 'tmp.out':

2|34|534|
1|355|54|
1|443|34|
3|43|768|
3|7|887|
1|9|990|

How do I extract the 2nd and 3rd columns of file 'tmp.out' only when the first column equals '1'.

Note that this file will be huge; atleast 5million+ rows, so I want to avoid looping over this file.

Thanks,

- CB
  #2 (permalink)  
Old 01-27-2009
Ikon's Avatar
Ikon Ikon is offline Forum Advisor  
Registered User
  
 

Join Date: Jul 2008
Location: Phoenix, Arizona
Posts: 669

Code:
 grep '^1|' test.log | awk -F '|' '{ print $2 "|" $3 }'

  #3 (permalink)  
Old 01-27-2009
joeyg's Avatar
joeyg joeyg is offline Forum Staff  
modérateur
  
 

Join Date: Dec 2007
Location: Home of 17-time world champion Boston Celtics
Posts: 1,311
Wink Perhaps this approach


Code:
> cat file150
2|34|534|
1|355|54|
1|443|34|
3|43|768|
3|7|887|
1|9|990|
> awk -F"|" '$1=="1"{print $2"|"$3}' file150
355|54
443|34
9|990

  #4 (permalink)  
Old 01-27-2009
ChicagoBlues ChicagoBlues is offline
Registered User
  
 

Join Date: Jan 2008
Location: Chicago
Posts: 77
Okay, so what if I want to search for that '1' in the sixth column, and extract 8 and 9th columns.

- CB
  #5 (permalink)  
Old 01-27-2009
ChicagoBlues ChicagoBlues is offline
Registered User
  
 

Join Date: Jan 2008
Location: Chicago
Posts: 77
got it...

awk -F"|" '$6=="1"{print $8"|"$9}' tmp.out

where the tmp.out file has 9 columns.

Thx.

- CB
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 04:01 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