Reading a file from a specified offset


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Reading a file from a specified offset
# 1  
Old 12-12-2007
Reading a file from a specified offset

Hi,

I want to read a file from a specified offset from the start of file. With the read command, is it possible to do so. Please suggest. Is there any other alternative?

Thanks,
Saurabh
# 2  
Old 12-12-2007
Do you mean the system call read()? If so use lseek() beforehand to seek into the file to the offset you want.
# 3  
Old 12-13-2007
Thanks Porter. I have calculated the offset from where I have to read. I believe before using lseek, I will have to open the file using open system call. I am getting a syntax error when I try to open the file. Please see code below:

apdma001:/amex/home/mqm/.saurabh # vi Myprog
"Myprog" 30 lines, 924 characters
# This is my first program

USAGE="Myprog <profile to be searched> "
if [ $# -ne 1 ]
then
echo "Invalid number of arguements :USAGE : $USAGE "
exit
fi

strToSearch="\."$1
# Write all relevant profiles into Myprog1. Filter those lines which have "MQQUEUE" so that only one occurance is listed

grep $strToSearch Sarah.txt | grep -v MQQUEUE > Myprog1
echo "Following profiles have been found:"

myfile="/amex/home/mqm/.saurabh/Myprog1"
myline=""
fd=open("Sarah.txt",O_RDONLY)
while [ 1 ]
do
read myline || break
# Remove "(G)" from the profile name
set -A profileNm $myline
# Search for second occurance of profileNm and read the line number of the second occurance
grep -n $profileNm Sarah.txt | head -n 2 | tail -n 1 | cut -f1 -d ":" | read startline
# Increase offset by 57 to move to access area
startline=`expr $startline + 57`
echo "$profileNm : $startline"
done < $myfile

Myprog[18]: 0403-057 Syntax error at line 18 : `(' is not expected.
I am new to unix, so my questions may appear very silly. But I am just trying to learn. Appreciate your help.

Thanks,
Saurabh
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

File name offset

Dear all, I want to offset the file numbers. can you please make some awk code or linux code for the same. Example: input file names ANI_WFMASS_PIST00001.gif ANI_WFMASS_PIST00002.gif . . . ANI_WFMASS_PIST0000n.gif offset --> 30 ANI_WFMASS_PIST00031.gif ANI_WFMASS_PIST00032.gif... (14 Replies)
Discussion started by: kri321shna
14 Replies

2. UNIX for Advanced & Expert Users

Grep --byte-offset not returning the offset (Grep version 2.5.1)

Hi, I am trying to get the position of a repeated string in a line using grep -b -o "pattern" In my server I am using GNU grep version 2.14 and the code is working fine. However when I am deploying the same code in a different server which is using GNU grep version 2.5.1 the code is not... (3 Replies)
Discussion started by: Subhamoy
3 Replies

3. Shell Programming and Scripting

Get Compressed byte offset from .gz file

Hi , I have a .gz file whose contents look like below. data1^filename1 data2^filename2. .. . . Is it possible to find out the byte offset of each record from the .gz file. Like in an uncompressed file. grep -nb "Filename" give the byte offset of the record in this case. ... (4 Replies)
Discussion started by: chetan.c
4 Replies

4. UNIX for Dummies Questions & Answers

Tail with positive offset

I have read the below from the book bash cookbook.Tail +1 filenames is similar to cat filename I have tried the same in Ubuntu 11.10 with bash. 4.0 . I have received error for the Same. May I know in which system that will work fine ? Thanks (1 Reply)
Discussion started by: pandeesh
1 Replies

5. Solaris

NTP client offset

How to add offset to NTP client so that, for eg., clock is -20 seconds? (2 Replies)
Discussion started by: orange47
2 Replies

6. Shell Programming and Scripting

Moving bytes in file by offset and length

Hi, I'm looking for a way (other than C) to pull out a number of bytes in a Linux file for a giving length. for example: file1 contains 2 records: abcdefghijkl mnopqrstuv ..... so, I want to pull starting in byte 9 for a length of 8 file2 would contain: ijkmnopq Thanks (2 Replies)
Discussion started by: jbt828
2 Replies

7. Solaris

how to calculate offset value to mb value in vxvm

root@erpqas# vxdg -g sap_dg free DISK DEVICE TAG OFFSET LENGTH FLAGS sapdisk3 c1t10d0s2 c1t10d0 15707513 2869 - sapdisk3 c1t10d0s2 c1t10d0 71080956 43335 - sapdisk5 c1t12d0s2 c1t12d0 70321149 803142 - ... (1 Reply)
Discussion started by: tv.praveenkumar
1 Replies

8. Shell Programming and Scripting

increment a value at an offset in hundreds very large hex file

I have a lot of very large hex files that I need to change one value at the same offset and save to another file. I have a script that finds each file and just need to put an operator for each file. I think sed might be able to do this but I have not used it before and would like some help. If... (8 Replies)
Discussion started by: Eruditass
8 Replies

9. Programming

Negative Offset

Function: int fcntl(int fd, int cmd, struct flock * lock) Data Type: struct flock This structure is used with the fcntl function to describe a file lock. It has these members: off_t l_start This specifies the offset of the start of the region to which the lock applies, and... (1 Reply)
Discussion started by: DNAx86
1 Replies

10. UNIX for Advanced & Expert Users

Script to Extract the line from File with specified offset

Hi All, I need to extract only XML details from large log file which may contain other unwanted junk details. For example, our xml will be start as <OUTBOUND_MESSAGE .....> and ends with </OUTBOUND_MESSAGE>. I want to extract only lines between these start and end tag (Including these tags)... (5 Replies)
Discussion started by: thinakarmani
5 Replies
Login or Register to Ask a Question