help with awk delimited by |~| in a record


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting help with awk delimited by |~| in a record
# 1  
Old 04-06-2008
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  
Old 04-06-2008
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  
Old 04-06-2008
Code:
awk -F"CARDDESC=" '{ split($2, arr, "|"); print arr[1] }' t1

the same could be done with multiple field delimiters
# 4  
Old 04-06-2008
Quote:
Originally Posted by era
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 Smilie
# 5  
Old 04-06-2008
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
# 6  
Old 04-06-2008
Quote:
Whoever devised that particular file format should be taken out and shot.
Oh! Really Smilie

Then I recommend you to take a look at the library formats ( bibliography formats ) am sure you would come back and say much better Smilie
# 7  
Old 04-06-2008
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
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Insert a header record (tab delimited) in multiple files

Hi Forum. I'm struggling to find a solution for the following issue. I have multiple files a1.txt, a2.txt, a3.txt, etc. and I would like to insert a tab-delimited header record at the beginning of each of the files. This is my code so far but it's not working as expected. for i in... (2 Replies)
Discussion started by: pchang
2 Replies

2. Shell Programming and Scripting

How to compare current record,with next and previous record in awk without using array?

Hi! all can any one tell me how to compare current record of column with next and previous record in awk without using array my case is like this input.txt 0 32 1 26 2 27 3 34 4 26 5 25 6 24 9 23 0 32 1 28 2 15 3 26 4 24 (7 Replies)
Discussion started by: Dona Clara
7 Replies

3. Shell Programming and Scripting

awk read one delimited file, search another delimited file

Hello folks, I have another doozy. I have two files. The first file has four fields in it. These four fields map to different locations in my second file. What I want to do is read the master file (file 2 - 23 fields) and compare each line against each record in file 1. If I get a match in all four... (4 Replies)
Discussion started by: dagamier
4 Replies

4. Shell Programming and Scripting

How to make delimited record ????

Can anyone suggest how to make delimited data for below report format Member Nbr Member Name Date Date Number Miles Rejected Reason Purge 1000000000 BROWNS N/B 10121998 01121998 377393930 500 INVALID CUSTOMER NUM issue is that my column name, data and between column i have variable... (7 Replies)
Discussion started by: ns64110
7 Replies

5. Shell Programming and Scripting

Use awk or sed to parse delimited string

Hi I am trying to figure out the best way to search a long log file and print out certain information. For example if I had a line in a log file delimited by ampersand first_name=mike&last_name=smith&zip_code=55555&phone=555-5555&state=ma&city=boston and I only wanted to search for and... (3 Replies)
Discussion started by: mstefaniak
3 Replies

6. Shell Programming and Scripting

awk help - delimited file

Hi, I have a delimited file with 10 fields. I have to replace a character in field 1 and field 5. It is possible to do using awk where i can read a row at a time field by field and if the field value is 1 or 5 then i will replace the character. But this operation is cumbersome since the... (8 Replies)
Discussion started by: ashwin3086
8 Replies

7. Shell Programming and Scripting

Using AWK to parse a delimited field

Hi everyone! How can I parse a delimited field using AWK? For example, if I have lastName#firstName or lastName*firstName. I'd like an AWK script that would return lastName and then another that would return firstName? Is this possible? (13 Replies)
Discussion started by: Fatbob
13 Replies

8. Red Hat

Counting columns in a delimited record with NULLs

I am trying to count the number of columns in a delimited flat file. The record is tab delimited. When I use the command wc -w, I am getting unexpected results when the record contains a NULL value. I believe it is because there is a "word" missing. The below example will return 4 words... (2 Replies)
Discussion started by: nmalencia
2 Replies

9. Shell Programming and Scripting

splitting tab-delimited file with awk

Hi all, I need help to split a tab-delimited list into separate files by the filename-field. The list is already sorted ascendingly by filename, an example list would look like this; filename001 word1 word2 filename001 word3 word4 filename002 word1 word2 filename002 word3 word4... (4 Replies)
Discussion started by: perkele
4 Replies

10. UNIX for Advanced & Expert Users

awk - delimited output

Hi I am looking for syntax in awk, where I am printing comma separated coulmns read from delimited file. Say for e.g. awk -F":" '{print $1","$2","9}' fname Instead of using "," withing print syntax,is there any way by which fields being printed will automatically get delimited by comma? ... (1 Reply)
Discussion started by: videsh77
1 Replies
Login or Register to Ask a Question