# Need to filter string between specific string in ksh

**URL:** <https://community.unix.com/t/need-to-filter-string-between-specific-string-in-ksh/376449>\
**Category:** Shell Programming and Scripting\
**Created:** [October 16, 2019, 1:19am UTC](https://community.unix.com/t/need-to-filter-string-between-specific-string-in-ksh/376449 "2019-10-16T01:19:37Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![mohtashims](https://community.unix.com/letter_avatar/mohtashims/32/6_449c533f586939b6e5f246632a46421f.png) [@mohtashims](https://community.unix.com/u/mohtashims)\
**Post date:** [October 16, 2019, 1:19am UTC](https://community.unix.com/t/need-to-filter-string-between-specific-string-in-ksh/376449/1 "2019-10-16T01:19:37Z")

</div>

My argument has data as below.

```nohighlight
10.9.9.85
-rwxr-xr-x user1 2019-10-15 17:40 /app/scripts/testingscr5.scr 127869538
-rwxr-xr-x user1 2019-10-15 17:40 /app/scripts/testingscr56scr 127869538
....... (note all these between lines will start with hyphen '-' )
-rwxr-xr-x user1 2019-10-15 17:40 /app/scripts/testingscr9.scr 127869538
10.9.9.140
-rwxr-xr-x user1 2019-10-15 17:40 /app/scripts/testingscr5.scr 127869538
-rwxr-xr-x user1 2019-10-15 17:40 /app/scripts/testingscr6.scr 127869538
....... (note all these between lines will start with hyphen '-' )
-rwxr-xr-x user1 2019-10-15 17:40 /app/scripts/testingscr9.scr 127869538
myhost1
-rwxr-xr-x user1 2019-10-15 17:40 /app/scripts/testingscr5.scr 127869538
-rwxr-xr-x user1 2019-10-15 17:40 /app/scripts/testingscr6.scr 127869538
....... (note all these between lines will start with hyphen '-' )
-rwxr-xr-x user1 2019-10-15 17:40 /app/scripts/testingscr9.scr 127869538
..... (and so on)

```

This is how i run the script:

```nohighlight
./readfile.sh '10.9.9.85\\n-rwxr-xr-x user1 2019-10-15 17:40 /app/scripts/testingscr5.scr 127869538\\n-rwxr-xr-x user1 2019-10-15 17:40 /app/mrt/testingscr6.mrt 4119663872\\n-rwxr-xr-x user1 2019-10-15 17:40 /app/exe/testingscr9.exe 1799452894\\n10.9.9.140\\n-rwxr-xr-x user1 2019-10-15 17:40 /app/scripts/testingscr5.scr 127869538\\n-rwxr-xr-x user1 2019-10-15 17:40 /app/mrt/testingscr6.mrt 4119663872\\n-rwxr-xr-x user1 2019-10-15 17:40 /app/exe/testingscr9.exe 1799452894'

```

I wish to get the the filenames between the first IP/hostname and the next IP/hostname only i.e between the first and second IP / Host specified.

Desired Output:

```nohighlight
/app/scripts/testingscr5.scr
/app/scripts/testingscr6.scr
.......
 /app/scripts/testingscr9.scr

```

below is what i did so far:

```nohighlight
#!/bin/ksh
VAR1=`echo "${1}" | sed -e "s/'//g"`
echo "${VAR1}" |
  while IFS=' '
read -r dirpath
do
  fileis=`echo "$dirpath" | awk -F' ' '{ print $5 }'`
  echo "FILE IS:$fileis"
 done

```

The above prints all file names but I m not able to get the logic of how can I filter filenames between the first IP/Hostname and the SecondOne only ... store it in a variable.

Note: There maybe a case where we do not have a second IP / Hostname in which case we need to get the filename after the first IP / Hostname only.

Tip: we can look for strings starting with hyphen '-' to know if that is a file entry.

I'm using ksh shell on AiX 6.1

---

<div class="post-metadata">

**Author:** ![mohtashims](https://community.unix.com/letter_avatar/mohtashims/32/6_449c533f586939b6e5f246632a46421f.png) [@mohtashims](https://community.unix.com/u/mohtashims)\
**Post date:** [October 16, 2019, 7:43am UTC](https://community.unix.com/t/need-to-filter-string-between-specific-string-in-ksh/376449/2 "2019-10-16T07:43:33Z")

</div>

Resolved the issue by this logic

```nohighlight
VAR1=`echo "${1}" | sed -e "s/'//g"`
echo "${VAR1}" |
  while IFS=' '
read -r dirpath
do
        if [$tmp -le 1]; then
  fileis=`echo "$dirpath" | awk -F' ' '{ print $5 }'`
  echo "FILE IS:$fileis"
         if [["$fileis" == "/"*]]; then
        echo "This is the File:$fileis"
        filelist[$count]="$fileis"
         count=`expr $count + 1`
        echo "INCREMENT VALUE:$count"
        else
         tmp=`expr $tmp + 1`
         echo "INCREMENT BY:$tmp"
         fi
           else
         break;
          fi   
done

```

---

<div class="post-metadata">

**Author:** ![Neo](https://community.unix.com/user_avatar/community.unix.com/neo/32/22838_2.png) [@Neo](https://community.unix.com/u/Neo)\
**Post date:** [October 16, 2019, 7:43am UTC](https://community.unix.com/t/need-to-filter-string-between-specific-string-in-ksh/376449/3 "2019-10-16T07:43:55Z")

</div>

Trivial example using PHP:

```nohighlight
cat example.php

```

```nohighlight
<?php

$stuff = '-rwxr-xr-x user1 2019-10-15 17:40 /app/scripts/testingscr5.scr 127869538
-rwxr-xr-x user1 2019-10-15 17:40 /app/scripts/testingscr56.scr 127869538
-rwxr-xr-x user1 2019-10-15 17:40 /app/scripts/testingscr9.scr 127869538
-rwxr-xr-x user1 2019-10-15 17:40 /app/scripts/testingscr5.scr 127869538
-rwxr-xr-x user1 2019-10-15 17:40 /app/scripts/testingscr6.scr 127869538
-rwxr-xr-x user1 2019-10-15 17:40 /app/scripts/testingscr9.scr 127869538
-rwxr-xr-x user1 2019-10-15 17:40 /app/scripts/testingscr5.scr 127869538
-rwxr-xr-x user1 2019-10-15 17:40 /app/scripts/testingscr6.scr 127869538
-rwxr-xr-x user1 2019-10-15 17:40 /app/scripts/testingscr9.scr 127869538';

foreach(preg_split("/((\r?\n)|(\r\n?))/", $stuff) as $line)
{
$pieces = explode(' ',$line);
echo $pieces[4] . "\n" ;
}

```

```nohighlight
osx$ php example.php
/app/scripts/testingscr56.scr
/app/scripts/testingscr9.scr
/app/scripts/testingscr5.scr
/app/scripts/testingscr6.scr
/app/scripts/testingscr9.scr
/app/scripts/testingscr5.scr
/app/scripts/testingscr6.scr
/app/scripts/testingscr9.scr

```

Modify as you please.

---

<div class="post-metadata">

**Author:** ![RudiC](https://community.unix.com/letter_avatar/rudic/32/6_449c533f586939b6e5f246632a46421f.png) [@RudiC](https://community.unix.com/u/RudiC)\
**Post date:** [October 16, 2019, 9:20am UTC](https://community.unix.com/t/need-to-filter-string-between-specific-string-in-ksh/376449/4 "2019-10-16T09:20:07Z")

</div>

How about

```nohighlight
echo "${1//\\\\n/$'\n'}" | awk '$5 {print "File is:", $5}'
File is: /app/scripts/testingscr5.scr
File is: /app/mrt/testingscr6.mrt
File is: /app/exe/testingscr9.exe
File is: /app/scripts/testingscr5.scr
File is: /app/mrt/testingscr6.mrt
File is: /app/exe/testingscr9.exe

```

---

<div class="post-metadata">

**Author:** ![system](https://community.unix.com/uploads/default/original/2X/c/c7e9e16a595259edafe88615719d5c5dcda035c2.png) [@system](https://community.unix.com/u/system)\
**Post date:** [May 25, 2020, 6:35pm UTC](https://community.unix.com/t/need-to-filter-string-between-specific-string-in-ksh/376449/5 "2020-05-25T18:35:33Z")

</div>

Automatically bumped on May 25, 2020, 6:35pm UTC

Moderator comments were removed during original forum migration.

---

<div class="post-metadata">

**Author:** ![system](https://community.unix.com/uploads/default/original/2X/c/c7e9e16a595259edafe88615719d5c5dcda035c2.png) [@system](https://community.unix.com/u/system)\
**Post date:** [January 17, 2024, 5:28pm UTC](https://community.unix.com/t/need-to-filter-string-between-specific-string-in-ksh/376449/6 "2024-01-17T17:28:25Z")

</div>

Automatically bumped on January 17, 2024, 5:28pm UTC


