Solved

text scripting

Posted on 2002-07-29
9
227 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
Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

 
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 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

Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
CPU monthly average? 12 120
Java core in Solaris 10 1 281
check unix curl command return value 7 114
How to check the PVU´s on AIX TSM servers? 3 91
Attention: This article will no longer be maintained. If you have any questions, please feel free to mail me. jgh@FreeBSD.org Please see http://www.freebsd.org/doc/en_US.ISO8859-1/articles/freebsd-update-server/ for the updated article. It is avail…
When you do backups in the Solaris Operating System, the file system must be inactive. Otherwise, the output may be inconsistent. A file system is inactive when it's unmounted or it's write-locked by the operating system. Although the fssnap utility…
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…
Learn how to find files with the shell using the find and locate commands. Use locate to find a needle in a haystack.: With locate, check if the file still exists.: Use find to get the actual location of the file.:

776 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