The UNIX and Linux Forums  

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 read record by record from a file in unix raoscb UNIX for Dummies Questions & Answers 1 05-16-2008 07:30 AM
Script to search a bad record in a file then put the record in the bad file shilendrajadon Shell Programming and Scripting 2 12-28-2007 10:02 AM
Script to search a bad record in a file then put the record in the bad file shilendrajadon UNIX for Advanced & Expert Users 1 12-28-2007 10:00 AM
splitting a record and adding a record to a file rsolap Shell Programming and Scripting 1 08-13-2007 02:58 PM
Converting Tab delimited file to Comma delimited file in Unix charan81 Shell Programming and Scripting 22 01-20-2006 09:24 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 04-06-2008
knijjar knijjar is offline
Registered User
  
 

Join Date: Jan 2008
Posts: 27
help with agrep or awk delimited by |~| in a record

I have a file which contains 1 million records of the following format. Each field is delimited by a pipe tilda pipe "|~|" show below. I would like to print only one column ie the CARDDESC value.

for example here it says CARDDESC=A8T1.
so anything after CARDDESC= and before |~|CARDTYPE is what I would like to get from this record and ignore everything else.

In other words the value between CARDDESC= and |~|CARDTYPE which in the following example is A8T1 is what I need to know.

Any help is really appreciated. if my question is not clear please let me know I will try to phrase it in a better way.

Each filed is delimited by |~| that is for sure through out the file. so even if I can get CARDDESC=A8T1 I am happy.

RECORD=NEW|~|NAME=SCO|~|MODEL=8220|~|WORK=ATM|~|SUBNET=ATM|~|SITE=RKS|~|REGION=NORTH A|~|COUNTRY=US|~
|SWITCH=FAX2|~|ETHERNET=1.4.1.1|~|LOOPB=N/A|~|SHELF=N/A|~|SLOT=12|~|SUBSLOT=N/A|~|STSCHAN=N/A|~|PORT=8|~|DS1SLOT=0|~|LINE=1|~|LPORTID=0|~|C
ARDDESC=A8T1|~|CARDTYPE=0|~|ENCAPSULATION=N/A|~|BUNDLEID=0|~|PORTUSE=N/A|~|STATUS=T|~|MANAGED=YES|~|BILLINGID=N/A|~|CKTID=W86|~|CKTIP=1.0.0.1
7|~|PORTSERVICE=T1|~|SPEED=6|~|CHNLS=N/A|~|NX_Y=83|

Last edited by knijjar; 04-06-2008 at 04:32 AM.. Reason: changing the title to include agrep or awk
  #2 (permalink)  
Old 04-06-2008
era era is offline Forum Advisor  
Herder of Useless Cats (On Sabbatical)
  
 

Join Date: Mar 2008
Location: /there/is/only/bin/sh
Posts: 3,652
Whoever devised that particular file format should be taken out and shot.

Is the field always in the same column?


Code:
 awk -F '\|~\|' '{ print $20 }'

  #3 (permalink)  
Old 04-06-2008
matrixmadhan matrixmadhan is offline Forum Advisor  
Technorati Master
  
 

Join Date: Mar 2005
Location: leaf node in B+ tree
Posts: 2,960
Quote:
Originally Posted by era View Post
Whoever devised that particular file format should be taken out and shot.

Is the field always in the same column?


Code:
 awk -F '\|~\|' '{ print $20 }'
I believe this will not give what the OP had asked for
  #4 (permalink)  
Old 04-06-2008
matrixmadhan matrixmadhan is offline Forum Advisor  
Technorati Master
  
 

Join Date: Mar 2005
Location: leaf node in B+ tree
Posts: 2,960
Quote:
Whoever devised that particular file format should be taken out and shot.
Oh! Really

Then I recommend you to take a look at the library formats ( bibliography formats ) am sure you would come back and say much better
  #5 (permalink)  
Old 04-06-2008
era era is offline Forum Advisor  
Herder of Useless Cats (On Sabbatical)
  
 

Join Date: Mar 2008
Location: /there/is/only/bin/sh
Posts: 3,652
Works for me on Linux (I guess gawk, or actually mawk). matrixmadhan's solution is closer to the real McCoy so use that if it works for you.

Or even, how about this.


Code:
sed -n 's/.*|~|CARDDESC=//; T; s/|~|.*//p'

matrixmadhan: agreed, I've seen much worse than this. I can also swear a lot louder than you have seen so far. (My colleagues can tell when I'm forced to use Windows.)

Last edited by era; 04-06-2008 at 04:57 AM.. Reason: It's apparently mawk actually
  #6 (permalink)  
Old 04-06-2008
knijjar knijjar is offline
Registered User
  
 

Join Date: Jan 2008
Posts: 27
Thanks ERA and MATRIXMADHAN.

/usr/local/bin/awk -F"CARDDESC=" '{ split($2, arr, "|"); print arr[1] }' t1
A8T1

now would you be kind enough try to explain what it really did.

I have gone with what matrixmadhan recommended

Peace !!!
  #7 (permalink)  
Old 04-06-2008
knijjar knijjar is offline
Registered User
  
 

Join Date: Jan 2008
Posts: 27
I wouldnt agree with you more about the format. I hate it when people do that. and yes the filed is always going to be in the same column

which awk
/usr/bin/awk
awk -F '\|~\|' '{ print $20 }' RECORD
awk: syntax error near line 1
awk: bailing out near line 1

/usr/local/bin/awk -F '\|~\|' '{ print $20 }' RECORD
|CARDDESC=A8T1|

/usr/local/bin/awk -F "|~|" '{print $20}' CIRCUITRECORD
|CARDDESC=A8T1|

same result

The output contains a "pipe" symbol which is ok but can that be taken out from the output. I am happy with the results but just wondering.

Thanks ERA in advance for your help
Closed Thread

Bookmarks

Tags
linux

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