?
Solved

text scripting

Posted on 2002-07-29
9
Medium Priority
?
232 Views
Last Modified: 2013-12-27
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
Comment
Question by:zeb
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
9 Comments
 
LVL 4

Accepted Solution

by:
Otetelisanu earned 400 total points
ID: 7185346
#

# 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
 

Author Comment

by:zeb
ID: 7185586
sorry but could you explain this a bit?

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

thanks.
0
 
LVL 4

Expert Comment

by:Otetelisanu
ID: 7185657
sed '$d' bb >bb.tmp
deleted last row and write to bb.tmp but not last row
$ is the last row.
0
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!

 
LVL 4

Expert Comment

by:Otetelisanu
ID: 7185662
sed '$d' bb >bb.tmp
deleted last row and write to bb.tmp
 $ is the last row.
0
 
LVL 1

Expert Comment

by:billwcc
ID: 7186298
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
 
LVL 38

Expert Comment

by:yuzh
ID: 7186866
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
 
LVL 2

Expert Comment

by:blowfish
ID: 7218305
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
 
LVL 21

Expert Comment

by:tfewster
ID: 8003444
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
 

Expert Comment

by:SpideyMod
ID: 8065857
per recommendation

SpideyMod
Community Support Moderator @Experts Exchange
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Java performance on Solaris - Managing CPUs There are various resource controls in operating system which directly/indirectly influence the performance of application. one of the most important resource controls is "CPU".   In a multithreaded…
Every server (virtual or physical) needs a console: and the console can be provided through hardware directly connected, software for remote connections, local connections, through a KVM, etc. This document explains the different types of consol…
Learn how to navigate the file tree with the shell. Use pwd to print the current working directory: Use ls to list a directory's contents: Use cd to change to a new directory: Use wildcards instead of typing out long directory names: Use ../ to move…
This video shows how to set up a shell script to accept a positional parameter when called, pass that to a SQL script, accept the output from the statement back and then manipulate it in the Shell.
Suggested Courses

777 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question