01-24-2008
Perl how to move pointer to previous line in a txt file?
I have a text file that has blocks of text. Each block starts with ### and ends with End_###.
I wrote a perl script to search a string from line 2 (ignore any line starts with ###) of each block
if matched, need to print that whole block. According to the input file in below, it will print blocks #02, #04, #05.
Here is my question, I have no problems matching the SEARCH_STR, but once matched, how do I move back previous lines?
e.g. in block #02, matched in line 3, but I need to print lines 1 & 2.
Thanks in advance for any help!
MY SCRIPT:
=========
#!/46020/local/bin/perl -w
#
if ($#ARGV != 0) {
print "You must provide a search criteria.\n";
exit;
}
my $query = $ARGV[0];
my $INPUT = "/mypath/inputFILE.txt";
my $line;
my $tag = 0;
my @results = ();
open (FILE, "<$INPUT") or die "Cannot open '$INPUT' file: $!";
while ($line = <FILE>) {
next if ($line =~ /^#[0-9][0-9].* - / || $line =~ /^\s*$/);
if ($line =~ /$query/i) {
$tag = 1;
}
if ($line =~ /^End_#/ && $tag == 1) {
push (@results,$line) if ($tag == 1);
$tag = 0;
}
push (@results,$line) if ($tag == 1);
}
close (FILE);
print "@results\n";
exit;
INPUTFILE:
========
#01 - block start line here
some text here
more text here
more lines
more lines
more lines
End_#01
#02 - block start line here
some text here
this line contains the SEARCH_STR and something
more lines
more lines
End_#02
#03 - block start line here
some text here
more text here
more lines
more lines
more lines
End_#03
#04 - block start line here
some text here
more lines
more lines
the SEARCH_STR is here and something
some text here
more lines
more lines
more lines
End_#04
#05 - block start line here
some text here
more lines
the SEARCH_STR is here and something
more lines
End_#05
#06 - block start line here
some text here
more text here
more lines
more lines
more lines
End_#06
10 More Discussions You Might Find Interesting
1. Programming
Hello experts,
I ve a text file I want to go to particular line . what is the best way to do this in c ?
I am tried as follows
fseek ( fh, pos, SEEK_SET);
but this functions moves the file pointer according to a number of bytes. Unfortunately I don't know the exact byte... (7 Replies)
Discussion started by: user_prady
7 Replies
2. Shell Programming and Scripting
I'm a novice at unix and need it more and more to do my work.
I seem running into problems getting this script "attempt" to work:
I need to copy all files in a directory, which is containing 22000 files, into a directory one level up. There a tool monitors the content of the dir and processes... (2 Replies)
Discussion started by: compasscard
2 Replies
3. Shell Programming and Scripting
Scenario: Users drop files into a directory which is regularly polled by my PERL process. On detecting a file my process will move it from the poll dir to a working directory. A user created a file with a £ symbol in the filename and my process now fails.
e.g £££F3AERO££.txt
... (1 Reply)
Discussion started by: thefal9
1 Replies
4. Shell Programming and Scripting
I have multiple txt files which begin with the word "orders" in folder C:\source. I need to move the files to folder C:\dest and rename them to "process_<date>_<count>"
So for example , if there are 3 files ordersa.txt , ordersb.txt and ordersc.txt in C:\source , after running the script I want... (1 Reply)
Discussion started by: johannd
1 Replies
5. UNIX for Dummies Questions & Answers
I have multiple txt files which begin with the word "orders" in folder C:\source. I need to move the files to folder C:\dest and rename them to "process_<date>_<count>"
So for example , if there are 3 files ordersa.txt , ordersb.txt and ordersc.txt in C:\source , after running the script I want... (7 Replies)
Discussion started by: johannd
7 Replies
6. Shell Programming and Scripting
Hello,
I'm new to this forum. I've been doing a lot of sed work lately and have found many useful tips on this forum. I've hit a roadblock in a project, though, and could really use some help.
I have a text file with many lines like the following, i.e., some lines begin with a single word... (3 Replies)
Discussion started by: paroikoi
3 Replies
7. Shell Programming and Scripting
I need to read the contents of a file. Then I need to grep for a keyword and replace part of the grepped line based on the condition of previous and present line.
Example input file:
V {
port1 = P;
port2 = 0;
shift_port = P0; /* if next shift_port is P0 I need... (9 Replies)
Discussion started by: naveen@
9 Replies
8. Shell Programming and Scripting
I want to add/append the info in the following format to my.txt file.
20130702|abcd20130702.txt FN|SN|DOB
I tried the below script but it throws me some exceptions.
<#!/bin/sh
dt = date '+%y%m%d'members;
echo $dt+|+members+$dt;
/usr/bin/awk -f
BEGIN { FS="|"; OFS="|"; } { print... (6 Replies)
Discussion started by: harik1982
6 Replies
9. UNIX for Dummies Questions & Answers
Hi All
Is there a way to export every line into new txt file where by the title of each txt output are same as the line ?
I have this txt files containing names:
Kandra Vanhooser
Rhona Menefee
Reynaldo Hutt
Houston Rafferty
Charmaine Lord
Albertine Poucher
Juana Maes
Mitch Lobel... (2 Replies)
Discussion started by: Nexeu
2 Replies
10. UNIX for Beginners Questions & Answers
Good morning, i need your help please
I need to move a .TXT file greater or igual 355 MB and its correspondent .LST file in a non recursive way
The operating system is:
uname -a
SunOS server01c 5.10 Generic_144488-01 sun4u sparc SUNW,SPARC-Enterprise
For example:
rw-r--r-- 1 xptol ... (8 Replies)
Discussion started by: alexcol
8 Replies
BBE(1) BBE(1)
NAME
bbe - binary block editor
SYNOPSIS
bbe [options]...
DESCRIPTION
bbe is a sed-like editor for binary files. It performs binary transformations on the blocks of input stream.
OPTIONS
bbe accepts the following options:
-b, --block=BLOCK
Block definition.
-e, --expression=COMMAND
Add the COMMAND to the commands to be executed.
-f, --file=script-file
Add the contest of script-file to commands.
-o, --output=name
Write output to name instead of standard output.
-s, --suppress
Suppress normal output, print only block contents.
-?, --help
List all available options and their meanings.
-V, --version
Show version of program.
BLOCK can be defined as:
N:M Where N'th byte starts a M bytes long block (first byte is 0).
:M Block length in input stream is M.
/start/:M
String start starts M bytes long block.
/start/:/stop/
String start starts the block and block ends to string stop.
/start/:
String start starts the block and block will end at next occurence of start. Only the first start is included to the block.
:/stop/
Block starts at the beginning of input stream (or at the end of previous block) and ends at the next occurrence of stop. String stop
will be included to the block.
Special value '$' of M means the end of stream.
Default value for block is 0:$, meaning the whole input stream.
Both start and stop strings are included to block. Nonprintable characters can be escaped as
nn decimal
xnn hexadecimal
nnn octal
Character '' can be escaped as '\'. Escape codes 'a','',' ','
','v','f','
' and ';' can also be used.
Length (N and M) can be defined as decimal (n), hexadecimal (xn) or octal (0n) value.
COMMAND SYNOPSIS
bbe has two type of commands: block and byte commands, both are allways related to current block. That means that the input stream outside
of block remains untouched.
Block commands
D [n] Delete the n'th block. Without n, all found blocks are deleted from the output stream.
I string
Insert the string string before the block.
A string
Append the string string at the end of block.
J n Skip n blocks before executing commands after this command.
L n Leave all blocks unmodified starting from block number n. Affects only commands after this command.
N Before printing a block, the file name in which the block starts is printed.
F f Before printing a block, the input stream offset at the begining of the block is printed. f can be H, D or O for Hexadecimal, Deci-
mal or Octal format of offset.
B f Before printing a block, the block number is printed (first block == 1) f can be H, D or O for Hexadecimal, Decimal or Octal format
of block number.
> file Before printing a block, the contents of file file is printed.
< file After printing a block, the contents of file file is printed.
Byte commands
n in byte commands is offset from the beginning of current block (starts from zero).
r n string
Replace bytes starting at position n with string string.
i n string
Insert string starting at position n.
p format
The contents of block is printed in format defined by format. format can have any of the formats H, D, O, A and B for Hexadecimal,
Decimal, Octal, Asciii and Binary.
s/search/replace/
Replace all occurrences of search with replace.
y/source/dest/
Translate bytes in source to the corresponding bytes in dest. Source and dest must have equal length.
d n m|*
Delete m bytes starting from the offset n. If * is defined instead of m, then all bytes starting from n are deleted.
c from to
Convert bytes from format from to to. Currently supported formats are:
BCD Binary coded decimal
ASC Ascii
j n Commands after the j-command are ignored for first n bytes of the block.
l n Commands after the l-command are ignored from n'th byte of the block.
w file Write bytes from the current block to file file. Commands before w-command have effect to what will be written. %B or %nB in file
will be replaced by current block number. n in %nB is field length, leading zero in n causes the block number to be left padded with
zeroes.
& c Performs binary and with c.
| c Performs binary or with c.
^ c Performs binary xor with c.
~ Performs binary negation.
u n c All bytes from start of the block to offset n are replaced by c.
f n c All bytes starting from offset n to end of the block are replaced by c.
x Exchange the contents of nibbles (half an octet) of bytes.
Nonvisible characters in strings can be escaped same way as in block definition strings. Character '/' in s and y commands can be any visi-
ble character.
Note that the D, A, I, F, B, c, s, i, y, p, <, > and d commands cause the length of input and output streams to be different.
EXAMPLES
bbe -e "s/c:\temp\data1.txt/c:\temp\data2.txt/" file1
all occurences of "c: empdata1.txt" in file file1 are changed to "c: empdata2.txt"
bbe -b 0420:16 -e "r 4 x12x4a" file1
Two bytes starting at fifth byte of a 16 byte long block starting at offset 0420 (octal) in file1 are changed to hexadecimal values
12 and 4a.
bbe -b :16 -e "A x0a" file1
Newline is added after every block, block length is 16.
SEE ALSO
sed(1).
AUTHOR
Timo Savinen <tjsa@iki.fi>
2006-11-02 BBE(1)