Solved

text scripting

Posted on 2002-07-29
9
225 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
9 Comments
 
LVL 4

Accepted Solution

by:
Otetelisanu earned 100 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
 
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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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 20

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Problem Imaging Computers With Clonezilla 2 82
EMC VNX  storage pool  Vs  raid group  question 3 120
Parsing a file using ksh 10 56
Solaris 10.  Nmap installation fails 2 49
Introduction Regular patching is part of a system administrator's tasks. However, many patches require that the system be in single-user mode before they can be installed. A cluster patch in particular can take quite a while to apply if the machine…
Using libpcap/Jpcap to capture and send packets on Solaris version (10/11) Library used: 1.      Libpcap (http://www.tcpdump.org) Version 1.2 2.      Jpcap(http://netresearch.ics.uci.edu/kfujii/Jpcap/doc/index.html) Version 0.6 Prerequisite: 1.      GCC …
Learn how to get help with Linux/Unix bash shell commands. Use help to read help documents for built in bash shell commands.: Use man to interface with the online reference manuals for shell commands.: Use man to search man pages for unknown command…
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.

929 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

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now