Swapping columns in specific blocks


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Swapping columns in specific blocks
# 1  
Old 12-31-2015
Swapping columns in specific blocks

Hi all,

I hope all you guys have a great new year!

I am trying to swap 2 columns in a specific block of a file. The file format is:

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


I am trying to
1) replace 5th column with 2nd column.
1-1) If a letter in 5th column is "r", this would be replaced with "v". If a letter in 5th column is "f", this would be replaced with "^".
2) insert two double quotation marks in 1st column.
3) replace 3rd column with ;
4) replace 4th column with //

This should only be done in blocks which are defined starting and ending lines - "input external delay" , "data arrival time".

Additionally, I want to change texts before/after the block with predefined texts. Before the block, I would like to add $path {
// from: in11
// to: out432
$name "test_1" ;
$cycle 1 ;
$slack -0.130978 ;
$transition {

After the block, I would like to add }
}


My expected output should be:
Code:
$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)
  }
}

Currently, I can swap columns, but I don't know how to use them only in the block. My code is:
Code:
awk ' { c1 = $1; c2 = $2; $1 = " \" "; $2 = c1; $3 = " \" "; $4 = $5; $5 = " ; // "; $6 = c2; print; } ' file1.txt


Any help you could give me will be greatly appreciated.
# 2  
Old 12-31-2015
Would this do?
Code:
awk -v DQ="\"" '
/dat.*ime/      {IN = 0
                 print "}" RS "}"
                 next
                }
/inp.*lay/      {IN = 1
                 print  "$path {"                       RS \
                        "// from: in11"                 RS \
                        "// to: out432"                 RS \
                        "$name \"test_" ++CNT "\" ;"    RS \
                        "$cycle 1 ;"                    RS \
                        "$slack -0.130978 ;"            RS \
                        "$transition {"
                 next
                }

IN              {$1 = DQ $1 DQ
                 TM = $2
                 $2 = $5 == "r"?"v":"^"
                 $3 = ";"
                 $4 = "//"
                 $5 = TM
                 print
                }

' OFS="\t" file
$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)
}
}

# 3  
Old 12-31-2015
Hello jypark22,

Following may help you in same.
Code:
awk -vs1="\$path" -vs2="// from: in11" -vs3="// to: out432" -vs4="\$name "test_" ;" -vs5="\$cycle 1 ;" -vs6="\$slack -0.130978 ;" -vs7="\$transition {" -vs8="\"" '{VAR=s1 "\n" s2 "\n" s3 "\n" s4  "\n" s5 "\n" s6 "\n" s7};/ Point/{getline;getline;getline;while($0 !~ /Endpoint/){if($0 ~ /data arrival time/ || $0 ~ /Path is unconstrained/ || $0 ~ /^$/){;next};$1=s8 $1 s8;if($5 ~ /r/){$5="v"};if($5 ~ /f/){$5="^"};sub($3,";");sub($4,"//");E=$NF;$NF=$2;$2=E;S=S?S ORS $0:$0;getline}};/ Endpoint/{if(S){d++;sub(/test_/,"&" d,VAR);print VAR ORS S ORS " }" ORS "}" ORS;S=""}} END{d++;sub(/test_/,"&" d,VAR);print VAR ORS S ORS " }" ORS "}"}'   Input_file

Output will be as follows.
Code:
$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)
 }
}

EDIT: Adding a non one-liner form for same solution too now.
Code:
awk -vs1="\$path" -vs2="// from: in11" -vs3="// to: out432" -vs4="\$name "test_" ;" -vs5="\$cycle 1 ;" -vs6="\$slack -0.130978 ;" -vs7="\$transition {" -vs8="\"" '{
VAR=s1 "\n" s2 "\n" s3 "\n" s4  "\n" s5 "\n" s6 "\n" s7};
/ Point/{
                getline;
                getline;
                getline;
                while($0 !~ /Endpoint/){
                                        if($0 ~ /data arrival time/ || $0 ~ /Path is unconstrained/ || $0 ~ /^$/){;
                                                                                                                        next
                                                                                                                 }
                ;$1=s8 $1 s8;
                if($5 ~ /r/)           {
                                        $5="v"
                                       };
                if($5 ~ /f/)           {
                                        $5="^"
                                       };
                sub($3,";");
                sub($4,"//");
                E=$NF;
                $NF=$2;
                $2=E;
                S=S?S ORS $0:$0;
                getline
                                       }
        };
/ Endpoint/{
                if(S)                  {
                                        d++;
                                        sub(/test_/,"&" d,VAR);
                                        print VAR ORS S ORS " }" ORS "}" ORS;
                                        S=""
                                       }
           }
END        {
                d++;
                sub(/test_/,"&" d,VAR);
                print VAR ORS S ORS " }" ORS "}"
           }'  Input_file

Output will be as follows.
Code:
$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)
 }
}

Thanks,
R. Singh

Last edited by RavinderSingh13; 12-31-2015 at 04:25 PM.. Reason: Added a non one-liner form for solution now.
# 4  
Old 12-31-2015
Here's a Perl version with the same indentation and extra space between blocks output, as hinted in the example posted.
Code:
perl -nle '
BEGIN{    
    %r=qw(f ^ r v);
    $addp1=q($path {
  // from: in11
  // to: out432
  $name "test_);
    $addp2=q(" ;
  $cycle 1 ;
  $slack -0.130978 ;
  $transition {);
    $addend=q(  }
});
    $start = "input external delay";
    $end = "data arrival time";
}

/$start/ and print $addp1 . ++$i . $addp2;
if(/$start/../$end/){
    print if s|^\s+([^\s]+)\s+(\(.*\)).*(\w)$|  "$1" $r{$3} ; // $2|;
};
/$end/ and print "$addend\n";
' jypark22.file

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


Last edited by Aia; 12-31-2015 at 06:41 PM.. Reason: add "hinted"
# 5  
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)
  }
}

$ 
$

# 6  
Old 01-03-2016
Thank you for all your help. RudiC's code works for me. Happy new year!

Best,

Jaeyoung
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

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

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

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

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

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

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

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

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

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

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