# To get a single string in output

**URL:** <https://community.unix.com/t/to-get-a-single-string-in-output/370661>\
**Category:** Shell Programming and Scripting\
**Created:** [January 30, 2018, 9:55am UTC](https://community.unix.com/t/to-get-a-single-string-in-output/370661 "2018-01-30T09:55:04Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![mirwasim](https://community.unix.com/letter_avatar/mirwasim/32/6_449c533f586939b6e5f246632a46421f.png) [@mirwasim](https://community.unix.com/u/mirwasim)\
**Post date:** [January 30, 2018, 9:55am UTC](https://community.unix.com/t/to-get-a-single-string-in-output/370661/1 "2018-01-30T09:55:04Z")

</div>

I am looking forward to achive below expecrted result, but when I am trying i could now get the whole string between two `/ /` what I am getting is the last word  
i need to take complete line which is between ` / /` ignore the line after ` , and before / which is (Test failed: text expected not to contain) `

```nohighlight
File input 

SKILLED LANGUAGE - ENTER CREDENTIALS,Test failed: text expected not to contain /You do not have skills or languages/,http://abc:1234/new.xhtml
BILLING - ONLINE BILL DETAILS 3,Test failed: text expected not to contain /FETCH_ONLINE_CURRENT_BILL_DETAILS/,http://abc:1234/new.xhtml
BILLING - ONLINE BILL DETAILS 4,Test failed: text expected not to contain /FETCH_ONLINE_PREVIOUS_BILL_DETAILS/,http://abc:1234/new.xhtml
INTERACTION - SUB ORDERS - VIEW - MANAGED SERVICE 3,Test failed: text expected not to contain /UN_HANDLED_EXCEPTION/,http://abc:1234/new.xhtml

```

```nohighlight
expected out put 

SKILLED LANGUAGE - ENTER CREDENTIALS,You do not have skills or languages,http://abc:1234/new.xhtml
BILLING - ONLINE BILL DETAILS 3,FETCH_ONLINE_CURRENT_BILL_DETAILS,http://abc:1234/new.xhtml
BILLING - ONLINE BILL DETAILS 4, FETCH_ONLINE_PREVIOUS_BILL_DETAILS,http://abc:1234/new.xhtml
INTERACTION - SUB ORDERS - VIEW - MANAGED SERVICE 3,UN_HANDLED_EXCEPTION,http://abc:1234/new.xhtml

```

```nohighlight
script I am using

nawk 'BEGIN{print "<table border="1">"}
       {print "<tr>";
       print "<TD>";
       for(i=1;i<NF;i++) printf("%s ", $i);
       print "</TD>";
       print "<TD>" $NF "</TD>";
       print "</tr>"}
     END{print "</table>"}' ERROR_FILE.log 

```

---

<div class="post-metadata">

**Author:** ![Yoda](https://community.unix.com/user_avatar/community.unix.com/yoda/32/2498_2.png) [@Yoda](https://community.unix.com/u/Yoda)\
**Post date:** [January 30, 2018, 10:21am UTC](https://community.unix.com/t/to-get-a-single-string-in-output/370661/2 "2018-01-30T10:21:22Z")

</div>

Using sed:-

```nohighlight
sed 's#,[^/]*/#,#;s#/##' file

```

---

<div class="post-metadata">

**Author:** ![rbatte1](https://community.unix.com/letter_avatar/rbatte1/32/6_449c533f586939b6e5f246632a46421f.png) [@rbatte1](https://community.unix.com/u/rbatte1)\
**Post date:** [January 30, 2018, 10:25am UTC](https://community.unix.com/t/to-get-a-single-string-in-output/370661/3 "2018-01-30T10:25:50Z")

</div>

I'm not very good with awk/nawk, but do you not need to set the field separator with the `-F` flag. It might be complicated because you have two delimiters or which one is `/` and also used in the literal text. The other being `,`

If it was in bash I would write something like:-

```nohighlight
while IFS="/," read f1 f2 f3 f4 f5
do
   echo "${f1},${f3},${f5}
done < ERROR_FILE.log

```

The output for me is:-

```nohighlight
SKILLED LANGUAGE - ENTER CREDENTIALS,You do not have skills or languages,http://abc:1234/new.xhtml
BILLING - ONLINE BILL DETAILS 3,FETCH_ONLINE_CURRENT_BILL_DETAILS,http://abc:1234/new.xhtml
BILLING - ONLINE BILL DETAILS 4,FETCH_ONLINE_PREVIOUS_BILL_DETAILS,http://abc:1234/new.xhtml
INTERACTION - SUB ORDERS - VIEW - MANAGED SERVICE 3,UN_HANDLED_EXCEPTION,http://abc:1234/new.xhtml

```

With a large file, the performance could be much slower than an awk or perhaps sed

I hope that this helps,  
Robin

---

<div class="post-metadata">

**Author:** ![mirwasim](https://community.unix.com/letter_avatar/mirwasim/32/6_449c533f586939b6e5f246632a46421f.png) [@mirwasim](https://community.unix.com/u/mirwasim)\
**Post date:** [January 30, 2018, 10:46am UTC](https://community.unix.com/t/to-get-a-single-string-in-output/370661/4 "2018-01-30T10:46:52Z")

</div>

> [@yoda](#):
>
> Using sed:-
> 
> ```plaintext
> sed 's#,[^/]*/#,#;s#/##' file
> 
> ```

thanks for your feedback ,but i will need in `awk` as i need the output to send in email in same script how i mentioned above.  
any modification in above script will be appreciated

---------- Post updated at 09:16 PM ---------- Previous update was at 09:15 PM ----------

> [@rbatte1](#):
>
> I'm not very good with awk/nawk, but do you not need to set the field separator with the `-F` flag. It might be complicated because you have two delimiters or which one is `/` and also used in the literal text. The other being `,`
> 
> If it was in bash I would write something like:-
> 
> ```plaintext
> while IFS="/," read f1 f2 f3 f4 f5
> do
> echo "${f1},${f3},${f5}
> done < ERROR_FILE.log
> 
> ```
> 
> The output for me is:-
> 
> ```plaintext
> SKILLED LANGUAGE - ENTER CREDENTIALS,You do not have skills or languages,http://abc:1234/new.xhtml
> BILLING - ONLINE BILL DETAILS 3,FETCH_ONLINE_CURRENT_BILL_DETAILS,http://abc:1234/new.xhtml
> BILLING - ONLINE BILL DETAILS 4,FETCH_ONLINE_PREVIOUS_BILL_DETAILS,http://abc:1234/new.xhtml
> INTERACTION - SUB ORDERS - VIEW - MANAGED SERVICE 3,UN_HANDLED_EXCEPTION,http://abc:1234/new.xhtml
> 
> ```
> 
> With a large file, the performance could be much slower than an awk or perhaps sed
> 
> I hope that this helps,  
> Robin

thanks for your feedback ,but i will need in `awk ` as i need the output to send in email in same script how i mentioned above.  
any modification in above script will be appreciated

---

<div class="post-metadata">

**Author:** ![Yoda](https://community.unix.com/user_avatar/community.unix.com/yoda/32/2498_2.png) [@Yoda](https://community.unix.com/u/Yoda)\
**Post date:** [January 30, 2018, 11:13am UTC](https://community.unix.com/t/to-get-a-single-string-in-output/370661/6 "2018-01-30T11:13:41Z")

</div>

Just need to use the same regex in nawk:-

```nohighlight
nawk -F, '
        BEGIN {
                print "<html>"
                print "<body>"
                print "<table border=1>"
        }
        {
                print "<tr>"
                for ( i = 1; i <= NF; i++ )
                {
                        if ( i == 2 )
                        {
                                sub ( /[^/]*\//, X, $i )
                                sub ( /\//, X, $i )
                        }
                        print "<td>" $i "</td>"
                }
                print "</tr>"
        }
        END {
                print "</table>"
                print "</body>"
                print "</html>"
        }
' file

```

---

<div class="post-metadata">

**Author:** ![mirwasim](https://community.unix.com/letter_avatar/mirwasim/32/6_449c533f586939b6e5f246632a46421f.png) [@mirwasim](https://community.unix.com/u/mirwasim)\
**Post date:** [January 30, 2018, 11:43am UTC](https://community.unix.com/t/to-get-a-single-string-in-output/370661/7 "2018-01-30T11:43:16Z")

</div>

> [@yoda](#):
>
> Just need to use the same regex in nawk:-
> 
> ```plaintext
> nawk -F, '
> BEGIN {
> print "<html>"
> print "<body>"
> print "<table border=1>"
> }
> {
> print "<tr>"
> for ( i = 1; i <= NF; i++ )
> {
> if ( i == 2 )
> {
> sub ( /[^/]*\//, X, $i )
> sub ( /\//, X, $i )
> }
> print "<td>" $i "</td>"
> }
> print "</tr>"
> }
> END {
> print "</table>"
> print "</body>"
> print "</html>"
> }
> ' file
> 
> ```

I am getting below error

```plaintext
nawk: extra ] at source line 13
 context is
                                   sub ( >>> /[^/] <<< *\//, X, $i )
nawk: nonterminated character class [^
 source line number 13

```

---------- Post updated at 10:13 PM ---------- Previous update was at 10:06 PM ----------

> [@yoda](#):
>
> Using sed:-
> 
> ```plaintext
> sed 's#,[^/]*/#,#;s#/##' file
> 
> ```

you script worked as expected by when i am running in as below the output is in single ROW, how can i get three lines in three rowS and so on

```plaintext
echo "<br />" > ,ERROR_FILE.html
echo "<html>" >> ERROR_FILE.htm
echo "<Body>" >> ERROR_FILE.html
sed 's#,[^/]*/#,#;s#/##' ERROR_FILE.log >> ERROR_FILE.html ##URLs with ports which are having error
echo "</Body>" >> ERROR_FILE.html
echo "</html>" >> ERROR_FILE.html

```

---

<div class="post-metadata">

**Author:** ![Yoda](https://community.unix.com/user_avatar/community.unix.com/yoda/32/2498_2.png) [@Yoda](https://community.unix.com/u/Yoda)\
**Post date:** [January 30, 2018, 11:49am UTC](https://community.unix.com/t/to-get-a-single-string-in-output/370661/8 "2018-01-30T11:49:33Z")

</div>

Ok, escape it:-

```nohighlight
sub ( /[^\/]*\//, X, $i )

```

---

<div class="post-metadata">

**Author:** ![Scrutinizer](https://community.unix.com/user_avatar/community.unix.com/scrutinizer/32/1216_2.png) [@Scrutinizer](https://community.unix.com/u/Scrutinizer)\
**Post date:** [January 30, 2018, 3:01pm UTC](https://community.unix.com/t/to-get-a-single-string-in-output/370661/9 "2018-01-30T15:01:27Z")

</div>

Or alternatively use a regex string:

```nohighlight
sub ( "[^/]*/", X, $i )
sub ( "/", X, $i )

```

---

<div class="post-metadata">

**Author:** ![mirwasim](https://community.unix.com/letter_avatar/mirwasim/32/6_449c533f586939b6e5f246632a46421f.png) [@mirwasim](https://community.unix.com/u/mirwasim)\
**Post date:** [January 31, 2018, 2:09pm UTC](https://community.unix.com/t/to-get-a-single-string-in-output/370661/10 "2018-01-31T14:09:15Z")

</div>

> [@yoda](#):
>
> Ok, escape it:-
> 
> ```plaintext
> sub ( /[^\/]*\//, X, $i )
> 
> ```

Thanks Yoda,  
i have added the header also to the script you provided, it is working fine, but I am expecting to get the header over those rows for which the URL or port changes. URL will remain same for few rows and then it change, and once the URL change the header should come,  
like in below input you can see the 3 record URL changed.so the header should populate over the third record(validate only server and port )

```plaintext
INPUT FILE:
SKILLED LANGUAGE - ENTER CREDENTIALS,You do not have skills or languages,http://xxx:7003/abc/protected/home.xhtml

NOT KNOWN - NEW VALUE,You do not have skills or languages,http://xxx:7003/abc/protected/home.xhtml
SKILLED LANGUAGE - ENTER CREDENTIALS,You do not have skills or languages,http://xxx:7005/abc/protected/home.xhtml

```

```plaintext
nawk -F, '
        BEGIN {
	        print "<html>"
                print "<body>"
                print "<table border=3>"        
                                print "<tr bgcolor=#85C1E9>"
                                print "<th>FUNCATIONALITY</th>"
                                print "<th>ERROR</th>"
                                print "<th>URL</th>"
                                print "</tr>"
        }
        {
                print "<tr>"
                for ( i = 1; i <= NF; i++ )
                {
                        if ( i == 2 )
                        {
                                sub ( /[^\/]*\//, X, $i )
                                sub ( /\//, X, $i )
                        }
                        print "<td>" $i "</td>"
                }
                print "</tr>"
        }
        END {
                print "</table>"
                print "</body>"
                print "</html>"
        }
                ' filename 
		

```
