<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic Re: String handling in shell scripts in Operating System - HP-UX</title>
    <link>https://community.hpe.com/t5/operating-system-hp-ux/string-handling-in-shell-scripts/m-p/4269698#M688261</link>
    <description>My mixed solution of shell and awk was working fine except for one case, when the first field had leading spaces and the second field didn't. Then, the number of args would be one and I'd have to use the cut to parse the fields. The problem with the shell dropping the leading white space was that the "cut 1-6" would then grab data from the second field. I wasn't worried about the leading space in awk. It was about getting the fields parsed correctly.</description>
    <pubDate>Tue, 16 Sep 2008 14:19:25 GMT</pubDate>
    <dc:creator>J Ruud</dc:creator>
    <dc:date>2008-09-16T14:19:25Z</dc:date>
    <item>
      <title>String handling in shell scripts</title>
      <link>https://community.hpe.com/t5/operating-system-hp-ux/string-handling-in-shell-scripts/m-p/4269687#M688250</link>
      <description>I'm wondering if someone would be able to suggest an approach to resolving a problem that I'm having with string handling in a shell script.&lt;BR /&gt;&lt;BR /&gt;I'm using ksh and I have a script that I'm using to read a file that consists of two fields per record. The fields may or may not be separated by spaces. The record length is fixed (12), as are the fields (6). I want to parse the fields and feed them to an awk script, which will use them to find a matching record in another file. I'm using a read loop to process the first file and using the set&lt;BR /&gt;command to determine how to parse the data. If the number of args is 2, then the fields are separated by one or more spaces and I can just assign $1 and $2. If I only have 1 arg then I use a cut command to parse the data. The problem I'm dealing with is that the shell seems to drop leading blanks. So if the first field has one or more leading blanks and the second field doesn't, the cut ends up grabbing data from the second field. How do I get the shell to leave the record data as it is in the file?&lt;BR /&gt;&lt;BR /&gt;Here's the code segment...&lt;BR /&gt;&lt;BR /&gt;while read line&lt;BR /&gt;    do&lt;BR /&gt; set $line&lt;BR /&gt; if [ $# -eq 2 ]&lt;BR /&gt; then&lt;BR /&gt;     a=$1&lt;BR /&gt;     b=$2&lt;BR /&gt; else&lt;BR /&gt;     a=`echo $line | cut -c 1-6`&lt;BR /&gt;     b=`echo $line | cut -c 7-12`&lt;BR /&gt; fi&lt;BR /&gt; buffer=`awk '{ if( $2 == '$a' &amp;amp;&amp;amp; $3 == '$b' )&lt;BR /&gt; print $2, $3, $4, $6, $7, $8, $9, $10, $11}' ${file2}`&lt;BR /&gt; echo $buffer &amp;gt;&amp;gt; ${ifn}&lt;BR /&gt;    done &amp;lt; $file1&lt;BR /&gt;</description>
      <pubDate>Mon, 15 Sep 2008 17:54:48 GMT</pubDate>
      <guid>https://community.hpe.com/t5/operating-system-hp-ux/string-handling-in-shell-scripts/m-p/4269687#M688250</guid>
      <dc:creator>J Ruud</dc:creator>
      <dc:date>2008-09-15T17:54:48Z</dc:date>
    </item>
    <item>
      <title>Re: String handling in shell scripts</title>
      <link>https://community.hpe.com/t5/operating-system-hp-ux/string-handling-in-shell-scripts/m-p/4269688#M688251</link>
      <description>&lt;!--!*#--&gt;More quotation?&lt;BR /&gt;&lt;BR /&gt;&amp;gt; line='  ab  '&lt;BR /&gt;&lt;BR /&gt;&amp;gt; e1=` echo $line `&lt;BR /&gt;&amp;gt; e2=` echo "$line" `&lt;BR /&gt;&lt;BR /&gt;&amp;gt; echo '&amp;gt;&amp;gt;'"$e1"'&amp;lt;&amp;lt;'&lt;BR /&gt;&amp;gt;&amp;gt;ab&amp;lt;&amp;lt;&lt;BR /&gt;&lt;BR /&gt;&amp;gt; echo '&amp;gt;&amp;gt;'"$e2"'&amp;lt;&amp;lt;'&lt;BR /&gt;&amp;gt;&amp;gt;  ab  &amp;lt;&amp;lt;&lt;BR /&gt;&lt;BR /&gt;Your "echo $line" is tossing white space.</description>
      <pubDate>Mon, 15 Sep 2008 18:17:46 GMT</pubDate>
      <guid>https://community.hpe.com/t5/operating-system-hp-ux/string-handling-in-shell-scripts/m-p/4269688#M688251</guid>
      <dc:creator>Steven Schweda</dc:creator>
      <dc:date>2008-09-15T18:17:46Z</dc:date>
    </item>
    <item>
      <title>Re: String handling in shell scripts</title>
      <link>https://community.hpe.com/t5/operating-system-hp-ux/string-handling-in-shell-scripts/m-p/4269689#M688252</link>
      <description>Hi,&lt;BR /&gt;&lt;BR /&gt;&amp;gt; a=`echo $line | cut -c 1-6`&lt;BR /&gt;&amp;gt; b=`echo $line | cut -c 7-12`&lt;BR /&gt;&lt;BR /&gt;try &lt;BR /&gt;&lt;BR /&gt;a=${line:0:6}&lt;BR /&gt;b=${line:6:12}&lt;BR /&gt;&lt;BR /&gt;</description>
      <pubDate>Mon, 15 Sep 2008 18:58:40 GMT</pubDate>
      <guid>https://community.hpe.com/t5/operating-system-hp-ux/string-handling-in-shell-scripts/m-p/4269689#M688252</guid>
      <dc:creator>Kenan Erdey</dc:creator>
      <dc:date>2008-09-15T18:58:40Z</dc:date>
    </item>
    <item>
      <title>Re: String handling in shell scripts</title>
      <link>https://community.hpe.com/t5/operating-system-hp-ux/string-handling-in-shell-scripts/m-p/4269690#M688253</link>
      <description>It looks like it happening before I get to the lines with the cut command. I tried using the double quote approach to do a little debugging. I used echo 'x'"$line"'x'. And, I found that where line = ' ab', I got 'xabx'. It looks like it's happening in the read.</description>
      <pubDate>Mon, 15 Sep 2008 19:07:23 GMT</pubDate>
      <guid>https://community.hpe.com/t5/operating-system-hp-ux/string-handling-in-shell-scripts/m-p/4269690#M688253</guid>
      <dc:creator>J Ruud</dc:creator>
      <dc:date>2008-09-15T19:07:23Z</dc:date>
    </item>
    <item>
      <title>Re: String handling in shell scripts</title>
      <link>https://community.hpe.com/t5/operating-system-hp-ux/string-handling-in-shell-scripts/m-p/4269691#M688254</link>
      <description>So, I guess my question now is...&lt;BR /&gt;How do I read this file without having the shell drop leading white space?</description>
      <pubDate>Mon, 15 Sep 2008 19:13:37 GMT</pubDate>
      <guid>https://community.hpe.com/t5/operating-system-hp-ux/string-handling-in-shell-scripts/m-p/4269691#M688254</guid>
      <dc:creator>J Ruud</dc:creator>
      <dc:date>2008-09-15T19:13:37Z</dc:date>
    </item>
    <item>
      <title>Re: String handling in shell scripts</title>
      <link>https://community.hpe.com/t5/operating-system-hp-ux/string-handling-in-shell-scripts/m-p/4269692#M688255</link>
      <description>change internal delimiter to another character than whitespace&lt;BR /&gt;&lt;BR /&gt;put IFS=':' to the top of your script.</description>
      <pubDate>Mon, 15 Sep 2008 19:34:30 GMT</pubDate>
      <guid>https://community.hpe.com/t5/operating-system-hp-ux/string-handling-in-shell-scripts/m-p/4269692#M688255</guid>
      <dc:creator>Kenan Erdey</dc:creator>
      <dc:date>2008-09-15T19:34:30Z</dc:date>
    </item>
    <item>
      <title>Re: String handling in shell scripts</title>
      <link>https://community.hpe.com/t5/operating-system-hp-ux/string-handling-in-shell-scripts/m-p/4269693#M688256</link>
      <description>Good Answer!&lt;BR /&gt;That did the job.&lt;BR /&gt;Thanks!</description>
      <pubDate>Mon, 15 Sep 2008 19:52:09 GMT</pubDate>
      <guid>https://community.hpe.com/t5/operating-system-hp-ux/string-handling-in-shell-scripts/m-p/4269693#M688256</guid>
      <dc:creator>J Ruud</dc:creator>
      <dc:date>2008-09-15T19:52:09Z</dc:date>
    </item>
    <item>
      <title>Re: String handling in shell scripts</title>
      <link>https://community.hpe.com/t5/operating-system-hp-ux/string-handling-in-shell-scripts/m-p/4269694#M688257</link>
      <description>&lt;!--!*#--&gt;Hi,&lt;BR /&gt;&lt;BR /&gt;to overcome the problem of your space handling, I suggest to use pure awk.&lt;BR /&gt;More, I would check, if the input is really in correct format.&lt;BR /&gt;awk 'BEGIN {while (getline &amp;lt; "file1" == 1) {if (NF==2) {&lt;BR /&gt;if (!(length($1) == 6 &amp;amp;&amp;amp; length($2) == 6)) print "incorrect data at","file1",NR &amp;gt;"error"&lt;BR /&gt;else {a[++i]=$1; b[i]=$2}}&lt;BR /&gt;else {if(length($0) &amp;lt;12) print "incorrect:","file1",NR &amp;gt;"error"&lt;BR /&gt;else {a[++i]=substr($0,1,6); b[i]=substr($0,6,12)}}}&lt;BR /&gt;NF &amp;gt;= 11 {for(j=1;j&amp;lt;=i;j++) {&lt;BR /&gt;if($2 == a[j] &amp;amp;&amp;amp; $3 == b[j]) {print $2, $3, $4, $6, $7, $8, $9, $10, $11&lt;BR /&gt;next}}' file2 &amp;gt;outfile&lt;BR /&gt;&lt;BR /&gt;NB: Untested - no UNIX at hand here.&lt;BR /&gt;Look carefully for balanced  () and {}.&lt;BR /&gt;&lt;BR /&gt;mfG Peter</description>
      <pubDate>Mon, 15 Sep 2008 20:15:37 GMT</pubDate>
      <guid>https://community.hpe.com/t5/operating-system-hp-ux/string-handling-in-shell-scripts/m-p/4269694#M688257</guid>
      <dc:creator>Peter Nikitka</dc:creator>
      <dc:date>2008-09-15T20:15:37Z</dc:date>
    </item>
    <item>
      <title>Re: String handling in shell scripts</title>
      <link>https://community.hpe.com/t5/operating-system-hp-ux/string-handling-in-shell-scripts/m-p/4269695#M688258</link>
      <description>Peter,&lt;BR /&gt;If I couldn't come up with a simple script solution, I was thinking about how I might be able to use more awk to solve the problem. But, not being that well versed in using it, I was struggling to come up with a solution. I was thinking that I would be able to use $NF, length and substr to determine the format of the input and parse it. But I wasn't sure how I could process the one file, one line at a time, while using its contents to process the other. I was looking into that when Kenan came up with the one line solution to my problem. But, thanks for your input. I'm going to save your answer for future reference.</description>
      <pubDate>Mon, 15 Sep 2008 20:56:45 GMT</pubDate>
      <guid>https://community.hpe.com/t5/operating-system-hp-ux/string-handling-in-shell-scripts/m-p/4269695#M688258</guid>
      <dc:creator>J Ruud</dc:creator>
      <dc:date>2008-09-15T20:56:45Z</dc:date>
    </item>
    <item>
      <title>Re: String handling in shell scripts</title>
      <link>https://community.hpe.com/t5/operating-system-hp-ux/string-handling-in-shell-scripts/m-p/4269696#M688259</link>
      <description>&lt;!--!*#--&gt;You can use mix solution of shell read and awk to do what you're doing.  But use awk to split the fixed length fields:&lt;BR /&gt;while read line; do&lt;BR /&gt;   # Your solution, with fixes&lt;BR /&gt;   set $line&lt;BR /&gt;   if [ $# -eq 2 ]; then&lt;BR /&gt;      a=$1&lt;BR /&gt;      b=$2&lt;BR /&gt;   else&lt;BR /&gt;      a=$(echo "$line" | cut -c 1-6)&lt;BR /&gt;      b=$(echo "$line" | cut -c 7-12)&lt;BR /&gt;   fi&lt;BR /&gt;   buffer=$(awk -v a="$a" -v b="$b" '{ if( $2 == a &amp;amp;&amp;amp; $3 == b)&lt;BR /&gt;      print $2, $3, $4, $6, $7, $8, $9, $10, $11}' ${file2})&lt;BR /&gt;   echo $buffer &amp;gt;&amp;gt; ${ifn}&lt;BR /&gt;&lt;BR /&gt;   # awk solution&lt;BR /&gt;   awk -v line="$line" '&lt;BR /&gt;BEGIN {&lt;BR /&gt;a = substr(line, 1, 6)&lt;BR /&gt;gsub(" ", "", a)  # remove spaces&lt;BR /&gt;b = substr(line, 7, 6)&lt;BR /&gt;gsub(" ", "", b)&lt;BR /&gt;}&lt;BR /&gt;{&lt;BR /&gt;if ($2 == a &amp;amp;&amp;amp; $3 == b)&lt;BR /&gt;    print $2, $3, $4, $6, $7, $8, $9, $10, $11&lt;BR /&gt;}' ${file2} &amp;gt;&amp;gt; ${ifn}&lt;BR /&gt;done &amp;lt; $file1</description>
      <pubDate>Tue, 16 Sep 2008 00:17:57 GMT</pubDate>
      <guid>https://community.hpe.com/t5/operating-system-hp-ux/string-handling-in-shell-scripts/m-p/4269696#M688259</guid>
      <dc:creator>Dennis Handly</dc:creator>
      <dc:date>2008-09-16T00:17:57Z</dc:date>
    </item>
    <item>
      <title>Re: String handling in shell scripts</title>
      <link>https://community.hpe.com/t5/operating-system-hp-ux/string-handling-in-shell-scripts/m-p/4269697#M688260</link>
      <description>&lt;!--!*#--&gt;You can use mixed solution of shell read and awk to do what you're doing.  But use awk to split the fixed length fields:&lt;BR /&gt;while read line; do&lt;BR /&gt;   # Your solution, with fixes&lt;BR /&gt;   set $line&lt;BR /&gt;   if [ $# -eq 2 ]; then&lt;BR /&gt;      a=$1&lt;BR /&gt;      b=$2&lt;BR /&gt;   else&lt;BR /&gt;      a=$(echo "$line" | cut -c 1-6)&lt;BR /&gt;      b=$(echo "$line" | cut -c 7-12)&lt;BR /&gt;   fi&lt;BR /&gt;   buffer=$(awk -v a="$a" -v b="$b" '{ if( $2 == a &amp;amp;&amp;amp; $3 == b)&lt;BR /&gt;      print $2, $3, $4, $6, $7, $8, $9, $10, $11}' ${file2})&lt;BR /&gt;   echo $buffer &amp;gt;&amp;gt; ${ifn}&lt;BR /&gt;&lt;BR /&gt;   # awk solution&lt;BR /&gt;   awk -v line="$line" '&lt;BR /&gt;BEGIN {&lt;BR /&gt;a = substr(line, 1, 6)&lt;BR /&gt;gsub(" ", "", a)  # remove spaces&lt;BR /&gt;b = substr(line, 7, 6)&lt;BR /&gt;gsub(" ", "", b)&lt;BR /&gt;}&lt;BR /&gt;{&lt;BR /&gt;if ($2 == a &amp;amp;&amp;amp; $3 == b)&lt;BR /&gt;    print $2, $3, $4, $6, $7, $8, $9, $10, $11&lt;BR /&gt;}' ${file2} &amp;gt;&amp;gt; ${ifn}&lt;BR /&gt;done &amp;lt; $file1&lt;BR /&gt;&lt;BR /&gt;&amp;gt;The problem I'm dealing with is that the shell seems to drop leading blanks. &lt;BR /&gt;&lt;BR /&gt;What do you want to do with those leading blanks?  You won't find them in your second file.</description>
      <pubDate>Tue, 16 Sep 2008 00:39:57 GMT</pubDate>
      <guid>https://community.hpe.com/t5/operating-system-hp-ux/string-handling-in-shell-scripts/m-p/4269697#M688260</guid>
      <dc:creator>Dennis Handly</dc:creator>
      <dc:date>2008-09-16T00:39:57Z</dc:date>
    </item>
    <item>
      <title>Re: String handling in shell scripts</title>
      <link>https://community.hpe.com/t5/operating-system-hp-ux/string-handling-in-shell-scripts/m-p/4269698#M688261</link>
      <description>My mixed solution of shell and awk was working fine except for one case, when the first field had leading spaces and the second field didn't. Then, the number of args would be one and I'd have to use the cut to parse the fields. The problem with the shell dropping the leading white space was that the "cut 1-6" would then grab data from the second field. I wasn't worried about the leading space in awk. It was about getting the fields parsed correctly.</description>
      <pubDate>Tue, 16 Sep 2008 14:19:25 GMT</pubDate>
      <guid>https://community.hpe.com/t5/operating-system-hp-ux/string-handling-in-shell-scripts/m-p/4269698#M688261</guid>
      <dc:creator>J Ruud</dc:creator>
      <dc:date>2008-09-16T14:19:25Z</dc:date>
    </item>
    <item>
      <title>Re: String handling in shell scripts</title>
      <link>https://community.hpe.com/t5/operating-system-hp-ux/string-handling-in-shell-scripts/m-p/4269699#M688262</link>
      <description>Dennis,&lt;BR /&gt;Regarding your solution, it gives me an idea of another approach that I might take using awk. But, I don't think it would have worked as you have it coded. You see, with the space being dropped, the awk script would only get a string of 11 bytes. So the substr would have the same problem as my cut.</description>
      <pubDate>Tue, 16 Sep 2008 14:25:49 GMT</pubDate>
      <guid>https://community.hpe.com/t5/operating-system-hp-ux/string-handling-in-shell-scripts/m-p/4269699#M688262</guid>
      <dc:creator>J Ruud</dc:creator>
      <dc:date>2008-09-16T14:25:49Z</dc:date>
    </item>
    <item>
      <title>Re: String handling in shell scripts</title>
      <link>https://community.hpe.com/t5/operating-system-hp-ux/string-handling-in-shell-scripts/m-p/4269700#M688263</link>
      <description>&lt;!--!*#--&gt;Hi:&lt;BR /&gt;&lt;BR /&gt;Instead of reading a line into a single variable, I would do:&lt;BR /&gt;&lt;BR /&gt;...&lt;BR /&gt;while read A B X&lt;BR /&gt;...&lt;BR /&gt;&lt;BR /&gt;Now, either you have two fields (or more, in which case the third..n-th are in 'X') or you have field 'A' with an empty 'B'.&lt;BR /&gt;&lt;BR /&gt;If you have one field ('B' is empty), you could reject the record read if the size of 'A' isn't exactly 12-characters (your requirement).&lt;BR /&gt;&lt;BR /&gt;For instance, you could issue an error message and continue the 'read' loop by adding:&lt;BR /&gt;&lt;BR /&gt;[ "${#A}" -ne 12 ] &amp;amp;&amp;amp; { echo "bad_size"; continue; }&lt;BR /&gt;&lt;BR /&gt;If you are going to change the Inter-Field-Seperator to prevent field splitting and preserve both leading and trailing spaces, *at least* localize its action:&lt;BR /&gt;&lt;BR /&gt;#!/usr/bin/sh&lt;BR /&gt;OLDIFS=${IFS}&lt;BR /&gt;IFS=''&lt;BR /&gt;while read A&lt;BR /&gt;do&lt;BR /&gt;    echo "[${A}]"&lt;BR /&gt;    echo "...and my size was: " ${#A}&lt;BR /&gt;done&lt;BR /&gt;IFS=${OLDIFS}&lt;BR /&gt;&lt;BR /&gt;...&lt;BR /&gt;&lt;BR /&gt;Regards!&lt;BR /&gt;&lt;BR /&gt;...JRF...</description>
      <pubDate>Tue, 16 Sep 2008 15:27:25 GMT</pubDate>
      <guid>https://community.hpe.com/t5/operating-system-hp-ux/string-handling-in-shell-scripts/m-p/4269700#M688263</guid>
      <dc:creator>James R. Ferguson</dc:creator>
      <dc:date>2008-09-16T15:27:25Z</dc:date>
    </item>
    <item>
      <title>Re: String handling in shell scripts</title>
      <link>https://community.hpe.com/t5/operating-system-hp-ux/string-handling-in-shell-scripts/m-p/4269701#M688264</link>
      <description>&lt;BR /&gt;Thanks for your input. The file will always have two fields, unless I change it. In case you're wondering, the format is such because of the number of different programs and files with which it is used. I'm not that experienced with programming in shell. Mainly, I've used it to call other programs and I learn as much as I need to get the job done. This whole exercise started when I decided to provide added functionality by modifying this script to take in a file of arguments, instead of the original two. So, first, I had to figure out how to read the file and pass the args to the awk script. Your suggestion shows me another way that shell can be used to perform file processing tasks. Also, I like the idea of localizing the IFS influence. And, although I don't have to worry about it having a negative impact on the processing of this script, I'll definitely implement your idea. Thanks to everyone for your responses. It's been a positive learning experience.&lt;BR /&gt;</description>
      <pubDate>Tue, 16 Sep 2008 16:55:38 GMT</pubDate>
      <guid>https://community.hpe.com/t5/operating-system-hp-ux/string-handling-in-shell-scripts/m-p/4269701#M688264</guid>
      <dc:creator>J Ruud</dc:creator>
      <dc:date>2008-09-16T16:55:38Z</dc:date>
    </item>
    <item>
      <title>Re: String handling in shell scripts</title>
      <link>https://community.hpe.com/t5/operating-system-hp-ux/string-handling-in-shell-scripts/m-p/4269702#M688265</link>
      <description>&amp;gt;with the space being dropped, the awk script would only get a string of 11 bytes. So the substr would have the same problem as my cut.&lt;BR /&gt;&lt;BR /&gt;Yes, you need that IFS solution too.  But you need to later remove those spaces.</description>
      <pubDate>Tue, 16 Sep 2008 17:51:25 GMT</pubDate>
      <guid>https://community.hpe.com/t5/operating-system-hp-ux/string-handling-in-shell-scripts/m-p/4269702#M688265</guid>
      <dc:creator>Dennis Handly</dc:creator>
      <dc:date>2008-09-16T17:51:25Z</dc:date>
    </item>
  </channel>
</rss>

