Sponsored Content
Top Forums Shell Programming and Scripting Swapping columns in specific blocks Post 302963542 by durden_tyler on Friday 1st of January 2016 07:05:56 PM
Old 01-01-2016
Here's a Python version:

Code:
$ 
$ cat input.txt

Startpoint: in11 (input port)
Endpoint: out421 (output port)
Path Group: (none)
Path Type: max

Point                                    Incr       Path
---------------------------------------------------------------
input external delay                     0.00       0.00 r
in11 (in)                                0.00       0.00 r
Ckt432/A[7] (TopLevel432b)               0.00       0.00 r
Ckt432/M1/A[7] (PriorityA)               0.00       0.00 r
Ckt432/M1/U7/Y (INVX1_RVT)               0.03       0.03 f
Ckt432/M5/U12/Y (OA21X1_RVT)             0.10       2.44 r
Ckt432/M5/Chan[3] (DecodeChan)           0.00       2.44 r
Ckt432/Chan[3] (TopLevel432b)            0.00       2.44 r
out421 (out)                             0.00       2.44 r
data arrival time                                   2.44
---------------------------------------------------------------
(Path is unconstrained)


Startpoint: in37 (input port)
Endpoint: out421 (output port)
Path Group: (none)
Path Type: max

Point                                    Incr       Path
---------------------------------------------------------------
input external delay                     0.00       0.00 r
in37 (in)                                0.00       0.00 r
Ckt432/A[5] (TopLevel432b)               0.00       0.00 r
Ckt432/M1/A[5] (PriorityA)               0.00       0.00 r
Ckt432/M1/U8/Y (INVX1_RVT)               0.03       0.03 f
Ckt432/M1/U13/Y (NAND2X0_RVT)            0.06       0.10 r
Ckt432/M5/U12/Y (OA21X1_RVT)             0.10       2.44 r
Ckt432/M5/Chan[3] (DecodeChan)           0.00       2.44 r
Ckt432/Chan[3] (TopLevel432b)            0.00       2.44 r
out421 (out)                             0.00       2.44 r
data arrival time                                   2.44
---------------------------------------------------------------
(Path is unconstrained)

$ 
$ cat -n process_input.py
     1	#!/usr/bin/env python
     2	# Usage: python process_input.py <input_file_name>
     3	
     4	from sys import argv
     5	input_file = argv[1]
     6	start_template = '''$path {
     7	  // from: in11
     8	  // to: out432
     9	  $name "test_#" ;
    10	  $cycle 1 ;
    11	  $slack -0.130978 ;
    12	  $transition {'''
    13	
    14	end_template = '''  }
    15	}
    16	'''
    17	
    18	inside_block = 0
    19	iter = 0
    20	chunk = []
    21	for line in open(input_file, 'rt'):
    22	    if line.startswith('input external delay'):
    23	        inside_block = 1
    24	        iter += 1
    25	        chunk.append(start_template.replace('#', str(iter)))
    26	    elif line.startswith('data arrival time'):
    27	        chunk.append(end_template)
    28	        for chunk_line in chunk:
    29	            print chunk_line
    30	        chunk = []
    31	        inside_block = 0
    32	    elif inside_block:
    33	        line = line.replace('\n', '')
    34	        tokens = line.split()
    35	        # 1-1) If a letter in 5th column is "r", this would be replaced with "v".
    36	        # If a letter in 5th column is "f", this would be replaced with "^".
    37	        if tokens[4] == 'r':
    38	            tokens[4] = 'v'
    39	        elif tokens[4] == 'f':
    40	            tokens[4] = '^'
    41	
    42	        # 1) replace 5th column with 2nd column. (Swap 2nd and 5th columns.)
    43	        temp = tokens[1]
    44	        tokens[1] = tokens[4]
    45	        tokens[4] = temp
    46	
    47	        # 2) insert two double quotation marks in 1st column. 
    48	        # 3) replace 3rd column with ;
    49	        # 4) replace 4th column with // 
    50	        tokens[0] = '"' + tokens[0] + '"'
    51	        tokens[2] = ';'
    52	        tokens[3] = '//'
    53	
    54	        # Append the transformed stringt to array "chunk"
    55	        transformed_str = ' '
    56	        for item in tokens:
    57	            transformed_str += ' ' + item
    58	        chunk.append(transformed_str)
    59	
$ 
$ python process_input.py input.txt
$path {
  // from: in11
  // to: out432
  $name "test_1" ;
  $cycle 1 ;
  $slack -0.130978 ;
  $transition {
  "in11" v ; // (in)
  "Ckt432/A[7]" v ; // (TopLevel432b)
  "Ckt432/M1/A[7]" v ; // (PriorityA)
  "Ckt432/M1/U7/Y" ^ ; // (INVX1_RVT)
  "Ckt432/M5/U12/Y" v ; // (OA21X1_RVT)
  "Ckt432/M5/Chan[3]" v ; // (DecodeChan)
  "Ckt432/Chan[3]" v ; // (TopLevel432b)
  "out421" v ; // (out)
  }
}

