Search and swap multiple lines in file using Perl


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Search and swap multiple lines in file using Perl
# 8  
Old 08-21-2012
Without seeing that, I don't think anyone could have helped you in perl, either...

What new output do you want for this new input?

Last edited by Corona688; 08-21-2012 at 04:50 PM..
# 9  
Old 08-21-2012
Quote:
Originally Posted by Corona688
Sort based on the first and third characters of the fifth column:
Code:
sort -r -k 5.1,5.1 -k 5.3,5.3 < input > output

That sort does not behave as intended. The -k5.1,5.1 restricted key never has any effect. While it restricts that key to the first character of the fifth field, since leading blanks aren't ignored by default, when using the default delimiter, 1-index is always going to be a whitespace character.

Code:
$ cat data
2 2 2 2 a 2
1 1 1 1 b 1

# 5.1 for both lines is a space, whole-line lexicographical comparison breaks tie
$ sort -k 5.1,5.1 data
1 1 1 1 b 1
2 2 2 2 a 2

# -b ignores leading blanks so that 5.1 yields 'a' and 'b', correct result.
$ sort -b -k 5.1,5.1 data 
2 2 2 2 a 2
1 1 1 1 b 1

Note that if the b modifier is used in a key definition, the modifier only applies to the component to which it is attached, and the -b option (if present) is ignored. Therefore it's possible that even within the same key definition, identical indices may refer to different positions. Given the three character field, ab (space character followed by two letters):
5.1b,5.1b ==> a (1 character)
5.1,5.1b ==> a (2 characters: space, a)
5.1b,5.1 ==> (0 characters: null result since the second component points to a character preceding the starting component)

Regards,
Alister

EDIT: The thread moved on while I was preparing this post. Perhaps it's no longer relevant to the solution, but the information may still be of use to someone. Smilie

Last edited by alister; 08-21-2012 at 10:20 PM..
These 2 Users Gave Thanks to alister For This Post:
# 10  
Old 08-21-2012
here is the expected output ... Note: Applies for all occurrences of the vector bits a[n..0],b[n..0] and sum[n..0].
Code:
$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 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 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

# 11  
Old 08-21-2012
What happened to this stuff?
Code:
$date
    Mon Aug 20 12:40:08 2012
$end
$version
    version 9.5-hbe-sp-mm
$end
$timescale
    1ps
$end

What happened to $scope module MAIN $end?

Will the output file work with these things missing?

If not, show a representative and complete output file. If you make us guess, we're going to guess wrong, which wastes your time and ours.
# 12  
Old 08-21-2012
The output file wont work missing those lines mentioned.
These are the first 40 odd lines of my 1000 line vcd script file. I am looking for the changes (shown in bold & italics) for any number of occurrences of vector bits a[n],b[n] and sum[n] found anywhere else in the code.If someone can show me how these changes could be done---even for the first instance of occurrence shown here--- i think i can take it from there...

Thanks in advance



Code:
$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 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 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 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

Note: Thanks Alister.It was useful neverthless.
# 13  
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..
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

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

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

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

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

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

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

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

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

9. 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
Login or Register to Ask a Question