• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 238
  • Last Modified:

text scripting

I need some help with text scripting...

Here's my problem.
I need to read in a text file like below...

sadfk
#sadsd
sdfaa
sdfsd
ORA
kkkfdfsf
sdfffs
ORA

If i find a text string ORA i want to echo that line and the previous 2 lines to a text file.

so reading the above example would produce:

sdfaa
sdfsd
ORA
kkkfdfsf
sdfffs
ORA


0
zeb
Asked:
zeb
1 Solution
 
OtetelisanuCommented:
#

# old file is bb

for i in `cat bb`
do

if tail -1 bb|grep ORA
then

# new file
tail -3 bb >> FILE.txt

sed '$d' bb >bb.tmp
cp bb.tmp bb

else

sed '$d' bb >bb.tmp
cp bb.tmp bb

fi

done

rm bb.tmp

0
 
zebAuthor Commented:
sorry but could you explain this a bit?

basically the sed '$d' bb >bb.tmp line in  particular.

thanks.
0
 
OtetelisanuCommented:
sed '$d' bb >bb.tmp
deleted last row and write to bb.tmp but not last row
$ is the last row.
0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
OtetelisanuCommented:
sed '$d' bb >bb.tmp
deleted last row and write to bb.tmp
 $ is the last row.
0
 
billwccCommented:
Hey, Zeb,

Here's an possible alternative.  The advantage to Perl
is that when you inevitably want to do something more
complicated with your data, Perl can easily handle it.

With Perl, you can get arbitrarily fancy with your output,
or just keep it simple.

Hope this is of some help.

#/path/to/perl #############################
open (FILE, "/path/to/your/file") or
die "Couldn't open input file: $!\n";

# read the file into an array line by line
@array = ();
while (<FILE>) {
    chomp;
    push (@array, $_);
}

# iterate thru array searching for "ORA"
# print line and 2 before it if found.
$arraysize = @array;
for ($i = 0; $i <  $arraysize; $i++) {
    next if ! /ORA/;                                                           #didn't find it--skip
    print ("$array[$i-2]\n$array[$i-1]\n$array[$i]\n";  #did find it--print
}
######################################
   
0
 
yuzhCommented:
I believe that the following script is easy to undersatnd and do the job for you:

#!/bin/ksh
#
# usage: scriptname INFILE OUTFILE
INFILE=$1
OUTFILE=$2

LINE1=""
LINE2=""
LINE3=""
LINE=""
LINECT=0        # Line counter
cat /dev/null > ${OUTFILE}

exec 0<$1
while read LINE ; do
  LINECT=`expr ${LINECT} + 1`
  LINE1=${LINE2}
  LINE2=${LINE3}
  LINE3=${LINE}
  if [ "${LINE}" = "ORA" ] ; then
     case ${LINECT} in
         1) echo "${LINE3}" >> ${OUTFILE} ;;
         2) echo "${LINE2}\n${LINE3}" >> ${OUTFILE} ;;
         *) echo "${LINE1}\n${LINE2}\n${LINE3}" >> ${OUTFILE} ;;
     esac
     # LINECT=0        # Line counter
   fi
done

exit

#=======================================================

   Note: the above script knows how to handle if "ORA" in 1st, 2nd line situations. if you want to handle your have
repeat "ORA" records in file, eg:
   aaa
   ORA
   ORA
   ORA
   mmm
   nnn

   you need to uncomment
   # LINECT=0        
   between "esac" and "fi"

   Cheers!

===============
yuzh


0
 
blowfishCommented:
Being one who likes to do things with the least number of keystrokes, I came up with the following:

  perl -0 -n -e 'print $1, while (m/(([^\n]*\n){0,2}ORA[\n])/gs)' data.txt

(that's perl -{number 0}, not perl -{letter O})

--frankf

PS.

If you need to print the 2 lines before AND the two lines after, use this:

  perl -0 -n -e 'print $1, while (m/(([^\n]*\n){0,2}ORA[\n]([^\n]*\n){0,2})/gs)' data.txt

Regards.
0
 
tfewsterCommented:
No comment has been added lately, so it's time to clean up this Topic Area.
I will leave a recommendation for this question in the Cleanup topic area as follows:

- Answered by Otetelisanu
(IMHO the other solutions were better, but Otetelisanu gave the first working answer)

Please leave any comments here within the next 7 days

PLEASE DO NOT ACCEPT THIS COMMENT AS AN ANSWER !

tfewster
Cleanup Volunteer
0
 
SpideyModCommented:
per recommendation

SpideyMod
Community Support Moderator @Experts Exchange
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Tackle projects and never again get stuck behind a technical roadblock.
Join Now