Solved

text scripting

Posted on 2002-07-29
9
224 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
Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

 
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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Installing FreeBSD… FreeBSD is a darling of an operating system. The stability and usability make it a clear choice for servers and desktops (for the cunning). Savvy?  The Ports collection makes available every popular FOSS application and packag…
I promised to write further about my project, and here I am.  First, I needed to setup the Primary Server.  You can read how in this article: Setup FreeBSD Server with full HDD encryption (http://www.experts-exchange.com/OS/Unix/BSD/FreeBSD/A_3660-S…
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.:
In a previous video, we went over how to export a DynamoDB table into Amazon S3.  In this video, we show how to load the export from S3 into a DynamoDB table.

706 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

19 Experts available now in Live!

Get 1:1 Help Now