Accessing substrings by offset and length


 
Thread Tools Search this Thread
Top Forums Programming Accessing substrings by offset and length
# 1  
Old 10-17-2002
Accessing substrings by offset and length

Hi,

I have a simple question... In C do we have a standard library function which will return the pointer to a substring at certain offset and having certain length...

Ofcourse we should take care not to access beyond allocated length in the parent string and don't overwrite beyond allocated length in the destination string...

it may look like this

int foo (char *s, char *t, int offset, int length);

if *s = "vishnu"; offset = 3; length = 2; it should give back
*t = "sh"

I'm not saying I want a function with the exact interface as above, but anything which allows me to access substrings by offset and length would be great...

This is not a homework question!!! I work in an office and write small C programs and sh scripts to automate task in a UNIX environment..

Thank you!
Vishnu.
# 2  
Old 10-17-2002
strncpy() can copy n bytes. If you want to start at position 3 then you need to skip the first two bytes. So use strncpy(dest, s+2, 2). The first 2 is number of bytes to skip, the second 2 is the number of bytes to copy.
# 3  
Old 10-17-2002
That was great Perderabo... and I learnt a lesson too! I certainly need to put more thought before taking for granted utility of certain standard C functions.. I have seen the strncpy(), but thought I'm restricted to access only from the beginning always...
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Look for substrings with special characters

Hello gurus, I have a lookup table cat tmp1 \\\erw``~ 1 ^774574574565665f\] 2 ()42543^ and I`m trying to compare a bunch of strings such that, either the lookup table column 1, or the string to be looked up are substrings of each other (and return the second lookup column if yes). ... (2 Replies)
Discussion started by: sheetalk
2 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

Extracting substrings from a string of variable length

I have a string like Months=jan feb mar april x y .. Here the number of fields in Months is not definite I need to extract each field in the Months string and pass it to awk . Don't want to use for in since it is a loop . How can i do it (2 Replies)
Discussion started by: Nevergivup
2 Replies

4. Shell Programming and Scripting

Flat file-make field length equal to header length

Hello Everyone, I am stuck with one issue while working on abstract flat file which i have to use as input and load data to table. Input Data- ------ ------------------------ ---- ----------------- WFI001 Xxxxxx Control Work Item A Number of Records ------ ------------------------... (5 Replies)
Discussion started by: sonali.s.more
5 Replies

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

6. Shell Programming and Scripting

extracting substrings

Hi guys, I am stuck in this problem. Please help. I have two files. FILE1 (with records starting from '>' ) >TC1723_3 similar to Scific_A7Q9Q3 EMSPSQDYCDDYFKLTYPCTAGAQYYGRGALPVYWNYNYGAIGEALKLDLLNHPEYIEQN ATMAFQAAIWRWMNPMKKGQPSAHDAFVGNWKP >TC214_2 similar to Quiet_Ref100_Q8W2B2 Cluster;... (1 Reply)
Discussion started by: smriti_shridhar
1 Replies

7. UNIX for Dummies Questions & Answers

What the command to find out the record length of a fixed length file?

I want to find out the record length of a fixed length file? I forgot the command. Any body know? (9 Replies)
Discussion started by: tranq01
9 Replies

8. UNIX for Dummies Questions & Answers

Sed working on lines of small length and not large length

Hi , I have a peculiar case, where my sed command is working on a file which contains lines of small length. sed "s/XYZ:1/XYZ:3/g" abc.txt > xyz.txt when abc.txt contains lines of small length(currently around 80 chars) , this sed command is working fine. when abc.txt contains lines of... (3 Replies)
Discussion started by: thanuman
3 Replies

9. Shell Programming and Scripting

creating a fixed length output from a variable length input

Is there a command that sets a variable length? I have a input of a variable length field but my output for that field needs to be set to 32 char. Is there such a command? I am on a sun box running ksh Thanks (2 Replies)
Discussion started by: r1500
2 Replies
Login or Register to Ask a Question