    <rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:sy="http://purl.org/rss/1.0/modules/syndication/" xmlns:admin="http://webns.net/mvcb/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:content="http://purl.org/rss/1.0/modules/content/">
     <channel>
        <title>Linux.com :: parsing config file via bash script</title>
        <link>http://www.linux.com/archive/forums/topic/1724</link>
        <description>Everything Linux and Open Source</description>
        <dc:language>en-us</dc:language> 
        <dc:creator>Linux.com Admin</dc:creator> 
        <admin:generatorAgent rdf:resource="http://www.linux.com" /> 
        <admin:errorReportsTo rdf:resource="mailto:noreply@linux.com" />
       <sy:updatePeriod>hourly</sy:updatePeriod>
       <sy:updateFrequency>1</sy:updateFrequency>
       <docs>http://backend.userland.com/rss</docs>



<div class="xarbb-noteswrapper">
</div>

   <div class="xarbb-noteswrapper">
       <div class="xar-sub rightnote">
           <div>
                    <a href="http://www.linux.com/archive/forums/topic/2087">
Previous Topic</a>
|
                    <a href="http://www.linux.com/archive/forums/topic/2027">
Next Topic</a>
</div>

           <div>
                    <a href="http://www.linux.com/archive/forums/printtopic/1724?theme=print">
Print this topic</a> |
You are not subscribed            </div>
        </div>
        <div style="clear: both;"></div>
    </div>

    <h1>
        <a class="xar-title" href="http://www.linux.com/archive/forums/topic/1724">parsing config file via bash script</a>
   </h1>


   <div class="xarbb-topiccontrols">
<form action="http://www.linux.com/archive/forums?func=jump" method="post">
   <label for="xarbb_jump_menu">Forum Jump</label>
    <select name="f" id="xarbb_jump_menu" onchange="this.form.submit();">
        <optgroup label="Options">
            <option value="5" selected="selected">
Please select forum            </option>
            <option value="5">--------------------</option>
        </optgroup>
        <optgroup label="Forums">
                <option value="1">New to Linux</option>
                <option value="2">Hardware</option>
                <option value="3">Linux Distributions</option>
                <option value="4">Applications</option>
                <option value="5">Programming and Development</option>
                <option value="6">Miscellaneous Discussion</option>
                <option value="7">Linux.com Writers Forum</option>
                <option value="8">Jobs</option>
        </optgroup>
    </select>
    <input type="submit" class="button" value="Jump" />
</form>
<div style="clear: both;"></div>
   </div>

   <div class="xarbb-topiccontrols">

<span class="xar-sub">
    <a href="http://www.linux.com/archive/forums">
Forum Index    </a>
    <span style="font-size: larger;">&#187;</span>
    <a href="http://www.linux.com/archive/forums/category/4181">
        Forums 
    </a>
    <span style="font-size: larger;">&#187;</span>
    <a href="http://www.linux.com/archive/forums/forum/5">
        Programming and Development   </a>
</span>
<div style="clear: both;"></div>
   </div>

<div class="xarbb-tablewrapper">



   <table>
       <tr>
           <th scope="col">
Author</th>
           <th scope="col">
Message</th>
       </tr>



            <tr>
                <td class="xar-norm author">
                    <div>
                        <a href="http://www.linux.com/archive/roles/402949">asmerm</a>
                   </div>
                   <div class="xar-sub">
Joined: May 12, 2008<br/>Posts: 5                        <br />
                        <a href="http://www.linux.com/archive/forums?by=402949&amp;func=searchtopics">
Other Topics</a>
                   </div>
               </td>

               <td class="xar-norm message">
                   <div class="xarbb-messagecontrols">
                       <div class="leftnote">
                           <span class="xar-sub">
Posted May 12, 2008 at 2:45:40 PM</span>
                       </div>



<div style="float: left;">
                           <strong class="xar-sub">
Subject: parsing config file via bash script                            </strong>
                        </div>
                        <div style="clear: both;"></div>
                    </div>

                    <div>
                        Hello to all Linux users! I am new to bash scripting, but an old Linux user. I have to make bash script which will return only the last row from this txt file:

one|two|three
four|five|six
seven|eight|nine

so that result will be:

three
six
nine

&quot;|&quot; are delimiters. Of course it is not necessary, it can be any other character than &quot;|&quot;.

If anyone knows please respond. Thank you!</div>

                </td>
            </tr>

            <tr>
                <td class="xar-norm author">
                    <a href="http://www.linux.com/archive/forums/topic/1724#top">
