Position of a string


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Position of a string
# 1  
Old 09-21-2007
Position of a string

Can some one help me,to find the postion of the string in the files for all the files in the directory.

Thanks in advance...
# 2  
Old 09-21-2007
This is beyond vague. Please give us one example.
# 3  
Old 09-21-2007
ok,I want to find the postion of a string in all the files in the directory.

For Example:
The string below is in a file:
1234 345 678 the

I want to find the postion of the word/string "the".That will be 14.

Hope I am clear this time...
# 4  
Old 09-21-2007
what if it is like this:

1234 345 678 the the

do you want to find the position of both?

or what about

1234 345 678 the their other
now "the" is repeated three times.
# 5  
Old 09-22-2007
YEs, their might be multiple occurences of "the" in a file.And I want the starting postion of all.But the word("the") will occur only once in a line.
For example:

asdshdjsh the--11
sdahsdhaskj the --13
aksjdhashahskshjjsdfjd the--24

Thank you!!
# 6  
Old 09-22-2007
Try this out.....

cat noname.txt
asdshdjsh the
sdahsdhaskj the
aksjdhashahskshjjsdfjd the


awk -v find=$var '{ printf ("%s %s %s %s %s %s\n", "The index of search value ", find, "in", $0, "is:-", index($0,find) ) }' noname.txt

NOTE: var is the parameter which you want to search.

o/p
The index of search value the in asdshdjsh the is:- 11
The index of search value the in sdahsdhaskj the is:- 13
The index of search value the in aksjdhashahskshjjsdfjd the is:- 24

Last edited by manas_ranjan; 09-22-2007 at 10:19 AM..
# 7  
Old 12-14-2008
Hi,

Not sure if I am allowed to write a query from a thread over one year ago , but it was most relevant to the help I needed.Smilie

The last piece of code is very helpful, but what if one wants to search for the positions of multiple words on each line of file..e.g.

if it is like this:

1234345678upthehappytheir

I want to find index of both "the", which should be : 11 and then 19

Cheers! I hope someone would still read this old thread Smilie
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Find a string and its position in a line from another string

Hello guys, would you please help me with this? this is the line inside a file: first line Something Today YYDDPPSVXIPYYY0XXXOFFS00000000000? I'd like to find the position of string XXX from string PYYY In the example above XXX starts from 6th position from PYYY desired... (4 Replies)
Discussion started by: netrom
4 Replies

2. Shell Programming and Scripting

Search for a string at a particular position and replace with blank based on position

Hi, I have a file with multiple lines(fixed width dat file). I want to search for '02' in the positions 45-46 and if available, in that lines, I need to replace value in position 359 with blank. As I am new to unix, I am not able to figure out how to do this. Can you please help me to achieve... (9 Replies)
Discussion started by: Pradhikshan
9 Replies

3. Shell Programming and Scripting

Grep for a string at a certain position

trying to grep for a string at a certain position in a commands output in order to use it as a variable: route Kernel IP routing table Destination Gateway Genmask Flags Metric Ref Use Iface default 192.168.2.1 0.0.0.0 UG 0 0 0 wlan1... (4 Replies)
Discussion started by: 3therk1ll
4 Replies

4. Shell Programming and Scripting

Search a string in a text file and add another string at the particular position of a line

I am having a text file which is having more than 200 lines. EX: 001010122 12000 BIB 12000 11200 1200003 001010122 2000 AND 12000 11200 1200003 001010122 12000 KVB 12000 11200 1200003 In the above file i want to search for string KVB and add/replace... (1 Reply)
Discussion started by: suryanarayana
1 Replies

5. Shell Programming and Scripting

sed or awk command to replace a string pattern with another string based on position of this string

here is what i want to achieve... consider a file contains below contents. the file size is large about 60mb cat dump.sql INSERT INTO `table1` (`id`, `action`, `date`, `descrip`, `lastModified`) VALUES (1,'Change','2011-05-05 00:00:00','Account Updated','2012-02-10... (10 Replies)
Discussion started by: vivek d r
10 Replies

6. UNIX for Dummies Questions & Answers

Search a string in the file and then replace another string after that position

Hi I am looking for a particular string in a file.If the string exists, then I want to replace another string with some other text.Once replaced, search for the same text after that character position in the file. :wall: E.g: Actual File content: Hello Name: Nitin Raj Welcome to Unix... (4 Replies)
Discussion started by: dashing201
4 Replies

7. UNIX for Dummies Questions & Answers

Search for a string and replace the searched string in the same position in samefile

Hi All, My requisite is to search for the string "0108"(which is the year and has come in the wrong year format) in a particular column say 4th column in a tab delimited file and then replace it with 2008(the correct year format) in the same position where 0108 was found in the same file..The... (27 Replies)
Discussion started by: ganesh_248
27 Replies

8. Shell Programming and Scripting

Search for a string and replace the searched string in the same position

Hi All, My requisite is to search for the string "0108"(which is the year and has come in the wrong year format) in a particular column say 4th column in a tab delimited file and then replace it with 2008(the correct year format) in the same position where 0108 was found..The issue is the last... (15 Replies)
Discussion started by: ganesh_248
15 Replies

9. Shell Programming and Scripting

Find the position of a string and replace with another string

Hi, I have a file named "Test_2008_01_21" The file contains a string "manual" that occurs many times in the file How can i find the positions of the string "manual" in the file Ex: if the string " manual " occurs three times in the file. i want to replace the second occurance of string... (6 Replies)
Discussion started by: bab123
6 Replies

10. Shell Programming and Scripting

how to find a position and print some string in the next and same position

I need a script for... how to find a position of column data and print some string in the next line and same position position should find based on *HEADER8* in text for ex: ord123 abs 123 987HEADER89 test234 ord124 abc 124 987HEADER88 test235 ... (1 Reply)
Discussion started by: naveenkcl
1 Replies
Login or Register to Ask a Question