$path {
  // from: in11
  // to: out432
  $name "test_2" ;
  $cycle 1 ;
  $slack -0.130978 ;
  $transition {
  "in37" v ; // (in)
  "Ckt432/A[5]" v ; // (TopLevel432b)
  "Ckt432/M1/A[5]" v ; // (PriorityA)
  "Ckt432/M1/U8/Y" ^ ; // (INVX1_RVT)
  "Ckt432/M1/U13/Y" v ; // (NAND2X0_RVT)
  "Ckt432/M5/U12/Y" v ; // (OA21X1_RVT)
  "Ckt432/M5/Chan[3]" v ; // (DecodeChan)
  "Ckt432/Chan[3]" v ; // (TopLevel432b)
  "out421" v ; // (out)
  }
}

$ 
$

 

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Mean of the specific columns

I have a input file that has some common values in 1st,2nd and 3rd columns. 4th and 5th are different. Now I would like to print the mean of the fourth column of similar values in 1st.2nd and 3rd columns along with all the values in 5th column. input NM_0 1.22 CR5 0.4 n_21663... (10 Replies)
Discussion started by: repinementer
10 Replies

2. UNIX for Dummies Questions & Answers

Convert 512-blocks to 4k blocks

I'm Unix. I'm looking at "df" on Unix now and below is an example. It's lists the filesystems out in 512-blocks, I need this in 4k blocks. Is there a way to do this in Unix or do I manually convert and how? So for container 1 there is 7,340,032 in size in 512-blocks. What would the 4k block be... (2 Replies)
Discussion started by: rockycj
2 Replies

3. Shell Programming and Scripting

how to split this file into blocks and then send these blocks as input to the tool called Yices?

Hello, I have a file like this: FILE.TXT: (define argc :: int) (assert ( > argc 1)) (assert ( = argc 1)) <check> # (define c :: float) (assert ( > c 0)) (assert ( = c 0)) <check> # now, i want to separate each block('#' is the delimeter), make them separate files, and then send them as... (5 Replies)
Discussion started by: paramad
5 Replies

4. Shell Programming and Scripting

Help swapping columns with AWK

Hi! Im trying to swap 2 columns in a file.The file format is: 'ColumnA','ColumnB' 'A1','A2' 'B1','B2' 'C1','C2' I tried to solve this using AWK, when I run this command: awk 'BEGIN {FS=OFS=","} {temp=$1; $1=$2; $2=temp} {print}' InFile.csv >> Outfile.csv What I get is this: ... (5 Replies)
Discussion started by: RedSpyder
5 Replies

5. UNIX for Dummies Questions & Answers

Display blocks containing specific pattern

Hi, I have a file containing multiple entries. Each block starts with <BEGIN and ends with <END. Sample data is given below <BEGIN IMSI=095001202630; MSISDN=00145132916; DEFCALL=TS11; CURRENTNAM=BOTH; CAT=COMMON; TBS=TS11&TS12&TS21&TS22; CARDTYPE=SIM; ... (2 Replies)
Discussion started by: krabu
2 Replies

6. UNIX for Dummies Questions & Answers

Swapping the columns of a text file for a subset of rows

Hi, I'd like to swap the columns 1 and 2 of a space-delimited text file but only for the first 1000 rows. How do I go about doing that? Thanks! (1 Reply)
Discussion started by: evelibertine
1 Replies

7. Shell Programming and Scripting

Can't figure out how to find specific characters in specific columns

I am trying to find a specific set of characters in a long file. I only want to find the characters in column 265 for 4 bytes. Is there a search for that? I tried cut but couldn't get it to work. Ex. I want to find '9999' in column 265 for 4 bytes. If it is in there, I want it to print... (12 Replies)
Discussion started by: Drenhead
12 Replies

8. Shell Programming and Scripting

Row blocks to column blocks

Hello, Searched for a while and found some "line-to-column" script. My case is similar but with multiple fields each row: S02 Length Per S02 7043 3.864 S02 54477 29.89 S02 104841 57.52 S03 Length Per S03 1150 0.835 S03 1321 0.96 S03 ... (9 Replies)
Discussion started by: yifangt
9 Replies

9. UNIX for Dummies Questions & Answers

Printing lines with specific strings at specific columns

Hi I have a file which is tab-delimited. Now, I'd like to print the lines which have "chr6" string in both first and second columns. Could anybody help? (3 Replies)
Discussion started by: a_bahreini
3 Replies

10. Shell Programming and Scripting

Modify blocks of text by printing missing columns

Hi Experts, I have a problem where I want to print missing columns (3,4) within a block of text. Each block is separated by "###". Some rows have missing column 3 and 4 which should be same as the previous value in column 3 and 4. The file is space delimited. For example: INPUT ###... (5 Replies)
Discussion started by: mira
5 Replies
All times are GMT -4. The time now is 05:49 PM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy