<?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: Using sed or awk to validate a file content in Operating System - HP-UX</title>
    <link>https://community.hpe.com/t5/operating-system-hp-ux/using-sed-or-awk-to-validate-a-file-content/m-p/3135677#M153973</link>
    <description>Hi Melvin,&lt;BR /&gt;a more traditional approach:&lt;BR /&gt; &lt;BR /&gt;#!/usr/bin/sh&lt;BR /&gt;# Check CORRECT num of delim in $1&lt;BR /&gt;&lt;BR /&gt;typeset -i NUM_CHARS=0 AFTER_DEL=0 DIFF=0 CORRECT=25&lt;BR /&gt;typeset DELIM="\;"&lt;BR /&gt;OK_FILE=./okrecs&lt;BR /&gt;REJ_FILE=./rejrecs&lt;BR /&gt;&lt;BR /&gt;while read line&lt;BR /&gt;do&lt;BR /&gt;        NUM_CHARS=$(echo "$line"| wc -c)&lt;BR /&gt;        AFTER_DEL=$(echo "$line" | tr -d $DELIM |wc -c)&lt;BR /&gt;        DIFF=$(( $NUM_CHARS - $AFTER_DEL ))&lt;BR /&gt;        if [ "$DIFF" = "$CORRECT" ]&lt;BR /&gt;        then&lt;BR /&gt;                echo "$line"  &amp;gt;&amp;gt; $OK_FILE&lt;BR /&gt;        else&lt;BR /&gt;                echo "$line"  &amp;gt;&amp;gt; $REJ_FILE&lt;BR /&gt;&lt;BR /&gt;        fi&lt;BR /&gt;done &amp;lt;$1&lt;BR /&gt; &lt;BR /&gt;Set OK_FILE and REJ_FILE to something appropriate and run it with your infile as $1.&lt;BR /&gt; &lt;BR /&gt;regards,&lt;BR /&gt;John K.</description>
    <pubDate>Thu, 04 Dec 2003 03:08:43 GMT</pubDate>
    <dc:creator>john korterman</dc:creator>
    <dc:date>2003-12-04T03:08:43Z</dc:date>
    <item>
      <title>Using sed or awk to validate a file content</title>
      <link>https://community.hpe.com/t5/operating-system-hp-ux/using-sed-or-awk-to-validate-a-file-content/m-p/3135674#M153970</link>
      <description>Hi Unix Gurus,&lt;BR /&gt;&lt;BR /&gt;I am new to sed and awk command. Currently I have a text file that has lines of records. The record fields are delimited by ";". A line of record has 26 fields and separated by 25 semi-colons ";".&lt;BR /&gt;&lt;BR /&gt;eg:&lt;BR /&gt;A;B;C;D;E;F;G;H;I;J;K;L;M;N;O;P;Q;R;S;T;U;V;W;X;Y;Z&lt;BR /&gt;&lt;BR /&gt;Our objective is to have a shell script that can validate the text file whether the records are complete in term of number of columns. Hence my idea is to count the number of delimiters on each of the records in the text file.&lt;BR /&gt;&lt;BR /&gt;If the count of delimiters has 25, then the complete record will be written to another file else if the delimiter count has less than 25, then I have to write the record to a reject file.&lt;BR /&gt;&lt;BR /&gt;Can the above objective be accomplished using the sed or awk command? I am stuck at this point using sed or awk to meet the objective.&lt;BR /&gt;&lt;BR /&gt;Any suggestion is very much appreciated. Thank you in advance!  &lt;BR /&gt;&lt;BR /&gt;Regards,&lt;BR /&gt;Melvin</description>
      <pubDate>Wed, 03 Dec 2003 23:03:38 GMT</pubDate>
      <guid>https://community.hpe.com/t5/operating-system-hp-ux/using-sed-or-awk-to-validate-a-file-content/m-p/3135674#M153970</guid>
      <dc:creator>Melvin Thong</dc:creator>
      <dc:date>2003-12-03T23:03:38Z</dc:date>
    </item>
    <item>
      <title>Re: Using sed or awk to validate a file content</title>
      <link>https://community.hpe.com/t5/operating-system-hp-ux/using-sed-or-awk-to-validate-a-file-content/m-p/3135675#M153971</link>
      <description>As Awk reads a line it will split it into words and setup the word coutn in a variable NF.&lt;BR /&gt;Awk default word seperator is white-space.&lt;BR /&gt;But you can make it anything including a semicolon (but you'll need to escape it for the shell).&lt;BR /&gt;The two notions combined solve your problem:&lt;BR /&gt;&lt;BR /&gt;awk -F\; '(NF==26); (NF!=26) {print $0 &amp;gt;&amp;gt; "bad-file"}' &amp;lt; mixed-file &amp;gt; good-file&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;Myself, I'd spend a few minutes more and cerate a PERL scripts to open good, bad, loop over the input, print left or right and close.&lt;BR /&gt;&lt;BR /&gt;hth,&lt;BR /&gt;Hein.&lt;BR /&gt;</description>
      <pubDate>Wed, 03 Dec 2003 23:41:33 GMT</pubDate>
      <guid>https://community.hpe.com/t5/operating-system-hp-ux/using-sed-or-awk-to-validate-a-file-content/m-p/3135675#M153971</guid>
      <dc:creator>Hein van den Heuvel</dc:creator>
      <dc:date>2003-12-03T23:41:33Z</dc:date>
    </item>
    <item>
      <title>Re: Using sed or awk to validate a file content</title>
      <link>https://community.hpe.com/t5/operating-system-hp-ux/using-sed-or-awk-to-validate-a-file-content/m-p/3135676#M153972</link>
      <description>It cannot be done simple by using sed, though it is easy to do by combining sed with wc:&lt;BR /&gt;&lt;BR /&gt;cat files | while read line&lt;BR /&gt;do&lt;BR /&gt;   if [ $(echo "$line" | sed 's|[^;]||g' | wc -c) -ne 26 ]&lt;BR /&gt;   then&lt;BR /&gt;      echo $line &amp;gt; rejectfile&lt;BR /&gt;   else &lt;BR /&gt;      echo $line &amp;gt; another_file&lt;BR /&gt;   fi&lt;BR /&gt;&lt;BR /&gt;done&lt;BR /&gt;&lt;BR /&gt;This should result the number of ';' plus 1 which indicates the number of columns. The plus 1 is because of the newline 'character' at the end of the line, which is counted.&lt;BR /&gt;</description>
      <pubDate>Thu, 04 Dec 2003 01:43:41 GMT</pubDate>
      <guid>https://community.hpe.com/t5/operating-system-hp-ux/using-sed-or-awk-to-validate-a-file-content/m-p/3135676#M153972</guid>
      <dc:creator>Elmar P. Kolkman</dc:creator>
      <dc:date>2003-12-04T01:43:41Z</dc:date>
    </item>
    <item>
      <title>Re: Using sed or awk to validate a file content</title>
      <link>https://community.hpe.com/t5/operating-system-hp-ux/using-sed-or-awk-to-validate-a-file-content/m-p/3135677#M153973</link>
      <description>Hi Melvin,&lt;BR /&gt;a more traditional approach:&lt;BR /&gt; &lt;BR /&gt;#!/usr/bin/sh&lt;BR /&gt;# Check CORRECT num of delim in $1&lt;BR /&gt;&lt;BR /&gt;typeset -i NUM_CHARS=0 AFTER_DEL=0 DIFF=0 CORRECT=25&lt;BR /&gt;typeset DELIM="\;"&lt;BR /&gt;OK_FILE=./okrecs&lt;BR /&gt;REJ_FILE=./rejrecs&lt;BR /&gt;&lt;BR /&gt;while read line&lt;BR /&gt;do&lt;BR /&gt;        NUM_CHARS=$(echo "$line"| wc -c)&lt;BR /&gt;        AFTER_DEL=$(echo "$line" | tr -d $DELIM |wc -c)&lt;BR /&gt;        DIFF=$(( $NUM_CHARS - $AFTER_DEL ))&lt;BR /&gt;        if [ "$DIFF" = "$CORRECT" ]&lt;BR /&gt;        then&lt;BR /&gt;                echo "$line"  &amp;gt;&amp;gt; $OK_FILE&lt;BR /&gt;        else&lt;BR /&gt;                echo "$line"  &amp;gt;&amp;gt; $REJ_FILE&lt;BR /&gt;&lt;BR /&gt;        fi&lt;BR /&gt;done &amp;lt;$1&lt;BR /&gt; &lt;BR /&gt;Set OK_FILE and REJ_FILE to something appropriate and run it with your infile as $1.&lt;BR /&gt; &lt;BR /&gt;regards,&lt;BR /&gt;John K.</description>
      <pubDate>Thu, 04 Dec 2003 03:08:43 GMT</pubDate>
      <guid>https://community.hpe.com/t5/operating-system-hp-ux/using-sed-or-awk-to-validate-a-file-content/m-p/3135677#M153973</guid>
      <dc:creator>john korterman</dc:creator>
      <dc:date>2003-12-04T03:08:43Z</dc:date>
    </item>
    <item>
      <title>Re: Using sed or awk to validate a file content</title>
      <link>https://community.hpe.com/t5/operating-system-hp-ux/using-sed-or-awk-to-validate-a-file-content/m-p/3135678#M153974</link>
      <description>Oops... I see my solution truncates the resulting files, returning only the last reject and good line. Should be &amp;gt;&amp;gt; instead of &amp;gt; in the echo's, but you had already seen that of course ;-)</description>
      <pubDate>Thu, 04 Dec 2003 03:21:12 GMT</pubDate>
      <guid>https://community.hpe.com/t5/operating-system-hp-ux/using-sed-or-awk-to-validate-a-file-content/m-p/3135678#M153974</guid>
      <dc:creator>Elmar P. Kolkman</dc:creator>
      <dc:date>2003-12-04T03:21:12Z</dc:date>
    </item>
    <item>
      <title>Re: Using sed or awk to validate a file content</title>
      <link>https://community.hpe.com/t5/operating-system-hp-ux/using-sed-or-awk-to-validate-a-file-content/m-p/3135679#M153975</link>
      <description>&lt;BR /&gt;I'm curious to know why, judging by assigned points, the author appears to value a simple one-line solution with the requested tool less than a broken complex solution with will perform like a dog due multiple forks per record.&lt;BR /&gt;(And there was even a bonus solution explanation! :-)&lt;BR /&gt;&lt;BR /&gt;The explanation could be that I simply read too much in the points assigned. The author assigning medium points to a first solution, seeing if something better still will show up.&lt;BR /&gt;&lt;BR /&gt;[no, I don't need more points, my boss might get worried where I found the time :^].&lt;BR /&gt;&lt;BR /&gt;Just curious,&lt;BR /&gt;   Hein.</description>
      <pubDate>Thu, 04 Dec 2003 09:44:45 GMT</pubDate>
      <guid>https://community.hpe.com/t5/operating-system-hp-ux/using-sed-or-awk-to-validate-a-file-content/m-p/3135679#M153975</guid>
      <dc:creator>Hein van den Heuvel</dc:creator>
      <dc:date>2003-12-04T09:44:45Z</dc:date>
    </item>
    <item>
      <title>Re: Using sed or awk to validate a file content</title>
      <link>https://community.hpe.com/t5/operating-system-hp-ux/using-sed-or-awk-to-validate-a-file-content/m-p/3135680#M153976</link>
      <description>A great book you need to get is:&lt;BR /&gt;&lt;BR /&gt;The AWK Programming Language by the original authors of the language... Aho, Weinberger, Kernigan&lt;BR /&gt;&lt;BR /&gt;It is a great little book and you will be a master of awk after you are done. also the Awk/sed book from O'reilly is good as well. should be able to find it at a used book store for $5.</description>
      <pubDate>Thu, 04 Dec 2003 10:08:17 GMT</pubDate>
      <guid>https://community.hpe.com/t5/operating-system-hp-ux/using-sed-or-awk-to-validate-a-file-content/m-p/3135680#M153976</guid>
      <dc:creator>Todd McDaniel_1</dc:creator>
      <dc:date>2003-12-04T10:08:17Z</dc:date>
    </item>
    <item>
      <title>Re: Using sed or awk to validate a file content</title>
      <link>https://community.hpe.com/t5/operating-system-hp-ux/using-sed-or-awk-to-validate-a-file-content/m-p/3135681#M153977</link>
      <description>I'm with Hein on this.&lt;BR /&gt;My one-liner would be marginally shorter still...&lt;BR /&gt;&lt;BR /&gt;awk -F\; '(NF!=26) {print &amp;gt;&amp;gt; "bad-file";next} {print}' mixed-file &amp;gt; good-file&lt;BR /&gt;&lt;BR /&gt;-- Graham&lt;BR /&gt;</description>
      <pubDate>Thu, 04 Dec 2003 10:09:48 GMT</pubDate>
      <guid>https://community.hpe.com/t5/operating-system-hp-ux/using-sed-or-awk-to-validate-a-file-content/m-p/3135681#M153977</guid>
      <dc:creator>Graham Cameron_1</dc:creator>
      <dc:date>2003-12-04T10:09:48Z</dc:date>
    </item>
    <item>
      <title>Re: Using sed or awk to validate a file content</title>
      <link>https://community.hpe.com/t5/operating-system-hp-ux/using-sed-or-awk-to-validate-a-file-content/m-p/3135682#M153978</link>
      <description>cat &lt;YOURFILE&gt; | awk '{&lt;BR /&gt;if(split($0,a,";")!=26) {print "bad"; exit}&lt;BR /&gt;}'&lt;BR /&gt;&lt;BR /&gt;Rgds&lt;BR /&gt;Jean-Luc&lt;/YOURFILE&gt;</description>
      <pubDate>Thu, 04 Dec 2003 11:48:01 GMT</pubDate>
      <guid>https://community.hpe.com/t5/operating-system-hp-ux/using-sed-or-awk-to-validate-a-file-content/m-p/3135682#M153978</guid>
      <dc:creator>Jean-Luc Oudart</dc:creator>
      <dc:date>2003-12-04T11:48:01Z</dc:date>
    </item>
    <item>
      <title>Re: Using sed or awk to validate a file content</title>
      <link>https://community.hpe.com/t5/operating-system-hp-ux/using-sed-or-awk-to-validate-a-file-content/m-p/3135683#M153979</link>
      <description>Why does no-one suggest perl?&lt;BR /&gt;&lt;BR /&gt;# perl -paF\; -e'select(@F==26?STDOUT:STDERR)' mixed-file &amp;gt; good-file 2&amp;gt;bad-file&lt;BR /&gt;&lt;BR /&gt;Enjoy, Have FUN! H.Merijn</description>
      <pubDate>Thu, 04 Dec 2003 12:26:13 GMT</pubDate>
      <guid>https://community.hpe.com/t5/operating-system-hp-ux/using-sed-or-awk-to-validate-a-file-content/m-p/3135683#M153979</guid>
      <dc:creator>H.Merijn Brand (procura</dc:creator>
      <dc:date>2003-12-04T12:26:13Z</dc:date>
    </item>
    <item>
      <title>Re: Using sed or awk to validate a file content</title>
      <link>https://community.hpe.com/t5/operating-system-hp-ux/using-sed-or-awk-to-validate-a-file-content/m-p/3135684#M153980</link>
      <description>Perhaps because the question was to do it in sed or awk? ;-)&lt;BR /&gt;</description>
      <pubDate>Thu, 04 Dec 2003 14:04:53 GMT</pubDate>
      <guid>https://community.hpe.com/t5/operating-system-hp-ux/using-sed-or-awk-to-validate-a-file-content/m-p/3135684#M153980</guid>
      <dc:creator>Elmar P. Kolkman</dc:creator>
      <dc:date>2003-12-04T14:04:53Z</dc:date>
    </item>
    <item>
      <title>Re: Using sed or awk to validate a file content</title>
      <link>https://community.hpe.com/t5/operating-system-hp-ux/using-sed-or-awk-to-validate-a-file-content/m-p/3135685#M153981</link>
      <description>I am sorry Hein, if my point assignment somehow made you questionable. Actually, I valued your input and explanation. It was indeed a good simple one line solution!&lt;BR /&gt;&lt;BR /&gt;After looking through all the replies, I found that the solutions provided met the objective. Surprisingly, the performance (execution speed) of all these different ways of syntax and coding is almost equal. I tried the execution with a big file. Perhaps as Hein commented, different ways of coding might take more resources though. Well, that's a good point to consider!&lt;BR /&gt;&lt;BR /&gt;Lastly, I appreciate all your contributions here. Thank you very much!</description>
      <pubDate>Thu, 04 Dec 2003 22:31:34 GMT</pubDate>
      <guid>https://community.hpe.com/t5/operating-system-hp-ux/using-sed-or-awk-to-validate-a-file-content/m-p/3135685#M153981</guid>
      <dc:creator>Melvin Thong</dc:creator>
      <dc:date>2003-12-04T22:31:34Z</dc:date>
    </item>
    <item>
      <title>Re: Using sed or awk to validate a file content</title>
      <link>https://community.hpe.com/t5/operating-system-hp-ux/using-sed-or-awk-to-validate-a-file-content/m-p/3135686#M153982</link>
      <description>And thank you for a quick closure message!&lt;BR /&gt;Hein.</description>
      <pubDate>Thu, 04 Dec 2003 23:27:18 GMT</pubDate>
      <guid>https://community.hpe.com/t5/operating-system-hp-ux/using-sed-or-awk-to-validate-a-file-content/m-p/3135686#M153982</guid>
      <dc:creator>Hein van den Heuvel</dc:creator>
      <dc:date>2003-12-04T23:27:18Z</dc:date>
    </item>
  </channel>
</rss>

