Perl Script to delete record if it exceeds 199 fields


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Perl Script to delete record if it exceeds 199 fields
# 1  
Old 10-13-2011
Perl Script to delete record if it exceeds 199 fields

HI All,
I am new to perl ,need your inputs for my better understanding.

I have 5 UNIX files ,inside which only required fields will be loaded except with junk data mentioned in double quotes .For Ex

File has below information:
Process=High
Name=Albert
Age=27
Occupation=Army
"Medlife Cont'd Capital partnership 12.36 Cont'd Sillicon valley Cont'd Global management overseas....................................................................
...........................Cont'd traditional quora etc....."

Fields starting with " " , wont be loaded as its invalid .Also has more than 199 fields which pops up inbetween correct records.When i use AWK ,its throwing error due to thresold limit.I cannot upgrade HP-UX to use gwak due to constraints.

Can PERL be used to search for pattern (Say "Medlife") and take count of each file line record ,if it exceeds 199 fields.It should delete that record and new file should be generated.

Please help me on this regard .





Constraints :
I cannot upgrade HP-UX to use gwak.Please advice how should i
# 2  
Old 10-13-2011
Why don't you remove the records with " (double quotes)?
Code:
egrep -v '"' File | <run your program here>

# 3  
Old 10-13-2011
@Shell Life,
Yes i tried it .But Problem is in one file ,it has more than 95lacs line records.
Even one line/two also contains with quotes" " .Which are usually ignored because of <199 fields. Though time taking to get required records excepts quotes is increasing processing speed .

I want perl code
1) to check for that pattern
2) If count is >=199 for that line record it should be deleted.

Only 3-5 cases have >199 lines in entire 95lacs record file.So if this condition is checked outcome would be pretty fast. Please guide if any other method make justice . Thanks
# 4  
Old 10-13-2011
It will help if you display a sample of the actual data.
# 5  
Old 10-13-2011
File
Process=Low
Name=Andrew Place=NY
Age=25 Sex=M
Occupation=Business
"Medlife Cont'd 12.23 AM transword"

Process=High Place=NE
Name=Micheal Sex=M
Age=63
Occupation=Banker
"Medlife Cont'd 16.6 AM Silicon valley Cont'd {Majority
Setback} Cont'd 13.54 AM port ........ " HAS MANY LINES

If pattern matching is "Medlife and line record
Count >=198 It should delete.
# 6  
Old 10-13-2011
you could store $. at start of the field and continue to add the lines to the field until the end of the field or current $. - $start_of_field > 199.
-
# 7  
Old 10-13-2011
Given the below is in file.in :-

File
Process=Low
Name=Andrew Place=NY
Age=25 Sex=M
Occupation=Business
"Medlife Cont'd 12.23 AM transword"

Process=High Place=NE
Name=Micheal Sex=M
Age=63
Occupation=Banker
"Medlife Cont'd 16.6 AM Silicon valley Cont'd {Majority
Setback} Cont'd 13.54 AM port ........ " HAS MANY LINES

If pattern matching is "Medlife and line record
Count >=198 It should delete.


The following basic perl oneliner will negate any line that matches Medlife that is above or equal to line 15 ie in the above case "If pattern matching is "Medlife and line record" will be negated. The output will be directed to file.out.

Code:
# perl -lane 'print unless (/Medlife/) && ($. >= 15)'  <file.in > file.out

Change the 15 for 198.
Not sure if this is what you are after but hope it helps.

Cheers,
Steve

Last edited by Paragon1970; 10-14-2011 at 06:09 AM..
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

awk - compare 1st 15 fields of record with 20 fields

I'm trying to compare 2 files for differences in a selct number of fields. When differnces are found it will write the whole record of the second file including appending '|C' out to a delta file. Each record will have 20 fields, but only want to do comparison of 1st 15 fields. The 1st field of... (7 Replies)
Discussion started by: sljnk
7 Replies

2. Shell Programming and Scripting

Delete last 2 fields from every record in a file

Sample file record : "20130617003","2013-06-18T07:00:03","OUTWARD","01001011","TEST PLC","","HFX834346364364","20130617","10","DUM87534758","","1.28","826","020201","65879278","","","","","","010101","56789","DUMMY... (3 Replies)
Discussion started by: bigbuk
3 Replies

3. Shell Programming and Scripting

Compare fields and keep record with bigger ID?

How do you write a shell script to compare records with the same fields then keep the biggeer id number fields (field separate by a pipe) 1150| San Jose|8|15|7|2013-02-19 00:00:00.000|2013-02-20 00:00:00.000 1263|San Jose|8|15|7|2013-02-19 00:00:00.000|2013-02-20 00:00:00.000... (4 Replies)
Discussion started by: sabercats
4 Replies

4. UNIX for Dummies Questions & Answers

To delete the oldest files in a file when file count in the folder exceeds 7

Hi All, I need to delete the oldest file in folder when the file count in the folder exceed 6 ( i have a process that puts the source files into this folder ) E.x : Folder : /data/opt/backup 01/01/2012 a.txt 01/02/2012 b.txt ... (1 Reply)
Discussion started by: akshay01987
1 Replies

5. Linux

perl program to delete the complete record

Hi all, I want a perl program to delete the record and its contents from a file if there is no particular line in the record given that all records are separated by a blank line. For example: #100 abcd efgh hijk 123 klm #200 abcd efgh hijk klm So, the pattern here is 123. If... (0 Replies)
Discussion started by: kaav06
0 Replies

6. Shell Programming and Scripting

Print all the fields of record using awk

Hi, i want to generate print statement using awk. i have 20+ and 30+ fields in each line Now its priting only first eight fields print statement as output not all. my record is as shown below filename ... (2 Replies)
Discussion started by: raghavendra.nsn
2 Replies

7. Shell Programming and Scripting

Shell script for searching a record,copy to a file and then delete it

Hi, I have a requirement in hand: I have a file with millions of records say file 1.I have another file, say file 2 which has 2000 records in it. The requirement is to read file2 , and remove the read record from file 1 and move i to a seperate file, file 3. For eg: Read file 2, get the... (5 Replies)
Discussion started by: kumara2010
5 Replies

8. Shell Programming and Scripting

Making changes in the fields of a record

:confused: Hi Friends, In the record below i have to make changes in the fields by putting the values stored in the temporary variables, x, y, z, p, q, r: 2) In the TBT record store the values in the various fields as: a) X in a field position 51 to 56 b) Y... (5 Replies)
Discussion started by: kanu_pathak
5 Replies

9. Shell Programming and Scripting

How count number of fields in a record

Dear All , I have the query cat temp.txt |28-07-1997|IF_LEG_DCCT|TOV JV sdfsdfdsfdsfdsCLOSED* KIEV|381015280 I need to count the number of fields in this pipe-seperated file. I beleive this is possible via AWK command. The in above file, output of the count should be 5.... Can some-one... (5 Replies)
Discussion started by: sureshg_sampat
5 Replies

10. Shell Programming and Scripting

awk: record has too many fields

Hi, I'm trying this command - but get this error. Do you guys have any workaround for this? cat tf|sed 's/{//g'|sed 's/,//g'|awk '{for (i=1;i<=NF;i++) {if ($i == "OPTIME") {k = i + 2; print $i,$k}}}' awk: record `2005 Jul 28 17:35:29...' has too many fields record number 15 This is how... (3 Replies)
Discussion started by: chaandana
3 Replies
Login or Register to Ask a Question