Sponsored Content
Top Forums Shell Programming and Scripting Search and swap multiple lines in file using Perl Post 302690057 by Corona688 on Wednesday 22nd of August 2012 12:02:05 PM
Old 08-22-2012
Now that we can finally tell what you need:

Code:
$ cat wire.pl

#!/usr/bin/perl

# Read all lines into @lines
my @lines=<STDIN>, @order, $n, $m, @l;

for($n=0; $n<=$#lines; $n++)
{
        if($lines[$n] =~ /[$]var[ \t]*wire/ &&
                (index($lines[$n], "[")>=0))
        {
                @order=();

                # Generate a list of lines like
                #       b       index   linenum
                for($m=$n;
                        $lines[$m] =~ /[$]var[ \t]*wire/ &&
                                (index($lines[$m], "[") >= 0) &&
                                ($m <= $#lines);
                        $m++)
                {
                        $lines[$m] =~ /([a-zA-Z]+)\[([0-9]+)\]/; # Extract name and index
                        $order[$m-$n]=sprintf("%s\t%06d\t%06d", $1, $2, $m); # Store in @order
                }

                # Sort the list in reverse order ( direct from perldoc -f sort )
                @sort=sort  {$b cmp $a} @order;

                # Skip these lines next loop, they'll be printed already
                $n=$m-1;

                # Iterate over our sorted list, extract line numbers,
                # print lines
                for($m=0; $sort[$m]; $m++)
                {
                        @l=split(/\t/, $sort[$m]);
                        print @lines[@l[2]+0];
                }
        }
        else    {       print $lines[$n];      }
}

exit 0;

$ ./wire.pl < data.wire

$date
Mon Aug 20 12:40:08 2012
$end
$version
version 9.5-hbe-sp-mm
$end
$timescale
1ps
$end

$scope module MAIN $end
$var  wire 1 i b[3] $end
$var  wire 1 h b[2] $end
$var  wire 1 g b[1] $end
$var  wire 1 f b[0] $end
$var  wire 1 e a[3] $end
$var  wire 1 d a[2] $end
$var  wire 1 c a[1] $end
$var  wire 1 b a[0] $end
$var wire 1 j cin $end
$var wire 1 k cout $end
$var wire 1 l gnd $end
$var wire 1 m gnd! $end
$var wire 1 n ground $end
$var wire 1 r sum[3] $end
$var wire 1 q sum[2] $end
$var wire 1 p sum[1] $end
$var wire 1 o sum[0] $end
$var wire 1 s vdd $end
$scope module x_adder $end
.......
....
#any such occurrences anywhere in the script

$


Last edited by Corona688; 08-22-2012 at 01:09 PM..
 

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Perl - How to search a text file with multiple patterns?

Good day, great gurus, I'm new to Perl, and programming in general. I'm trying to retrieve a column of data from my text file which spans a non-specific number of lines. So I did a regexp that will pick out the columns. However,my pattern would vary. I tried using a foreach loop unsuccessfully.... (2 Replies)
Discussion started by: Sp3ck
2 Replies

2. Shell Programming and Scripting

Pattern search in multiple lines

Hi, I have to search those statements from the file which starts from "shanky"(only shanky, shanky09 or 09shanky is not allowed) and ends with ");". These two string can be in a same line or different line. And also i have to negate those lines which starts with #. Can any one please give me... (2 Replies)
Discussion started by: shanky09
2 Replies

3. Shell Programming and Scripting

Search for multiple lines in large file

Hi, I have a requirement to search for a string in a large log file along with few lines before and after the the string. The following script was sufficient to search such an entry. STRING_TO_GREP="$1" FILE_TO_GREP="$2" NUMBER_OF_LINES_BEFORE=$3 NUMBER_OF_LINES_AFTER=$4 for i in `grep... (3 Replies)
Discussion started by: praveen123
3 Replies

4. Shell Programming and Scripting

Using Perl to Merge Multiple Lines in a File

I've hunted and hunted but nothing seems to apply to what I need. Any help will be much appreciated! My input file looks like (Unix): marker,allele1,allele2 RS1002244,1,1 RS1002244,1,3 RS1002244,3,3 RS1003719,2,2 RS1003719,2,4 RS1003719,4,4 Most markers are listed 3 times but a few... (2 Replies)
Discussion started by: Peggy White
2 Replies

5. Shell Programming and Scripting

Perl to Search through next lines

I have log file that I need to extract time difference occurance when two events happend, between first occurance of TV and when W_NO happend. also read the value=, below example...I can only read the next line but not able to seach all the next lines untill i see W_NO.. Thanks for your help.... (6 Replies)
Discussion started by: bataf
6 Replies

6. Shell Programming and Scripting

search and replace, when found, delete multiple lines, add new set of lines?

hey guys, I tried searching but most 'search and replace' questions are related to one liners. Say I have a file to be replaced that has the following: $ cat testing.txt TESTING AAA BBB CCC DDD EEE FFF GGG HHH ENDTESTING This is the input file: (3 Replies)
Discussion started by: DeuceLee
3 Replies

7. Shell Programming and Scripting

Combine multiple unique lines from event log text file into one line, use PERL or AWK?

I can't decide if I should use AWK or PERL after pouring over these forums for hours today I decided I'd post something and see if I couldn't get some advice. I've got a text file full of hundreds of events in this format: Record Number : 1 Records in Seq : ... (3 Replies)
Discussion started by: Mayday22
3 Replies

8. Shell Programming and Scripting

How to search multiple patterns and remove lines from a file?

Hi, I have a file content as below. Table : PAYR Displayed fields: 15 of 15 Fixed columns: 4 List width 0999... (4 Replies)
Discussion started by: shirdi
4 Replies

9. Shell Programming and Scripting

Removing multiple lines from input file, if multiple lines match a pattern.

GM, I have an issue at work, which requires a simple solution. But, after multiple attempts, I have not been able to hit on the code needed. I am assuming that sed, awk or even perl could do what I need. I have an application that adds extra blank page feeds, for multiple reports, when... (7 Replies)
Discussion started by: jxfish2
7 Replies
insdelln(3XCURSES)					  X/Open Curses Library Functions					insdelln(3XCURSES)

NAME
insdelln, winsdelln - insert/delete lines to/from the window SYNOPSIS
cc [ flag... ] file... -I /usr/xpg4/include -L /usr/xpg4/lib -R /usr/xpg4/lib -lcurses [ library... ] c89 [ flag... ] file... -lcurses [ library... ] #include <curses.h> int insdelln(int n); int winsdelln(WINDOW *win, int n); PARAMETERS
n Is the number of lines to insert or delete (positive n inserts; negative n deletes). win Is a pointer to the window in which to insert or delete a line. DESCRIPTION
The insdelln() and winsdelln() functions insert or delete blank lines in stdscr or win, respectively. When n is positive, n lines are added before the current line and the bottom n lines are lost; when n is negative, n lines are deleted starting with the current line, the remaining lines are moved up, and the bottom n lines are cleared. The position of the cursor does not change. RETURN VALUES
On success, these functions return OK. Otherwise, they return ERR. ERRORS
None. ATTRIBUTES
See attributes(5) for descriptions of the following attributes: +-----------------------------+-----------------------------+ | ATTRIBUTE TYPE | ATTRIBUTE VALUE | +-----------------------------+-----------------------------+ |Interface Stability |Standard | +-----------------------------+-----------------------------+ |MT-Level |Unsafe | +-----------------------------+-----------------------------+ SEE ALSO
deleteln(3XCURSES), insertln(3XCURSES), libcurses(3XCURSES), attributes(5), standards(5) SunOS 5.10 5 Jun 2002 insdelln(3XCURSES)
All times are GMT -4. The time now is 05:26 PM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy