stepping through ascii file


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers stepping through ascii file
# 1  
Old 08-28-2002
Data stepping through ascii file

How can one step through an ascii file line by line looking for a word and then pull out the whole section that the word is in. I have a project working Oracles tkprof utility where I am trying to look for a word and need to pull out the section of the ascii file that the word is in. I have not worked with perl ever. I am trying to do this through a ksh file. Please help.

Here is an example of what I am looking for:
Say I am searching for the section that contains the word world.
The file that I am looking through though is as follows

Go to the store.
Get milk.
***************
Then go to the laundromat.
Get the clothes.

*****************
Go the bookstore.
Get a book on world travel.

*******************
Then come home.

*********************
end.

How would I pull out just the section with the word world in it?
# 2  
Old 08-28-2002
Can we assume that your ASCII file with have asterisks (*) as group delimiter and that the data within the group will not have a asterisk character in the group data?
# 3  
Old 08-28-2002
This thread may help. Discusses pulling a section from a text file based on first occurance of two different strings.
# 4  
Old 08-29-2002
auswipe,
You are correct in what you assumed.
# 5  
Old 08-29-2002
This script seem to work:
Code:
#! /usr/bin/ksh
target=$1
sed -n -e '
${
b tester
}
s/\([*][*]*\)/\1/
t tester
H
b
:tester
H
x
s/\('${target}'\)/\1/
t print
s/.*//
h
b
:print
p
s/.*//
h'
exit 0

# 6  
Old 08-29-2002
Here is my solution in Perl. It would be advisable to change it to accept command line parameters and whatnought, but this is a start if you wish to use a Perl solution.

Code:
#!/usr/bin/perl

open(IN_FILE, "text.txt") || die "$!";

my $foundKeyWord   = 0;
my @statements;
my $keyWord = "world";

while ($inputLine = <IN_FILE>) {

  chomp($inputLine);
  $statements[$#statements+1] = $inputLine;

  if ($inputLine =~ m/$keyWord/ig) {
    $foundKeyWord = 1;
  };

  if ($inputLine =~ m/\*/g) {
    if ($foundKeyWord) {
      $foundKeyWord   = 0;

      for ($popCount = 1; $popCount < $#statements; $popCount++) {
        print  "$statements[$popCount]\n";
      };
    };
    splice(@statements,0,$#statements);
  };

};

My test data:

Code:
Go to the store. 
Get milk. 
*************** 
Then go to the laundromat. 
Get the clothes. 

***************** 
Go the bookstore. 
Get a book on world travel. 

******************* 
Then come home. 

********************* 
end.

Sample output:

Code:
FreeBSD:auswipe:/home/auswipe/sample $ ./pullGroup.pl
Go the bookstore.
Get a book on world travel.

FreeBSD:auswipe:/home/auswipe/sample $

# 7  
Old 08-29-2002
Perderabo what you posted isn't working for me. The only thing I changed is the $target to FULL (the word I am searching for). Smilie
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Convert Hex to Ascii in a Ascii file

Hi All, I have an ascii file in which few columns are having hex values which i need to convert into ascii. Kindly suggest me what command can be used in unix shell scripting? Thanks in Advance (2 Replies)
Discussion started by: HemaV
2 Replies

2. UNIX for Dummies Questions & Answers

After Ftp'ing file to destination how to check the file if it is in correct ASCII and not corrupted

Hi Folks, While transferring file from FTP software like Filezilla the files gets corrupted. Is there any way I can check if the recently transferred file is in ASCII and not corrupted. I have tried using file -i filename command which does tell if the file character set is ASCII or binary... (6 Replies)
Discussion started by: Khan28
6 Replies

3. Shell Programming and Scripting

Perl Debug Stepping Answering Questions

I am new to perl and want to get a little better understanding of debugging code in perl. I have a perl script that has questions to be answered like: he following PERL modules are recommended: Crypt::DES Crypt::PasswdMD5 IO::Pty Net::Write::Layer2 String::CRC32 Attempt to install... (0 Replies)
Discussion started by: metallica1973
0 Replies

4. Cybersecurity

Firewall bypass or stepping stone security question

Hi, I really do not know how to describe this problem; but, I think it's a firewall issue. My Distro is Slackware 12.0 (somewhat updated). My company firewall uses Netfilter and the e-mail server uses Sendmail. Let's say the firewall's Ext IP = A and Internal DMZ IP = B. The firewall's... (0 Replies)
Discussion started by: cc_ew
0 Replies

5. Solaris

stepping through newfs

On a RAID-5 solaris 9 server, we replaced a bad disk. Upon boot up, a mount point failed: vxvm:vxvol: ERROR: Volume IQ_Staging is not startable; some subdisks are unusable and the parity is stale With Sun tech support, we tried vxvol start and vxvol resync, but it remained... (3 Replies)
Discussion started by: abstractrick
3 Replies

6. SCO

Stair-stepping printing

Hi, We have a Unix 3.2v5.0.5. I installed a printer via scoadmin, HP network printer manager with network peripheral name an ip-adress. I have a lot of trouble, the printer do stair-stepping. I know it is because of the translation LF to CR/LF. But it is impossible to set this correct in... (2 Replies)
Discussion started by: haezeban
2 Replies

7. Shell Programming and Scripting

convert ascii values into ascii characters

Hi gurus, I have a file in unix with ascii values. I need to convert all the ascii values in the file to ascii characters. File contains nearly 20000 records with ascii values. (10 Replies)
Discussion started by: sandeeppvk
10 Replies

8. Linux

Stair stepping

Is there a command line flag to lpr or a utility similar to the Unix 'xtod' command to fix stair stepping when printing? Under Unix I would: # cat textfile | xtod | lp -d lineprinter Anything like that for linux? Thanks (1 Reply)
Discussion started by: lochraven
1 Replies

9. Shell Programming and Scripting

Rsync script in cron from stepping on itself

I have the following rsync script that I use for syncing MySQL files from one server to another. I run the script at 20 minutes past every second hour in cron. I want to make sure that the script completes in it's entirety before it is set to kick off again. For example, when the script starts at... (3 Replies)
Discussion started by: sunsysadm2003
3 Replies

10. UNIX for Dummies Questions & Answers

Scripting stepping stones.

It's safe to say that I have had no scripting / programming experience before. I took a job as an AIX / Linux administrator, and I have found myself up to my ears in bash scripting. I'm starting to really catch on, I've become a casual linux user, to a hopeful future power user. I was... (2 Replies)
Discussion started by: syndex
2 Replies
Login or Register to Ask a Question