Back to top</a>
</td>
               <td class="xar-alt">

                        <a href="http://www.linux.com/archive/roles/402949">
                            <img src="modules/xarbb/xarimages/new/icon_profile.gif" alt="Profile" />
                        </a>
                        <a href="http://www.linux.com/archive/?module=roles&amp;func=email&amp;uid=402949">
                            <img src="modules/xarbb/xarimages/new/icon_email.gif" alt="Email" />
                       </a>
                            <a href="http://">
                                <img src="modules/xarbb/xarimages/new/icon_www.gif" alt="Website" />
                           </a>
</td>
           </tr>





           <tr>
               <td class="xar-norm author">
                   <strong>

                            <a href="http://www.linux.com/archive/roles/401955">
                                Khabi                           </a>
</strong>

                   <span class="xar-sub">
<br />
Joined Apr 21, 2008<br/>Posts: 121<br />
                        <br />
                        <a href="http://www.linux.com/archive/forums?by=401955&amp;func=searchtopics">
Other Topics                        </a>
                    </span>
                </td>

                <td class="xar-accent message">
                   <div class="xarbb-messagecontrols">
                       <div class="leftnote">
                           <span class="xar-sub">
Posted:
                                May 12, 2008 7:13:08 PM                           </span>
                       </div>
                       <div class="rightnote">
</div>
                       <div style="float:left;">
                           <strong class="xar-sub">
Subject:
                                <a name="1189726" href="http://www.linux.com/archive/forums/topic/1724#1189726">
                                    Re:  parsing config file via bash script                                </a>
                            </strong>
                        </div>
                        <div style="clear: both;"></div>
                    </div>

                    <div style="clear: left;">
                        Simple way is to use awk.

if all of those lines were in a file called test.txt  this one line would print out the last column:

awk -F'|' '{print $NF}' test.txt

-F being the filed delimiter (in this case |)
$NF telling it to be the last column (you can make that $2 or any other number to tell it specifically what column you want)

:)
[Modified by: Khabi on May 12, 2008 07:13 PM]
                    </div>
                </td>
            </tr>

            <tr>
                <td class="xar-norm author">
                    <a href="http://www.linux.com/archive/forums/topic/1724#top">
Back to top</a>
</td>

               <td class="xar-alt">

                            <a href="http://www.linux.com/archive/roles/401955">
                                <img src="modules/xarbb/xarimages/new/icon_profile.gif" alt="Profile" />
                            </a>
                            <a href="http://www.linux.com/archive/?module=roles&amp;func=email&amp;uid=401955">
                                <img src="modules/xarbb/xarimages/new/icon_email.gif" alt="Email" />
                           </a>
                                <a href="http://">
                                    <img src="modules/xarbb/xarimages/new/icon_www.gif" alt="Website" />
                               </a>
</td>
           </tr>

           <tr>
               <td class="xar-norm author">
                   <strong>

                            <a href="http://www.linux.com/archive/roles/402949">
                                asmerm                           </a>
</strong>

                   <span class="xar-sub">
<br />
Joined May 12, 2008<br/>Posts: 5<br />
                        <br />
                        <a href="http://www.linux.com/archive/forums?by=402949&amp;func=searchtopics">
Other Topics                        </a>
                    </span>
                </td>

                <td class="xar-norm message">
                   <div class="xarbb-messagecontrols">
                       <div class="leftnote">
                           <span class="xar-sub">
Posted:
                                May 13, 2008 8:22:23 AM                           </span>
                       </div>
                       <div class="rightnote">
</div>
                       <div style="float:left;">
                           <strong class="xar-sub">
Subject:
                                <a name="1189776" href="http://www.linux.com/archive/forums/topic/1724#1189776">
                                    Re:  parsing config file via bash script                                </a>
                            </strong>
                        </div>
                        <div style="clear: both;"></div>
                    </div>

                    <div style="clear: left;">
                        Thanks a lot! It works like a charm.
Also thanks for explanations!                    </div>
                </td>
            </tr>

            <tr>
                <td class="xar-norm author">
                    <a href="http://www.linux.com/archive/forums/topic/1724#top">
Back to top</a>
</td>

               <td class="xar-alt">

                            <a href="http://www.linux.com/archive/roles/402949">
                                <img src="modules/xarbb/xarimages/new/icon_profile.gif" alt="Profile" />
                            </a>
                            <a href="http://www.linux.com/archive/?module=roles&amp;func=email&amp;uid=402949">
                                <img src="modules/xarbb/xarimages/new/icon_email.gif" alt="Email" />
                           </a>
                                <a href="http://">
                                    <img src="modules/xarbb/xarimages/new/icon_www.gif" alt="Website" />
                               </a>
</td>
           </tr>

           <tr>
               <td class="xar-norm author">
                   <strong>

                            <a href="http://www.linux.com/archive/roles/401955">
                                Khabi                           </a>
</strong>

                   <span class="xar-sub">
