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
# 1  
Old 08-21-2012
Search and swap multiple lines in file using Perl

Hi all,
I have a vcd file with a bunch of lines containing an array, like this
Code:
$var wire 1 b a[0] $end
$var wire 1 c a[1] $end
$var wire 1 d a[2] $end
$var wire 1 e a[3] $end
$var wire 1 f b[0] $end
$var wire 1 g b[1] $end
$var wire 1 h b[2] $end
$var wire 1 i b[3] $end

I want it like this:

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

In simple terms,it should find the line having a random x[0] and the highest x[n],then capture those lines and print them in the descending order.Hope i am making it clear
1) the variable could be a or b or sum or anything.I want to find lines that have individual scalar from 0 to n
2) I have written a perl script already but stuck not sure of the logic to do this line swap
My code so far:
------------------------------------------------------------------------------------
Code:
use warnings;

my $filename = "adder_copy2.dmp";
if (open(IN,"<$filename"))
{
 while (<IN>) 
 {
  if (/\[\d\]/)
   {   print "$_";
#Just prints out the lines that have array bits or vector bits so far
#open (OUT, ">$filename");
#print (OUT ;
  }

#       }
#else
#{ print "no word"; }
}

}
else
{
die("Cant open the file:$!");
}

close(IN);
close(OUT);

---------------------------------------------------------------------------
Thanks in advance.
Moderator's Comments:
Mod Comment Please view this code tag video for how to use code tags when posting code and data.

Last edited by Corona688; 08-21-2012 at 03:51 PM..
# 2  
Old 08-21-2012
Show the output you want, please.
# 3  
Old 08-21-2012
Expected output:

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

# 4  
Old 08-21-2012
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


[edit] I suppose you could just sort on the 5th column itself... sort -r -k 5,5
This User Gave Thanks to Corona688 For This Post:
# 5  
Old 08-21-2012
Thanks but I have many vcd files like this,so I wanna generalized perl script i could run on all these files instead of seraching for the lines.T
The pattern would be this in all these vcd files originally

Code:
$var ......   x[0] $end
"   "           x[1] $end
...
...
$var ....... x[n] $end

Expected pattern aft perl script:

Code:
$var ....... x[n] $end
....
...            x[1] $end
$var .......x [0] $end

P.S: I actually have to capture the numbers inside the array vectors,find out the highest number,then capture the whole line and push it to the top.
# 6  
Old 08-21-2012
What would that do which sort isn't doing already?

Why can you generalize with perl but not sort?

Right tool for the right job and all that. If I can do in one line what 30 can't, maybe you're using the wrong tool.
# 7  
Old 08-21-2012
I should have clarified in the beginning.My bad.
The vcd script has almost 1000 lines and not all lines have this
$var ..... $end pattern and not all of them vector bits,so the one using sort doesn't work in my case.
Let me show the first 40 odd lines of my vcd file
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 a 0 $end
$var wire 1 b a[0] $end
$var wire 1 c a[1] $end
$var wire 1 d a[2] $end
$var wire 1 e a[3] $end
$var wire 1 f b[0] $end
$var wire 1 g b[1] $end
$var wire 1 h b[2] $end
$var wire 1 i b[3] $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 o sum[0] $end
$var wire 1 p sum[1] $end
$var wire 1 q sum[2] $end
$var wire 1 r sum[3] $end
$var wire 1 s vdd $end
$scope module x_adder $end
$var wire 1 b a[0] $end
$var wire 1 c a[1] $end
$var wire 1 d a[2] $end
$var wire 1 e a[3] $end
$var wire 1 f b[0] $end
$var wire 1 g b[1] $end
$var wire 1 h b[2] $end
$var wire 1 i b[3] $end
$var wire 1 j cin $end
$var wire 1 k cout $end
$var wire 1 l gnd $end
$var wire 1 t n_0 $end
$var wire 1 u n_1 $end
$var wire 1 v n_10 $end
$var wire 1 w n_11 $end
$var wire 1 x n_13 $end
$var wire 1 y n_15 $end

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