how to read record by record from a file in unix


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers how to read record by record from a file in unix
# 1  
Old 05-16-2008
PHP how to read record by record from a file in unix

Hi guys,
i have a big file with the following format.This includes header(H),detail(D) and trailer(T) information in the file.My problem is i have to search for the character "6h" at 14 th and 15 th position in all the records .if it is there i have to write all those records into a separate file along with the header and trailer and the last nine digits in the trailer should indiacate the count of the detaied records.can any one help me on this how to achive? Basically i am new to unix shell scripting.

following the file format is

HBM6303000000000200805060940412008050609404120080506SCITS 8.2
D6303200805066HJ8164-61002 EURO6303I0073554514 0017992008031120080430HP00DD00Hewlett-Packard Pte Ltd 12.000 20080502 0000000509.3900UDNB USDEA 00001EA SG 00022797990001 00000120080226 0073554514 6303 N101 HP00DD00 20080503 00022797990001
D6303200805066HJ8164-61002 EURO6303I0073554514 0018052008031120080430HP00DD00Hewlett-Packard Pte Ltd 16.000 20080502 0000000509.3900UDNB USDEA 00001EA SG 00022798050001 00000120080226 0073554514 6303 N101 HP00DD00 20080503 00022798050001
D6303200805066HJ8164-61002 EURO6303I0073554514 0018142008031120080430HP00DD00Hewlett-Packard Pte Ltd 16.000 20080502 0000000509.3900UDNB USDEA 00001EA SG 00022798140001 00000120080226 0073554514 6303 N101 HP00DD00 20080503 00022798140001
D6303200805066HJ8164-61002 EURO6303I0073554514 0018092008031120080430HP00DD00Hewlett-Packard Pte Ltd 16.000 20080502 0000000509.3900UDNB USDEA 00001EA SG 00022798090001 00000120080226 0073554514 6303 N101 HP00DD00 20080503 00022798090001
TBM630300000000020080506094041000003818

advancedthanks
narasimha.
# 2  
Old 05-16-2008
Code:
awk '/^H/ { h=$0; next }
/^T/ { if (p) print; p=0; next }
substr($0,14,2) == "6h" { if (h) print h; if (d) print d; print; h = d = ""; p=1; next }
{ h = d = ""; p=0; }' file

With only a single example to test with, it's hard to know if this correct. Apparently all the D lines in an entry will have "6H" (not "6h")?
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Extract timestamp from first record in xml file and it checks if not it will replace first record

I have test.xml <emp><id>101</id><name>AAA</name><date>06/06/14 1811</date></emp> <Join><id>101</id><city>london</city><date>06/06/14 2011</date></join> <Join><id>101</id><city>new york</city><date>06/06/14 1811</date></join> <Join><id>101</id><city>sydney</city><date>06/06/14... (2 Replies)
Discussion started by: vsraju
2 Replies

2. Shell Programming and Scripting

[Solved] awk command to read sequentially from a file until last record

Hello, I have a file that looks like this: Generated geometry (...some special descriptor) 1 0.56784 1.45783 -0.87965 8 1.29873 -0.8767 1.098789 ... ... ... ... Generated geometry (....come special descriptor) ... .... ... ... ... ... ... ... and... (4 Replies)
Discussion started by: jaldo0805
4 Replies

3. 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

4. Shell Programming and Scripting

Update file record inside read loop

Hi, I am reading file records inside a while loop, and want to update the record when certain condition is met. How can I update a file while being read? I want to avoid using temporary files, copy, rename, ... while IFS=',' read -r f1 f2 do function(f1,f2) if then <add... (1 Reply)
Discussion started by: ysrini
1 Replies

5. Shell Programming and Scripting

Read record from the text file & assign those values to variables in the script

For eg: I have sample.txt file with 4 rows of record like: user1|password1 user2|password2 user3|password3 user4|password4 The username and password is sepsrated by '|' I want to get the 1st row value from the file and assign it to two different variables(username and password) in my... (1 Reply)
Discussion started by: priya001
1 Replies

6. Shell Programming and Scripting

Bash Script to read a file and parse each record

Hi Guys, I am new to unix scripting and I am tasked to parse through a CSV file delimited by #. Sample: sample.csv H#A#B#C D#A#B#C T#A#B#C H = Header D = Detail Record T = Tail What I need is to read the file and parse through it to get the columns. I have no idea on how... (8 Replies)
Discussion started by: 3vilwyatt
8 Replies

7. Shell Programming and Scripting

Read the record from a text file

Hi I want to read one row record from a text file. For eg: I have Sample.txt file with one row of record like 123456768 I want to get the above value from the file and assign it to a variable in my script. Please guide me how to proceed. Thanks, Soll (2 Replies)
Discussion started by: sollins
2 Replies

8. UNIX for Advanced & Expert Users

How to read an Xml record contained in a file--urgent

Hi I have an xml file which has multiple xml records.. I don't know how to read those records and pipe them to another shell command the file is like <abc>z<def>y<ghi>x........</ghi></def></abc> (1st record) <jkl>z<mno>y<pqr>x........</pqr></mno></jkl> (2nd record) Each record end... (4 Replies)
Discussion started by: aixjadoo
4 Replies

9. Shell Programming and Scripting

splitting a record and adding a record to a file

Hi, I am new to UNIX scripting and woiuld appreicate your help... Input file contains only one (but long) record: aaaaabbbbbcccccddddd..... Desired file: NEW RECORD #new record (hardcoded) added as first record - its length is irrelevant# aaaaa bbbbb ccccc ddddd ... ... ... (1 Reply)
Discussion started by: rsolap
1 Replies

10. Shell Programming and Scripting

read record from file

Hi all I have file f1 like this: Set AM/PM indicator to PM started|14155| Generate Error Re|7| Projected Cash Ba|741| Roll System Date |4| Projected Cash Balances started|2| Process Mark To Market started|13429| Process paydowns started|14189| Process Fixed Inc|439| Process Mark To... (3 Replies)
Discussion started by: koti_rama
3 Replies
Login or Register to Ask a Question