<br />
Joined Apr 21, 2008<br/>Posts: 121<br />
                        <br />
                        <a href="http://www.linux.com/archive/forums?by=401955&amp;func=searchtopics">
Other Topics                        </a>
                    </span>
                </td>

                <td class="xar-accent message">
                   <div class="xarbb-messagecontrols">
                       <div class="leftnote">
                           <span class="xar-sub">
Posted:
                                May 13, 2008 10:52:04 PM                           </span>
                       </div>
                       <div class="rightnote">
</div>
                       <div style="float:left;">
                           <strong class="xar-sub">
Subject:
                                <a name="1189813" href="http://www.linux.com/archive/forums/topic/1724#1189813">
                                    parsing config file via bash script                                </a>
                            </strong>
                        </div>
                        <div style="clear: both;"></div>
                    </div>

                    <div style="clear: left;">
                        No problem!  Glad I could help out  :)                    </div>
                </td>
            </tr>

            <tr>
                <td class="xar-norm author">
                    <a href="http://www.linux.com/archive/forums/topic/1724#top">
Back to top</a>
</td>

               <td class="xar-alt">

                            <a href="http://www.linux.com/archive/roles/401955">
                                <img src="modules/xarbb/xarimages/new/icon_profile.gif" alt="Profile" />
                            </a>
                            <a href="http://www.linux.com/archive/?module=roles&amp;func=email&amp;uid=401955">
                                <img src="modules/xarbb/xarimages/new/icon_email.gif" alt="Email" />
                           </a>
                                <a href="http://">
                                    <img src="modules/xarbb/xarimages/new/icon_www.gif" alt="Website" />
                               </a>
</td>
           </tr>

           <tr>
               <td class="xar-norm author">
                   <strong>

                            <a href="http://www.linux.com/archive/roles/402669">
                                AR                           </a>
</strong>

                   <span class="xar-sub">
<br />
<br />
Joined May 06, 2008<br/>Posts: 22<br />
                        <br />
                        <a href="http://www.linux.com/archive/forums?by=402669&amp;func=searchtopics">
Other Topics                        </a>
                    </span>
                </td>

                <td class="xar-norm message">
                   <div class="xarbb-messagecontrols">
                       <div class="leftnote">
                           <span class="xar-sub">
Posted:
                                May 15, 2008 6:25:00 PM                           </span>
                       </div>
                       <div class="rightnote">
</div>
                       <div style="float:left;">
                           <strong class="xar-sub">
Subject:
                                <a name="1189908" href="http://www.linux.com/archive/forums/topic/1724#1189908">
                                    parsing config file via bash script                                </a>
                            </strong>
                        </div>
                        <div style="clear: both;"></div>
                    </div>

                    <div style="clear: left;">
                        I think cut is easier on your resources like &quot;cut -f3 -d\| filename&quot;
-f3 being the third field and -d\| being the separator (pipe has to be escaped hence the \)

example:
$ echo '1232|4565|23432' | cut -f3 -d\|  
23432

                    </div>
                </td>
            </tr>

            <tr>
                <td class="xar-norm author">
                    <a href="http://www.linux.com/archive/forums/topic/1724#top">
Back to top</a>
</td>

               <td class="xar-alt">

                            <a href="http://www.linux.com/archive/roles/402669">
                                <img src="modules/xarbb/xarimages/new/icon_profile.gif" alt="Profile" />
                            </a>
                            <a href="http://www.linux.com/archive/?module=roles&amp;func=email&amp;uid=402669">
                                <img src="modules/xarbb/xarimages/new/icon_email.gif" alt="Email" />
                           </a>
                                <a href="http://www.google.com/">
                                    <img src="modules/xarbb/xarimages/new/icon_www.gif" alt="Website" />
                               </a>
</td>
           </tr>

           <tr>
               <td class="xar-norm author">
                   <strong>

                            <a href="http://www.linux.com/archive/roles/402478">
                                xi                           </a>
</strong>

                   <span class="xar-sub">
<br />
Joined May 02, 2008<br/>Posts: 5<br />
                        <br />
                        <a href="http://www.linux.com/archive/forums?by=402478&amp;func=searchtopics">
Other Topics                        </a>
                    </span>
                </td>

                <td class="xar-accent message">
                   <div class="xarbb-messagecontrols">
                       <div class="leftnote">
                           <span class="xar-sub">
Posted:
                                May 17, 2008 2:06:27 PM                           </span>
                       </div>
                       <div class="rightnote">
</div>
                       <div style="float:left;">
                           <strong class="xar-sub">
