Operating System - Linux
1754798 Members
3959 Online
108825 Solutions
New Discussion юеВ

Re: File handling to fetch records

 
SOLVED
Go to solution
Panpalia
Occasional Advisor

File handling to fetch records

Hi,
I need to fetch records from the file by checking some condition. Please find the sample of records in the file.

I/P file
0350000000013502400107395824*Pamela Anne Lentine*Calvary Electric*****Lentine, Pamela Anne**45 ----first records end
0429000000013702600468749296*GESCO ASESORIA DE EMPRESAS, SL********PASEO DE ALMERIA - PLT 2 63**ALMERIA**1404**ALMERIA**04001
*SPAIN*693*3***---second records end
0725000000013802800899116768*AVG SiderM-#rgia Ltda.*AVG SiderM-#rgia*Siderurgica Ita Min Ltda*AVG Siderurgia Ltda*A V G Sider
urgia Ltda****Rua GrM-Fo ---third records end
Now I want to fetch only records from the file whose first five fields must be greater then 0500.

So my o/p file should contains following records

O/P
0725000000013802800899116768*AVG SiderM-#rgia Ltda.*AVG SiderM-#rgia*Siderurgica Ita Min Ltda*AVG Siderurgia Ltda*A V G Sider
urgia Ltda****Rua GrM-Fo
Logically i need to cut first five fields then compare whether it's greater then 0500 if yes fetch the whole records or read next records. but i unable to put above logic syntactically.

Could you please provide me the commands and if possible syntax as well .
13 REPLIES 13
Dennis Handly
Acclaimed Contributor

Re: File handling to fetch records

You don't have any field delimiters. Also you'll need to better tell us where records end. I.e. attach a sample file or make the records shorter/simpler.

>i need to cut first five fields

Did you mean the first 4 columns/bytes?
awk '
{
value1 = substr($0, 1, 4)
if (value <= 500)
next
print $0
} file
Dennis Handly
Acclaimed Contributor

Re: File handling to fetch records

Oops, these should have the same name:
value1 = substr($0, 1, 4)
if (value1 <= 500)
Panpalia
Occasional Advisor

Re: File handling to fetch records

Hi There is no field delimiter but we can add.The length of records are variable in size. please find the sample of records in first post.
OFC_EDM
Respected Contributor

Re: File handling to fetch records

Need some clarification

Is this record one continuous line with a NewLine at the end?

0350000000013502400107395824*Pamela Anne Lentine*Calvary Electric*****Lentine, Pamela Anne**45 ----first records end

Or are there mulitiple Newlines?

As well do you need to just compare the first four characters 0350 against 0500?
The Devil is in the detail.
Hein van den Heuvel
Honored Contributor
Solution

Re: File handling to fetch records

>> Logically i need to cut first five fields then compare

You write 'five fields' twice, but the text suggest '4 characters'. Was there a leading space which dissapeard in teh web page formatting?


>> if yes fetch the whole records or read next records

You write "if ... do this or do that"
Did you mean "if ... do this else to that" ?

A solution might be:

$ awk '0+substr($0,1,4) > 500' input-file

- The "0 + " forces the value to become numeric for comparison.
- The empty action defaults to 'print current line'.
- awk always loops through all records.

Hein.


James R. Ferguson
Acclaimed Contributor

Re: File handling to fetch records

Hi:

Judging from what you posted for sample data, it would appear that your record delimiter is simply a newline. Thus:

# perl -ne 'print if substr($_,0,4) >= 500' file

...would print records whose first (zero-relative) column for four characters is greater or equal to 500.

*PLEASE* also read:

http://forums12.itrc.hp.com/service/forums/helptips.do?#28

You have answers to previous questions that you have not yet evaluated. These questions of yours can be found by looking at your profile:

http://forums12.itrc.hp.com/service/forums/questionanswer.do?threadId=1236026

http://forums12.itrc.hp.com/service/forums/questionanswer.do?threadId=1236353

Providing feedback to the responses to your questions helps the community at large determine which answers solved your problem.

Regards!

...JRF...

Panpalia
Occasional Advisor

Re: File handling to fetch records

Thanks everyone for your quick reply.

Kevin - The new records start with new line. There is no multiple new line.I need to compare only first five byte as mentioned by you but need to fetch record if condition is true.

Hein van den Heuvel - i need to check first five field (0350) if it's greater then 500 then fetch the complete records.
Peter Nikitka
Honored Contributor

Re: File handling to fetch records

Hi,

you seem to consider Heins solutions as incomplete - why?
Perhaps:
1) You stated 'greater than 0500' - this leads to '>'. But because you want to get a line starting 0500, you mean '>=' .

2) The first *five* bytes for the first line seem '03500' to me - not '0350' as you told us:
Either that are only four bytes or your string is incomplete. 1) + 2) may lead to
awk '0+substr($0,1,5) >= 5000'

3) The first '0' - is it used to get an octal representation of a number or is it just used to fill the record?

mfG Peter
mfG Peter
The Universe is a pretty big place, it's bigger than anything anyone has ever dreamed of before. So if it's just us, seems like an awful waste of space, right? Jodie Foster in "Contact"
Dennis Handly
Acclaimed Contributor

Re: File handling to fetch records

>There is no field delimiter but we can add.

You don't need to add but you do need to say where they begin and end.

>please find the sample of records in first post.

As I mentioned, this wasn't good enough.
You need to attach a file and give definition of your record "key".

>MK: 3) The first '0' - is it used to get an octal representation of a number or is it just used to fill the record?

Only broken C does that octal junk.