<?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: Shell script. in Operating System - HP-UX</title>
    <link>https://community.hpe.com/t5/operating-system-hp-ux/shell-script/m-p/2433096#M728906</link>
    <description>Hi Curt !&lt;BR /&gt;I read your script closely. I find it a little more complicated than the Andreas and Alan's scripts. I'm afraid your script would be more time and cpu consuming than the Andreas and Alan's ones.&lt;BR /&gt;&lt;BR /&gt;Thank you very much for your contribution!&lt;BR /&gt;Pat.</description>
    <pubDate>Fri, 28 Jul 2000 05:45:25 GMT</pubDate>
    <dc:creator>LEJARRE Patrick</dc:creator>
    <dc:date>2000-07-28T05:45:25Z</dc:date>
    <item>
      <title>Shell script.</title>
      <link>https://community.hpe.com/t5/operating-system-hp-ux/shell-script/m-p/2433086#M728896</link>
      <description>I would like to extract from a input text file the lines that would match the following specifications:&lt;BR /&gt;The extracted lines should contain a given string&lt;BR /&gt;AND&lt;BR /&gt;The line before the first occurence in the input file should also be extracted&lt;BR /&gt;AND&lt;BR /&gt;All the extracted lines should be deleted from the input file.&lt;BR /&gt;The result should be placed ino a output text file.&lt;BR /&gt;&lt;BR /&gt;I think that an "awk" guru could help me best.&lt;BR /&gt;</description>
      <pubDate>Thu, 27 Jul 2000 11:23:50 GMT</pubDate>
      <guid>https://community.hpe.com/t5/operating-system-hp-ux/shell-script/m-p/2433086#M728896</guid>
      <dc:creator>LEJARRE Patrick</dc:creator>
      <dc:date>2000-07-27T11:23:50Z</dc:date>
    </item>
    <item>
      <title>Re: Shell script.</title>
      <link>https://community.hpe.com/t5/operating-system-hp-ux/shell-script/m-p/2433087#M728897</link>
      <description>Hi,&lt;BR /&gt;&lt;BR /&gt;here you are:&lt;BR /&gt;&lt;BR /&gt;awk '{&lt;BR /&gt;if($0 ~ "pattern")&lt;BR /&gt;{&lt;BR /&gt;  print $0;&lt;BR /&gt;  print prev;&lt;BR /&gt;}&lt;BR /&gt;prev=$0;&lt;BR /&gt;}' &lt;FILE&gt;&lt;BR /&gt;&lt;BR /&gt;You have to modify "pattern" for your requirements.&lt;BR /&gt;&lt;BR /&gt;Cheers&lt;BR /&gt;&lt;BR /&gt;Andrew&lt;/FILE&gt;</description>
      <pubDate>Thu, 27 Jul 2000 11:34:27 GMT</pubDate>
      <guid>https://community.hpe.com/t5/operating-system-hp-ux/shell-script/m-p/2433087#M728897</guid>
      <dc:creator>Andreas Voss</dc:creator>
      <dc:date>2000-07-27T11:34:27Z</dc:date>
    </item>
    <item>
      <title>Re: Shell script.</title>
      <link>https://community.hpe.com/t5/operating-system-hp-ux/shell-script/m-p/2433088#M728898</link>
      <description>Sorry title mistake in the output order:&lt;BR /&gt;Instead:&lt;BR /&gt;print $0&lt;BR /&gt;print prev&lt;BR /&gt;use:&lt;BR /&gt;print prev&lt;BR /&gt;print $0</description>
      <pubDate>Thu, 27 Jul 2000 11:35:49 GMT</pubDate>
      <guid>https://community.hpe.com/t5/operating-system-hp-ux/shell-script/m-p/2433088#M728898</guid>
      <dc:creator>Andreas Voss</dc:creator>
      <dc:date>2000-07-27T11:35:49Z</dc:date>
    </item>
    <item>
      <title>Re: Shell script.</title>
      <link>https://community.hpe.com/t5/operating-system-hp-ux/shell-script/m-p/2433089#M728899</link>
      <description>hi&lt;BR /&gt;your answer helped me a lot but the lines containing the search string are printed twice:&lt;BR /&gt;&lt;BR /&gt;awk '{if ($0 ~ "pattern") {print prev;print $0;} prev=$0;}' inputfile &amp;gt; outputfile&lt;BR /&gt;&lt;BR /&gt;I solved the problem with the following additional commands:&lt;BR /&gt;&lt;BR /&gt;head -1 outputfile &amp;gt; outputfile1&lt;BR /&gt;line=`wc -l outputfile | awk '{print $1}'`&lt;BR /&gt;line=`expr $line - 1`&lt;BR /&gt;tail -$line outputfile | sort | uniq &amp;gt; outputfile2&lt;BR /&gt;cat outputfile1 outputfile2 &amp;gt; outputfile&lt;BR /&gt;&lt;BR /&gt;I don't know wether it is possible to do it within the awk command, but it works perfectly. &lt;BR /&gt;&lt;BR /&gt;Thanks.&lt;BR /&gt;</description>
      <pubDate>Thu, 27 Jul 2000 13:23:13 GMT</pubDate>
      <guid>https://community.hpe.com/t5/operating-system-hp-ux/shell-script/m-p/2433089#M728899</guid>
      <dc:creator>LEJARRE Patrick</dc:creator>
      <dc:date>2000-07-27T13:23:13Z</dc:date>
    </item>
    <item>
      <title>Re: Shell script.</title>
      <link>https://community.hpe.com/t5/operating-system-hp-ux/shell-script/m-p/2433090#M728900</link>
      <description>The initial goals aren't fullfilled yet.&lt;BR /&gt;The extracted lines should be deleted from the input file.</description>
      <pubDate>Thu, 27 Jul 2000 13:29:34 GMT</pubDate>
      <guid>https://community.hpe.com/t5/operating-system-hp-ux/shell-script/m-p/2433090#M728900</guid>
      <dc:creator>LEJARRE Patrick</dc:creator>
      <dc:date>2000-07-27T13:29:34Z</dc:date>
    </item>
    <item>
      <title>Re: Shell script.</title>
      <link>https://community.hpe.com/t5/operating-system-hp-ux/shell-script/m-p/2433091#M728901</link>
      <description>Hi Andrew !&lt;BR /&gt;Below the solution.&lt;BR /&gt;&lt;BR /&gt;Take a look at the -v option of awk to assign a parameter to be passed to the command.&lt;BR /&gt;&lt;BR /&gt;Pat&lt;BR /&gt;&lt;BR /&gt;---------------------------------------------&lt;BR /&gt;Shell script&lt;BR /&gt;---------------------------------------------&lt;BR /&gt;&lt;BR /&gt;#!/sbin/sh&lt;BR /&gt;&lt;BR /&gt;# Initialisation&lt;BR /&gt;search_string=$1&lt;BR /&gt;inputfile=$2&lt;BR /&gt;outputfile=$3&lt;BR /&gt;outputfile1=${outputfile}_tmp1&lt;BR /&gt;outputfile2=${outputfile}_tmp2&lt;BR /&gt;&lt;BR /&gt;# Main&lt;BR /&gt;awk -v search_string=$search_string '{if ($0 ~ search_string) {print prev;print $0;} prev=$0;}' $inputfile &amp;gt; $outputfile&lt;BR /&gt;&lt;BR /&gt;head -1 ${outputfile} &amp;gt; $outputfile1&lt;BR /&gt;nb_lines=`wc -l ${outputfile} | awk '{print $1}'`&lt;BR /&gt;nb_lines=`expr $nb_lines - 1`&lt;BR /&gt;&lt;BR /&gt;tail -$nb_lines ${outputfile} | sort | uniq &amp;gt; $outputfile2&lt;BR /&gt;cat $outputfile1 $outputfile2 &amp;gt; ${outputfile}&lt;BR /&gt;&lt;BR /&gt;sed -e 's/ /?/g' ${outputfile} &amp;gt; ${outputfile}$$&lt;BR /&gt;mv ${outputfile}$$ ${outputfile}&lt;BR /&gt;sed -e 's/"/@/g' ${outputfile} &amp;gt; ${outputfile}$$&lt;BR /&gt;mv ${outputfile}$$ ${outputfile}&lt;BR /&gt;&lt;BR /&gt;sed -e 's/ /?/g' ${inputfile} &amp;gt; ${inputfile}$$&lt;BR /&gt;mv ${inputfile}$$ ${inputfile}&lt;BR /&gt;sed -e 's/"/@/g' ${inputfile} &amp;gt; ${inputfile}$$&lt;BR /&gt;mv ${inputfile}$$ ${inputfile}&lt;BR /&gt;&lt;BR /&gt;ligne=`head -1 ${outputfile}`&lt;BR /&gt;&lt;BR /&gt;grep -v "$ligne" ${inputfile} &amp;gt; ${inputfile}$$&lt;BR /&gt;mv ${inputfile}$$ ${inputfile}&lt;BR /&gt;&lt;BR /&gt;grep -v "$search_string" ${inputfile} &amp;gt; ${inputfile}$$&lt;BR /&gt;mv ${inputfile}$$ ${inputfile}&lt;BR /&gt;&lt;BR /&gt;sed -e 's/?/ /g' ${outputfile} &amp;gt; ${outputfile}$$&lt;BR /&gt;mv ${outputfile}$$ ${outputfile}&lt;BR /&gt;sed -e 's/@/"/g' ${outputfile} &amp;gt; ${outputfile}$$&lt;BR /&gt;mv ${outputfile}$$ ${outputfile}&lt;BR /&gt;&lt;BR /&gt;sed -e 's/?/ /g' ${inputfile} &amp;gt; ${inputfile}$$&lt;BR /&gt;mv ${inputfile}$$ ${inputfile}&lt;BR /&gt;sed -e 's/@/"/g' ${inputfile} &amp;gt; ${inputfile}$$&lt;BR /&gt;mv ${inputfile}$$ ${inputfile}&lt;BR /&gt;</description>
      <pubDate>Thu, 27 Jul 2000 15:36:16 GMT</pubDate>
      <guid>https://community.hpe.com/t5/operating-system-hp-ux/shell-script/m-p/2433091#M728901</guid>
      <dc:creator>LEJARRE Patrick</dc:creator>
      <dc:date>2000-07-27T15:36:16Z</dc:date>
    </item>
    <item>
      <title>Re: Shell script.</title>
      <link>https://community.hpe.com/t5/operating-system-hp-ux/shell-script/m-p/2433092#M728902</link>
      <description>Pat:&lt;BR /&gt;&lt;BR /&gt;Let's use Andreas' suggestion amended as follows:&lt;BR /&gt;&lt;BR /&gt;awk '{ &lt;BR /&gt;if ($0 ~ /pattern/) &lt;BR /&gt;{ &lt;BR /&gt;print $0   &amp;gt;&amp;gt; "/tmp/file.1";&lt;BR /&gt;print prev &amp;gt;&amp;gt; "/tmp/file.1";&lt;BR /&gt;} &lt;BR /&gt;else&lt;BR /&gt;{&lt;BR /&gt;print $0   &amp;gt;&amp;gt; "/tmp/file.2";&lt;BR /&gt;prev=$0; &lt;BR /&gt;}&lt;BR /&gt;}' /tmp/file&lt;BR /&gt;&lt;BR /&gt;I think you will find that /tmp/file.2 is the input file stripped according to your mandate.&lt;BR /&gt;&lt;BR /&gt;...JRF...</description>
      <pubDate>Thu, 27 Jul 2000 16:37:15 GMT</pubDate>
      <guid>https://community.hpe.com/t5/operating-system-hp-ux/shell-script/m-p/2433092#M728902</guid>
      <dc:creator>James R. Ferguson</dc:creator>
      <dc:date>2000-07-27T16:37:15Z</dc:date>
    </item>
    <item>
      <title>Re: Shell script.</title>
      <link>https://community.hpe.com/t5/operating-system-hp-ux/shell-script/m-p/2433093#M728903</link>
      <description>How about:&lt;BR /&gt;&lt;BR /&gt;INFILE=xxxx&lt;BR /&gt;OUTFILE=yyyyy&lt;BR /&gt;&lt;BR /&gt;awk '{if($0 ~ "AND")&lt;BR /&gt;{&lt;BR /&gt;print prev&lt;BR /&gt;print $0&lt;BR /&gt;}&lt;BR /&gt;prev=$0&lt;BR /&gt;}' $INFILE &amp;gt; $OUTFILE&lt;BR /&gt;while read LINE&lt;BR /&gt;do&lt;BR /&gt;grep -v "$LINE" $INFILE &amp;gt; tmpfile&lt;BR /&gt;mv tmpfile $INFILE&lt;BR /&gt;done &amp;lt; $OUTFILE</description>
      <pubDate>Thu, 27 Jul 2000 17:10:07 GMT</pubDate>
      <guid>https://community.hpe.com/t5/operating-system-hp-ux/shell-script/m-p/2433093#M728903</guid>
      <dc:creator>Alan Riggs</dc:creator>
      <dc:date>2000-07-27T17:10:07Z</dc:date>
    </item>
    <item>
      <title>Re: Shell script.</title>
      <link>https://community.hpe.com/t5/operating-system-hp-ux/shell-script/m-p/2433094#M728904</link>
      <description>#!/usr/bin/ksh&lt;BR /&gt;&lt;BR /&gt;mfile=file_before_deletions&lt;BR /&gt;&lt;BR /&gt;file1=mfile_after_deletions&lt;BR /&gt;file2=deletions_from_mfile&lt;BR /&gt;&lt;BR /&gt;pattern=your_pattern&lt;BR /&gt;&lt;BR /&gt;count=$(grep -c $pattern $mfile)&lt;BR /&gt;&lt;BR /&gt;#if count = 0 no lines to delete&lt;BR /&gt;#&lt;BR /&gt;if (( $count == 0 )) ;then&lt;BR /&gt; cp $mfile $file1&lt;BR /&gt; touch $file2&lt;BR /&gt;else&lt;BR /&gt; #get line number of first occurance&lt;BR /&gt; #&lt;BR /&gt; line_num=$(grep -n $pattern $mfile |       awk -F: '{print $1;exit;}')&lt;BR /&gt;&lt;BR /&gt; #if the first occurance is the first line&lt;BR /&gt; #you can't delete the previous line&lt;BR /&gt; #so just delete the lines with the pattern&lt;BR /&gt; #&lt;BR /&gt; if (( $line_num == 1 )) ;then&lt;BR /&gt;  grep -v $pattern $mfile &amp;gt; $file1&lt;BR /&gt;  grep    $pattern $mfile &amp;gt; $file2&lt;BR /&gt; else&lt;BR /&gt;  #&lt;BR /&gt;  # get the line number of the line&lt;BR /&gt;  # before the first occurance&lt;BR /&gt;  #&lt;BR /&gt;  (( line_num = $line_num - 1 ))&lt;BR /&gt;&lt;BR /&gt;  # use sed to delete line then grep for all rest&lt;BR /&gt;  #&lt;BR /&gt;  sed -e "${line_num}d" $mfile |   grep -v $pattern  &amp;gt; $file1&lt;BR /&gt;&lt;BR /&gt;  #for deletions file use sed to print the line&lt;BR /&gt;  #before the first occurance then grep the rest&lt;BR /&gt;  #&lt;BR /&gt;  sed -ne "${line_num}p" $mfile &amp;gt; $file2&lt;BR /&gt;  grep $pattern $mfile &amp;gt;&amp;gt; $file2&lt;BR /&gt; fi&lt;BR /&gt;fi</description>
      <pubDate>Thu, 27 Jul 2000 18:25:07 GMT</pubDate>
      <guid>https://community.hpe.com/t5/operating-system-hp-ux/shell-script/m-p/2433094#M728904</guid>
      <dc:creator>curt larson</dc:creator>
      <dc:date>2000-07-27T18:25:07Z</dc:date>
    </item>
    <item>
      <title>Re: Shell script.</title>
      <link>https://community.hpe.com/t5/operating-system-hp-ux/shell-script/m-p/2433095#M728905</link>
      <description>and if you know there is at least one occurance and it isn't the first line, then this will work also:&lt;BR /&gt;&lt;BR /&gt;ex -s +"/$pattern/ | .- d | 1,\$ global /$pattern/d | w &amp;gt; $file_with_deletions | q!" your_file</description>
      <pubDate>Thu, 27 Jul 2000 20:05:30 GMT</pubDate>
      <guid>https://community.hpe.com/t5/operating-system-hp-ux/shell-script/m-p/2433095#M728905</guid>
      <dc:creator>curt larson</dc:creator>
      <dc:date>2000-07-27T20:05:30Z</dc:date>
    </item>
    <item>
      <title>Re: Shell script.</title>
      <link>https://community.hpe.com/t5/operating-system-hp-ux/shell-script/m-p/2433096#M728906</link>
      <description>Hi Curt !&lt;BR /&gt;I read your script closely. I find it a little more complicated than the Andreas and Alan's scripts. I'm afraid your script would be more time and cpu consuming than the Andreas and Alan's ones.&lt;BR /&gt;&lt;BR /&gt;Thank you very much for your contribution!&lt;BR /&gt;Pat.</description>
      <pubDate>Fri, 28 Jul 2000 05:45:25 GMT</pubDate>
      <guid>https://community.hpe.com/t5/operating-system-hp-ux/shell-script/m-p/2433096#M728906</guid>
      <dc:creator>LEJARRE Patrick</dc:creator>
      <dc:date>2000-07-28T05:45:25Z</dc:date>
    </item>
    <item>
      <title>Re: Shell script.</title>
      <link>https://community.hpe.com/t5/operating-system-hp-ux/shell-script/m-p/2433097#M728907</link>
      <description>Well, my script  is more complicated for a couple of reasons:&lt;BR /&gt;&lt;BR /&gt;1) I extract only the line before the first occurance of the pattern as specified, not the line before every occurance of the pattern as alan's does&lt;BR /&gt;&lt;BR /&gt;2) my script handles the situation if the pattern occurs in the first line.  Alan's will output an empty line being prev will have no value at that time and will later remove all the blank lines from the file.  I don't think this was your intention.&lt;BR /&gt;&lt;BR /&gt;As far as time and cpu, my script would only do 3 greps and an awk on the file worst case (double that if you want to count creating the file of the deleted lines which could be removed if it isn't necessary), where if there is only 2 matches in the file alan's script will do 4 greps and an awk on the file.  And with more matches alan's would do even more greps increasing by 2 greps for every line with a pattern match.&lt;BR /&gt;&lt;BR /&gt;But, if you'd like to do it even faster:&lt;BR /&gt;&lt;BR /&gt;#!/usr/bin/ksh&lt;BR /&gt;&lt;BR /&gt;line_num=$(grep -n $pattern $mfile |     awk -F: '{print $1;exit;}')&lt;BR /&gt;&lt;BR /&gt;if [[ -n $line_num ]] ;then&lt;BR /&gt; if (( $line_num &amp;gt; 1 )) ;then&lt;BR /&gt;&lt;BR /&gt;  (( line_num = $line_num - 1 ))&lt;BR /&gt;  sed -e '$line_num d' &lt;BR /&gt;          -e '/$pattern/d' $mfile &amp;gt; $tmpfile&lt;BR /&gt;&lt;BR /&gt; else&lt;BR /&gt;  sed -e '/$pattern/d' $mfile &amp;gt; $tmpfile&lt;BR /&gt; fi&lt;BR /&gt; mv $tmpfile $mfile&lt;BR /&gt;fi&lt;BR /&gt;&lt;BR /&gt;sorry, but I have a different opinion then you</description>
      <pubDate>Fri, 28 Jul 2000 09:43:51 GMT</pubDate>
      <guid>https://community.hpe.com/t5/operating-system-hp-ux/shell-script/m-p/2433097#M728907</guid>
      <dc:creator>curt larson</dc:creator>
      <dc:date>2000-07-28T09:43:51Z</dc:date>
    </item>
    <item>
      <title>Re: Shell script.</title>
      <link>https://community.hpe.com/t5/operating-system-hp-ux/shell-script/m-p/2433098#M728908</link>
      <description>and if you really want to spend the time maintaining an awk script:&lt;BR /&gt;&lt;BR /&gt;#!/usr/bin/ksh&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;cat infile | awk '&lt;BR /&gt;&lt;BR /&gt;BEGIN {    First = "no";&lt;BR /&gt; Match = "no";&lt;BR /&gt; getline;&lt;BR /&gt; if ( $0 ~ /pattern/ ) {&lt;BR /&gt;  First = "yes";&lt;BR /&gt;  Match = "yes";&lt;BR /&gt; }&lt;BR /&gt; prev = $0;&lt;BR /&gt;}&lt;BR /&gt;{&lt;BR /&gt;   if ( $0 ~ /pattern/ ) {&lt;BR /&gt; if ( First == "no" ) {&lt;BR /&gt;  First = "yes";&lt;BR /&gt; }&lt;BR /&gt; else {&lt;BR /&gt;  if ( Match == "no" ) {&lt;BR /&gt;   print prev; }&lt;BR /&gt; }&lt;BR /&gt; Match = "yes";&lt;BR /&gt;  }&lt;BR /&gt;  else {&lt;BR /&gt; if ( Match == "no" ) {&lt;BR /&gt;  print prev; }&lt;BR /&gt; Match = "no";&lt;BR /&gt;  }&lt;BR /&gt;  prev = $0&lt;BR /&gt;}&lt;BR /&gt;END &lt;BR /&gt;{  if ( Match == "no" ) print prev; } &lt;BR /&gt;' &amp;gt; outfile&lt;BR /&gt;&lt;BR /&gt;mv outfile infile&lt;BR /&gt;&lt;BR /&gt;myself the greps, seds, and eds are simplier to understand.</description>
      <pubDate>Fri, 28 Jul 2000 11:30:33 GMT</pubDate>
      <guid>https://community.hpe.com/t5/operating-system-hp-ux/shell-script/m-p/2433098#M728908</guid>
      <dc:creator>curt larson</dc:creator>
      <dc:date>2000-07-28T11:30:33Z</dc:date>
    </item>
  </channel>
</rss>