Subject:
                                <a name="1190086" href="http://www.linux.com/archive/forums/topic/1724#1190086">
                                    Re:  parsing config file via bash script                                </a>
                            </strong>
                        </div>
                        <div style="clear: both;"></div>
                    </div>

                    <div style="clear: left;">
                        Assuming the separator symbol is not part of any of the words, this also works:

[code=xml]cat file.lst | while read line ; do
  echo &quot;${line##*|}&quot;
done[/code]                    </div>
                </td>
            </tr>

            <tr>
                <td class="xar-norm author">
                    <a href="http://www.linux.com/archive/forums/topic/1724#top">
Back to top</a>
</td>

               <td class="xar-alt">

                            <a href="http://www.linux.com/archive/roles/402478">
                                <img src="modules/xarbb/xarimages/new/icon_profile.gif" alt="Profile" />
                            </a>
                            <a href="http://www.linux.com/archive/?module=roles&amp;func=email&amp;uid=402478">
                                <img src="modules/xarbb/xarimages/new/icon_email.gif" alt="Email" />
                           </a>
                                <a href="http://">
                                    <img src="modules/xarbb/xarimages/new/icon_www.gif" alt="Website" />
                               </a>
</td>
           </tr>

           <tr>
               <td class="xar-norm author">
                   <strong>

                            <a href="http://www.linux.com/archive/roles/384369">
                                Slarti                           </a>
</strong>

                   <span class="xar-sub">
<br />
Joined Jun 28, 2007<br/>Posts: 1<br />
                        <br />
                        <a href="http://www.linux.com/archive/forums?by=384369&amp;func=searchtopics">
Other Topics                        </a>
                    </span>
                </td>

                <td class="xar-norm message">
                   <div class="xarbb-messagecontrols">
                       <div class="leftnote">
                           <span class="xar-sub">
Posted:
                                May 19, 2008 10:22:10 AM                           </span>
                       </div>
                       <div class="rightnote">
</div>
                       <div style="float:left;">
                           <strong class="xar-sub">
Subject:
                                <a name="1190219" href="http://www.linux.com/archive/forums/topic/1724#1190219">
                                    Re:  parsing config file via bash script                                </a>
                            </strong>
                        </div>
                        <div style="clear: both;"></div>
                    </div>

                    <div style="clear: left;">
                        The cut statement above only works if there are exactly three fields per line.  I don't know awk to comment on that.

Just to complete the trio, thought I'd give the sed version which prints the last field on each line, rather than the third field:

[code=xml]sed -e &quot;s/.*|\([^|]*\)$/\1/&quot; test.txt[/code]

To avoid lines which don't have any fields on them, such as comments, use:

[code=xml]sed -n -e &quot;s/.*|\([^|]*\)$/\1/p&quot; test.txt[/code]

To print the third field, even if there are more than three fields use:

[code=xml]sed -n -e &quot;s/[^|]*|[^|]*|\([^|]*\)|*.*$/\1/p&quot; test.txt[/code]                    </div>
                </td>
            </tr>

            <tr>
                <td class="xar-norm author">
                    <a href="http://www.linux.com/archive/forums/topic/1724#top">
Back to top</a>
</td>

               <td class="xar-alt">

                            <a href="http://www.linux.com/archive/roles/384369">
                                <img src="modules/xarbb/xarimages/new/icon_profile.gif" alt="Profile" />
                            </a>
                            <a href="http://www.linux.com/archive/?module=roles&amp;func=email&amp;uid=384369">
                                <img src="modules/xarbb/xarimages/new/icon_email.gif" alt="Email" />
                           </a>
</td>
           </tr>

           <tr>
               <td class="xar-norm author">
                   <strong>

                            <a href="http://www.linux.com/archive/roles/402669">
                                AR                           </a>
</strong>

                   <span class="xar-sub">
<br />
<br />
Joined May 06, 2008<br/>Posts: 22<br />
                        <br />
                        <a href="http://www.linux.com/archive/forums?by=402669&amp;func=searchtopics">
Other Topics                        </a>
                    </span>
                </td>

                <td class="xar-accent message">
                   <div class="xarbb-messagecontrols">
                       <div class="leftnote">
                           <span class="xar-sub">
Posted:
                                May 19, 2008 1:37:16 PM                           </span>
                       </div>
                       <div class="rightnote">
</div>
                       <div style="float:left;">
                           <strong class="xar-sub">
Subject:
                                <a name="1190226" href="http://www.linux.com/archive/forums/topic/1724#1190226">
                                    Re:  Re:  parsing config file via bash script                                </a>
                            </strong>
                        </div>
                        <div style="clear: both;"></div>
                    </div>

                    <div style="clear: left;">
                        [quote=Slarti]The cut statement above only works if there are exactly three fields per line.  I don't know awk to comment on that.[/quote]

I think you're wrong. It will work fine as long as it's at least three fields. Otherwise it returns an empty string/line (which is still fine).

example:

$ echo '1232|23423|23432|2398746832|3242342|234324' | cut -f3 -d\|
23432
$ echo -e '1232|234324' | cut -f3 -d\|                                        

$

I like the bash pattern substitution one :)
                    </div>
                </td>
            </tr>

            <tr>
                <td class="xar-norm author">
                    <a href="http://www.linux.com/archive/forums/topic/1724#top">
Back to top</a>
</td>

               <td class="xar-alt">

                            <a href="http://www.linux.com/archive/roles/402669">
                                <img src="modules/xarbb/xarimages/new/icon_profile.gif" alt="Profile" />
                            </a>
                            <a href="http://www.linux.com/archive/?module=roles&amp;func=email&amp;uid=402669">
                                <img src="modules/xarbb/xarimages/new/icon_email.gif" alt="Email" />
                           </a>
                                <a href="http://www.google.com/">
                                    <img src="modules/xarbb/xarimages/new/icon_www.gif" alt="Website" />
                               </a>
</td>
           </tr>

           <tr>
               <td class="xar-norm author">
                   <strong>

                            <a href="http://www.linux.com/archive/roles/401955">
                                Khabi                           </a>
</strong>

                   <span class="xar-sub">
<br />
Joined Apr 21, 2008<br/>Posts: 121<br />
                        <br />
                        <a href="http://www.linux.com/archive/forums?by=401955&amp;func=searchtopics">
Other Topics                        </a>
                    </span>
                </td>

                <td class="xar-norm message">
                   <div class="xarbb-messagecontrols">
                       <div class="leftnote">
                           <span class="xar-sub">
Posted:
                                May 19, 2008 7:20:41 PM                           </span>
                       </div>
                       <div class="rightnote">
</div>
                       <div style="float:left;">
                           <strong class="xar-sub">
Subject:
                                <a name="1190261" href="http://www.linux.com/archive/forums/topic/1724#1190261">
                                    parsing config file via bash script                                </a>
                            </strong>
                        </div>
                        <div style="clear: both;"></div>
                    </div>

                    <div style="clear: left;">
                        I still stand by my awk example.   But thats probably because I try to use as few pipes as possible when i write anything in bash.  It just makes it hard to maintain after time, especially because when you get into the habit of using pipes sooner or later your going to have a command with 5-10 pipes in it (I've seen it in old scripts in the place I work, makes me cringe :P ).

If you just want the 3rd field in the awk example just change $NR to $3 and thats all you'll get.  If the the line doesn't have a third field it just outputs a newline.  :)                    </div>
                </td>
            </tr>

            <tr>
                <td class="xar-norm author">
                    <a href="http://www.linux.com/archive/forums/topic/1724#top">
Back to top</a>
</td>

               <td class="xar-alt">

                            <a href="http://www.linux.com/archive/roles/401955">
                                <img src="modules/xarbb/xarimages/new/icon_profile.gif" alt="Profile" />
                            </a>
                            <a href="http://www.linux.com/archive/?module=roles&amp;func=email&amp;uid=401955">
                                <img src="modules/xarbb/xarimages/new/icon_email.gif" alt="Email" />
                           </a>
                                <a href="http://">
                                    <img src="modules/xarbb/xarimages/new/icon_www.gif" alt="Website" />
                               </a>
</td>
           </tr>

           <tr>
               <td class="xar-norm author">
                   <strong>

                            <a href="http://www.linux.com/archive/roles/402669">
                                AR                           </a>
</strong>

                   <span class="xar-sub">
<br />
<br />
Joined May 06, 2008<br/>Posts: 22<br />
                        <br />
                        <a href="http://www.linux.com/archive/forums?by=402669&amp;func=searchtopics">
Other Topics                        </a>
                    </span>
                </td>

                <td class="xar-accent message">
                   <div class="xarbb-messagecontrols">
                       <div class="leftnote">
                           <span class="xar-sub">
Posted:
                                May 24, 2008 12:47:52 PM                           </span>
                       </div>
                       <div class="rightnote">
</div>
                       <div style="float:left;">
                           <strong class="xar-sub">
Subject:
                                <a name="1190631" href="http://www.linux.com/archive/forums/topic/1724#1190631">
                                    parsing config file via bash script                                </a>
                            </strong>
                        </div>
                        <div style="clear: both;"></div>
                    </div>

                    <div style="clear: left;">
                        I must confess I love pipes but it's probably just me :) 
For some reason I have a feeling that using dedicated binaries (eg. cut) is faster than awk especially if two sides of the pipe can run on a different CPUs. I often have to parse these neverending log files and it matters there. I might be completely wrong though, I mean, they can't run in parallel since one is connected to the other.
You definitely have a point with maintenance problems. These complicated pipes can get almost as bad as they were written in perl :)                    </div>
                </td>
            </tr>

            <tr>
                <td class="xar-norm author">
                    <a href="http://www.linux.com/archive/forums/topic/1724#top">
Back to top</a>
</td>

               <td class="xar-alt">

                            <a href="http://www.linux.com/archive/roles/402669">
                                <img src="modules/xarbb/xarimages/new/icon_profile.gif" alt="Profile" />
                            </a>
                            <a href="http://www.linux.com/archive/?module=roles&amp;func=email&amp;uid=402669">
                                <img src="modules/xarbb/xarimages/new/icon_email.gif" alt="Email" />
                           </a>
                                <a href="http://www.google.com/">
                                    <img src="modules/xarbb/xarimages/new/icon_www.gif" alt="Website" />
                               </a>
</td>
           </tr>

           <tr>
               <td class="xar-norm author">
                   <strong>

                            <a href="http://www.linux.com/archive/roles/403836">
                                Kyle Brandt                           </a>
</strong>

                   <span class="xar-sub">
<br />
Joined May 27, 2008<br/>Posts: 1<br />
Location:Boston, Ma<br />
                        <br />
                        <a href="http://www.linux.com/archive/forums?by=403836&amp;func=searchtopics">
Other Topics                        </a>
                    </span>
                </td>

                <td class="xar-norm message">
                   <div class="xarbb-messagecontrols">
                       <div class="leftnote">
                           <span class="xar-sub">
Posted:
                                May 27, 2008 5:51:28 PM                           </span>
                       </div>
                       <div class="rightnote">
</div>
                       <div style="float:left;">
                           <strong class="xar-sub">
Subject:
                                <a name="1190840" href="http://www.linux.com/archive/forums/topic/1724#1190840">
                                    Re:  Re:  parsing config file via bash script                                </a>
                            </strong>
                        </div>
                        <div style="clear: both;"></div>
                    </div>

                    <div style="clear: left;">
                        [quote=xi]Assuming the separator symbol is not part of any of the words, this also works:

[code=xml]cat file.lst | while read line ; do
  echo &quot;${line##*|}&quot;
done[/code][/quote]

I like this solution, fyi it could also be written:


[code=xml]while read line; do
  echo &quot;${line##*|}&quot;
done &lt; file.lst[/code]<p class="hiddensignature">
http://www.kbrandt.com</p>
                    </div>
                </td>
            </tr>

            <tr>
                <td class="xar-norm author">
                    <a href="http://www.linux.com/archive/forums/topic/1724#top">
Back to top</a>
</td>

               <td class="xar-alt">

                            <a href="http://www.linux.com/archive/roles/403836">
                                <img src="modules/xarbb/xarimages/new/icon_profile.gif" alt="Profile" />
                            </a>
                            <a href="http://www.linux.com/archive/?module=roles&amp;func=email&amp;uid=403836">
                                <img src="modules/xarbb/xarimages/new/icon_email.gif" alt="Email" />
                           </a>
                                <a href="http://www.kbrandt.com">
                                    <img src="modules/xarbb/xarimages/new/icon_www.gif" alt="Website" />
                               </a>
</td>
           </tr>

           <tr>
               <td class="xar-norm author">
                   <strong>

                            <a href="http://www.linux.com/archive/roles/401955">
                                Khabi                           </a>
</strong>

                   <span class="xar-sub">
<br />
Joined Apr 21, 2008<br/>Posts: 121<br />
                        <br />
                        <a href="http://www.linux.com/archive/forums?by=401955&amp;func=searchtopics">
Other Topics                        </a>
                    </span>
                </td>

                <td class="xar-accent message">
                   <div class="xarbb-messagecontrols">
                       <div class="leftnote">
                           <span class="xar-sub">
Posted:
                                May 29, 2008 10:46:56 PM                           </span>
                       </div>
                       <div class="rightnote">
</div>
                       <div style="float:left;">
                           <strong class="xar-sub">
Subject:
                                <a name="1191016" href="http://www.linux.com/archive/forums/topic/1724#1191016">
                                    Re:  parsing config file via bash script                                </a>
                            </strong>
                        </div>
                        <div style="clear: both;"></div>
                    </div>

                    <div style="clear: left;">
                        [quote=Anthony Robbins]I must confess I love pipes but it's probably just me :) 
For some reason I have a feeling that using dedicated binaries (eg. cut) is faster than awk especially if two sides of the pipe can run on a different CPUs. I often have to parse these neverending log files and it matters there. I might be completely wrong though, I mean, they can't run in parallel since one is connected to the other.
You definitely have a point with maintenance problems. These complicated pipes can get almost as bad as they were written in perl :)[/quote]

You know, that would be an interesting test.  If you get a chance next you parse whatever it is your are to try running the time test against it and possibly try it again with awk I would be interested to see what the results are.  :)                    </div>
                </td>
            </tr>

            <tr>
                <td class="xar-norm author">
                    <a href="http://www.linux.com/archive/forums/topic/1724#top">
Back to top</a>
</td>

               <td class="xar-alt">

                            <a href="http://www.linux.com/archive/roles/401955">
                                <img src="modules/xarbb/xarimages/new/icon_profile.gif" alt="Profile" />
                            </a>
                            <a href="http://www.linux.com/archive/?module=roles&amp;func=email&amp;uid=401955">
                                <img src="modules/xarbb/xarimages/new/icon_email.gif" alt="Email" />
                           </a>
                                <a href="http://">
                                    <img src="modules/xarbb/xarimages/new/icon_www.gif" alt="Website" />
                               </a>
</td>
           </tr>

           <tr>
               <td class="xar-norm author">
                   <strong>

                            <a href="http://www.linux.com/archive/roles/402949">
                                asmerm                           </a>
</strong>

                   <span class="xar-sub">
<br />
Joined May 12, 2008<br/>Posts: 5<br />
                        <br />
                        <a href="http://www.linux.com/archive/forums?by=402949&amp;func=searchtopics">
Other Topics                        </a>
                    </span>
                </td>

                <td class="xar-norm message">
                   <div class="xarbb-messagecontrols">
                       <div class="leftnote">
                           <span class="xar-sub">
Posted:
                                Jun 03, 2008 10:43:29 AM                           </span>
                       </div>
                       <div class="rightnote">
</div>
                       <div style="float:left;">
                           <strong class="xar-sub">
Subject:
                                <a name="1191280" href="http://www.linux.com/archive/forums/topic/1724#1191280">
                                    parsing config file via bash script                                </a>
                            </strong>
                        </div>
                        <div style="clear: both;"></div>
                    </div>

                    <div style="clear: left;">
                        wow! This became discussion! cut is also very easy to use for beginners like me!                    </div>
                </td>
            </tr>

            <tr>
                <td class="xar-norm author">
                    <a href="http://www.linux.com/archive/forums/topic/1724#top">
Back to top</a>
</td>

               <td class="xar-alt">

                            <a href="http://www.linux.com/archive/roles/402949">
                                <img src="modules/xarbb/xarimages/new/icon_profile.gif" alt="Profile" />
                            </a>
                            <a href="http://www.linux.com/archive/?module=roles&amp;func=email&amp;uid=402949">
                                <img src="modules/xarbb/xarimages/new/icon_email.gif" alt="Email" />
                           </a>
                                <a href="http://">
                                    <img src="modules/xarbb/xarimages/new/icon_www.gif" alt="Website" />
                               </a>
</td>
           </tr>

           <tr>
               <td class="xar-norm author">
                   <strong>

                            <a href="http://www.linux.com/archive/roles/402669">
                                AR                           </a>
</strong>

                   <span class="xar-sub">
<br />
<br />
Joined May 06, 2008<br/>Posts: 22<br />
                        <br />
                        <a href="http://www.linux.com/archive/forums?by=402669&amp;func=searchtopics">
Other Topics                        </a>
                    </span>
                </td>

                <td class="xar-accent message">
                   <div class="xarbb-messagecontrols">
                       <div class="leftnote">
                           <span class="xar-sub">
Posted:
                                Jun 25, 2008 8:56:46 PM                           </span>
                       </div>
                       <div class="rightnote">
</div>
                       <div style="float:left;">
                           <strong class="xar-sub">
Subject:
                                <a name="1193104" href="http://www.linux.com/archive/forums/topic/1724#1193104">
                                    Re:  Re:  Re:  parsing config file via bash script                                </a>
                            </strong>
                        </div>
                        <div style="clear: both;"></div>
                    </div>

                    <div style="clear: left;">
                        I've run some tests a couple of times on a 14MB text file consisting of pwgen's output like

Yee2Aith Kaibo9ku oY2Ipe3y iPah9ieg zoo3oNg1 Pa3phae6 Caatho8a Ush5ohTe
thahth6S yeep3Phi ounee5Ei eopeik4S goi1Aiye quo8iZes Aewie0Xu AeZae0ia
phee5Ahw Too1uaR0 taem9ieP ShiY2eek sie8eeZ0 auNg7hie iich0Oom cohQu9Uu
...

# cut -f3,5 -d\  big2.txt &gt; /dev/null 
average: 0.13s 

# awk '{print $3&quot; &quot;$5}' big2.txt&gt;/dev/null
average: 0.27s (128% of cut|tr)

awk actually does more (it skips multiple field separators while cut does not)
so to achieve the exact same effect the correct command line would be
# tr -s \ &lt; big2.txt |  cut -f3,5 -d\  &gt; /dev/null  
average: 0.21s (100%)

since this is a semi-SMP system (3ghz p4 HT) I've run the last 
two tests (awk vs. tr | cut) on a mono processor computer (2.4ghz p4) on the same file:

# awk '{print $3&quot; &quot;$5}' big2.txt &gt; /dev/null
average: 0.38s (135%)

# tr -s \ &lt; big2.txt | cut -f3,5 -d\   &gt; /dev/null
average: 0.28s (100%)

The bottom line is that cut is way faster, cut | tr is still faster than awk 
and it's interesting to see that HT-SMP did not help at all (tr | cut was relatively 
slower on HT). All the computers were running the same kernel/os (2.6,debian etch)

edit:
I've managed to get my hands on a 8 CPU system and increased the file size to 10x

# awk '{print $3&quot; &quot;$5}' big3.txt&gt;/dev/null
average: 2.35s (189%)

# tr -s \ &lt; big3.txt |  cut -f3,5 -d\  &gt; /dev/null
average: 1.24s (100%)

looks like real SMP helps and filters with pipes in them scale a lot better :)


[Modified by: Anthony Robbins on June 25, 2008 09:16 PM]
                    </div>
                </td>
            </tr>

            <tr>
                <td class="xar-norm author">
                    <a href="http://www.linux.com/archive/forums/topic/1724#top">
Back to top</a>
</td>

               <td class="xar-alt">

                            <a href="http://www.linux.com/archive/roles/402669">
                                <img src="modules/xarbb/xarimages/new/icon_profile.gif" alt="Profile" />
                            </a>
                            <a href="http://www.linux.com/archive/?module=roles&amp;func=email&amp;uid=402669">
                                <img src="modules/xarbb/xarimages/new/icon_email.gif" alt="Email" />
                           </a>
                                <a href="http://www.google.com/">
                                    <img src="modules/xarbb/xarimages/new/icon_www.gif" alt="Website" />
                               </a>
</td>
           </tr>

           <tr>
               <td class="xar-norm author">
                   <strong>

                            <a href="http://www.linux.com/archive/roles/401955">
                                Khabi                           </a>
</strong>

                   <span class="xar-sub">
<br />
Joined Apr 21, 2008<br/>Posts: 121<br />
                        <br />
                        <a href="http://www.linux.com/archive/forums?by=401955&amp;func=searchtopics">
Other Topics                        </a>
                    </span>
                </td>

                <td class="xar-norm message">
                   <div class="xarbb-messagecontrols">
                       <div class="leftnote">
                           <span class="xar-sub">
Posted:
                                Jun 26, 2008 7:36:31 PM                           </span>
                       </div>
                       <div class="rightnote">
</div>
                       <div style="float:left;">
                           <strong class="xar-sub">
Subject:
                                <a name="1193189" href="http://www.linux.com/archive/forums/topic/1724#1193189">
                                    Re:  parsing config file via bash script                                </a>
                            </strong>
                        </div>
                        <div style="clear: both;"></div>
                    </div>

                    <div style="clear: left;">
                         Anthony,

I'm actually surprised that the difference was that much, I would have figured it would have been smaller if any difference at all.  I'm guessing its the overhead from awk's ability to do formatted output or something.                      </div>
                </td>
            </tr>

            <tr>
                <td class="xar-norm author">
                    <a href="http://www.linux.com/archive/forums/topic/1724#top">
Back to top</a>
</td>

               <td class="xar-alt">

                            <a href="http://www.linux.com/archive/roles/401955">
                                <img src="modules/xarbb/xarimages/new/icon_profile.gif" alt="Profile" />
                            </a>
                            <a href="http://www.linux.com/archive/?module=roles&amp;func=email&amp;uid=401955">
                                <img src="modules/xarbb/xarimages/new/icon_email.gif" alt="Email" />
                           </a>
                                <a href="http://">
                                    <img src="modules/xarbb/xarimages/new/icon_www.gif" alt="Website" />
                               </a>
</td>
           </tr>
</table>
</div>

<div class="xarbb-topiccontrols">

<span class="xar-sub">
    <a href="http://www.linux.com/archive/forums">
Forum Index    </a>
    <span style="font-size: larger;">&#187;</span>
    <a href="http://www.linux.com/archive/forums/category/4181">
        Forums 
    </a>
    <span style="font-size: larger;">&#187;</span>
    <a href="http://www.linux.com/archive/forums/forum/5">
        Programming and Development   </a>
</span>

</div>



            </channel>
</rss>
