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


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Find a string and its position in a line from another string
# 1  
Old 02-27-2017
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:


Code:
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 result = 6


Another file, another line

Code:
TODAY YYDDPPSVXIPYYY000XXXOFFS00000


desired result = 8

==

Which command to use to find out the position of XXX from PYYY?


Thanks a lot,
Marian
Moderator's Comments:
Mod Comment Please use CODE tags (not B tags) to display sample input, sample output, and code segments.

Last edited by Don Cragun; 02-27-2017 at 05:55 AM.. Reason: Change B tags to CODE tags; add ICODE tags.
# 2  
Old 02-27-2017
You could try something like:
Code:
awk 'match($0, /PYY.*XXX/){printf("%d %s\n", RLENGTH - 2, substr($0, RSTART, RLENGTH))}' file

which, if file contains the text found in post #1 in this thread, produces the output:
Code:
6 PYYY0XXX
8 PYYY000XXX

If you want to try this on a Solaris/SunOS system, change awk to /usr/xpg4/bin/awk or nawk.
This User Gave Thanks to Don Cragun For This Post:
# 3  
Old 02-27-2017
Try
Code:
awk '{print index ($0, STR2) - index ($0, STR1) + 1}' STR1="PYYY" STR2="XXX" file
6
8

This User Gave Thanks to RudiC For This Post:
# 4  
Old 02-27-2017
thanks a lot, it works :-)
# 5  
Old 02-27-2017
Because i am a closet-show-off (and because the almighy Korn shell is really the only tool one ever needs ;-)) ) here is a solution in pure ksh, call it like ./script "string" "pattern":

Code:
#! /bin/ksh

typeset    chStr="$1"
typeset    chPattern="$2"

typeset -i iStrIdx=1
typeset -i iPatLen=${#chPattern}
typeset -i iStrLen=$(( ${#chStr} - iPatLen + 1 ))

while [ $iStrIdx -le $iStrLen ] ; do
     if [ "${chStr#${chPattern}}" != "${chStr}" ] ; then
          print - "$chPattern found at position $iStrIdx"
          exit 0
     fi
     (( iStrIdx += 1 ))
     chStr="${chStr#?}"
done

print - "Pattern $chPattern not found in String"
exit 1

This script is supposed to be called with the string and the pattern on the command line. With the following slight modification it will work inside a pipeline instead, called like cat file | ./script "pattern" (cat file represents any datastream here):

Code:
#! /bin/ksh

typeset    chPattern="$1"

typeset -i iStrIdx=1
typeset -i iPatLen=${#chPattern}
typeset -i iStrLen=0
typeset -i iLineCnt=0
typeset -i lFound=0

while read chStr ; do
     iStrIdx=1
     iStrlen=$(( ${#chStr} - iPatLen + 1 ))
     (( iLineCnt += 1 ))
     (( lFound = 0 ))
     while [ $iStrIdx -le $iStrLen ] ; do
          if [ "${chStr#${chPattern}}" != "${chStr}" ] ; then
               print - "$chPattern found at position $iStrIdx in line $iLineCnt"
               (( iStrIdx = iStrLen )) # skip rest of string
               (( lFound = 1 ))
          else
               (( iStrIdx += 1 ))
               chStr="${chStr#?}"
          fi
     done
     if ! (( lFound )) ; then
          print - "$chPattern not found in line $iLineCnt"
     fi
done

exit 0

To make the pattern equally dynamic as the input (i.e. the patterns to be read from a stream either) is left as an exercise to the reader.

I hope this helps.

bakunin

Last edited by bakunin; 02-27-2017 at 01:31 PM..
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

To find nth position of character in string

Hi guyz i want to know nth position of character in string. For ex. var="UK,TK,HK,IND,AUS" now if we see 1st occurance of , is at 3 position, 2nd at 6,..4th at 13 position. 1st position we can find through INDEX, but what about 2nd,3rd and 4th or may be upto nth position. ? In oracle we had... (2 Replies)
Discussion started by: Jonty Immortal
2 Replies

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

3. Shell Programming and Scripting

Search string position in a line

WeeksD1|$26.52|$26.52|03/02/2013|12.48|D1|1.09|0|0|0|0|0|.95|0|0|0|0|0|0|0|0|0|0|0|0|0|0||$0.00|$0.00||0|0|0|0|0|0|0|0|0|0|0|0|0|0|0|0|0|0|0|0|0|0||$0.00|$0.00||0|0|0|0|0|0|0|0|0|0|0|0|0|0|0|0|0|0|0|0|0|0||$0.00|$0.00||0|0|0|0|0|0|0|0|0|0|0|0|0|0|0|0|0|0|0|0|0|0|$0.00|$0.00|$0.00|$0.00|$0.00|$0.00|$... (3 Replies)
Discussion started by: gavemani
3 Replies

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

5. UNIX for Dummies Questions & Answers

Find the list of filenames that have the string 31 at 4th and 5th position

Hi, Can anyone let me know the command to know the list of filenames that have string 31 in their 4th and 5th positions inside the file: grep -l "31" main*.txt The above grep lists all the files which have 31 at any position but I want filenames having 31 at position 4 and position 5. (8 Replies)
Discussion started by: okkadu
8 Replies

6. Shell Programming and Scripting

Find the position of lines matching string

I have a file with the below format, GS*8***** ST*1******** A* B* E* RMR*123455(This is the unique number to locate this row) F* SE*1*** GE** GS*9***** ST*2 H* J* RMR*567889(This is the unique number to locate this row) L* SE* GE***** (16 Replies)
Discussion started by: Muthuraj K
16 Replies

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

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

9. Shell Programming and Scripting

how to find substring position in a given string

Hello, I have a string like str = "14: Jan 29 13:27:12 : Processor----: : Start of splitting file " from this, i have to find the position or location number starting for "Processor". I have to extract date from this entire string. string which i will give will not have fixed length. ... (2 Replies)
Discussion started by: balareddy
2 Replies
Login or Register to Ask a Question