Sponsored Content
Top Forums Shell Programming and Scripting Find a string and its position in a line from another string Post 302992546 by bakunin on Monday 27th of February 2017 09:03:27 AM
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..
 

9 More Discussions You Might Find Interesting

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

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

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

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

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

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

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

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

9. 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
ttk::frame(n)							 Tk Themed Widget						     ttk::frame(n)

__________________________________________________________________________________________________________________________________________________

NAME
ttk::frame - Simple container widget SYNOPSIS
ttk::frame pathName ?options? _________________________________________________________________ DESCRIPTION
A ttk::frame widget is a container, used to group other widgets together. STANDARD OPTIONS
-class -cursor -takefocus -style See the ttk_widget manual entry for details on the standard options. WIDGET-SPECIFIC OPTIONS [-borderwidth borderWidth] The desired width of the widget border. Defaults to 0. [-relief relief] One of the standard Tk border styles: flat, groove, raised, ridge, solid, or sunken. Defaults to flat. [-padding padding] Additional padding to include inside the border. [-width width] If specified, the widget's requested width in pixels. [-height height] If specified, the widget's requested height in pix- els. WIDGET COMMAND
Supports the standard widget commands configure, cget, identify, instate, and state; see ttk::widget(n). NOTES
Note that if the pack, grid, or other geometry managers are used to manage the children of the frame, by the GM's requested size will nor- mally take precedence over the frame widget's -width and -height options. pack propagate and grid propagate can be used to change this. SEE ALSO
ttk::widget(n), ttk::labelframe(n), frame(n) KEYWORDS
widget, frame, container Tk 8.5 ttk::frame(n)
All times are GMT -4. The time now is 02:37 PM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy