1

I have a text file in the following format:-

$DATA1 SOURCE='HSPICE' VERSION='I-2013.12-SP2-1 32-BIT'  
.TITLE 'cmos inverter transfer characteristics'  
index            t_r              t_f              t_rf              
                 t_fr             t_rr             t_ff              
                 temper           alter#            
1                 1.361e-11        1.177e-11        8.807e-12        
                  9.063e-12        2.002e-08        2.002e-08        
                  2.500e+01       1                 
2                 1.339e-11        1.178e-11        8.805e-12        
                  8.867e-12        2.002e-08        2.002e-08        
                  2.500e+01       1                 
3                 1.334e-11        1.177e-11        8.811e-12        
                  8.824e-12        2.002e-08        2.002e-08        
                  2.500e+01       1   

.  
.  
.  
1000 1.339e-11        1.178e-11        8.805e-12        
                  8.867e-12        2.002e-08        2.002e-08        
                  2.500e+01       1   

I wish to remove first 5 lines and extract the 9 numerical values from set of three lines, all in 9 different output files using awk command.
example of output file is:-
File1.txt

1  
2  
3  
.  
.  
1000  

file2.txt

1.361e-11   
1.339e-11   
1.334e-11  
.  
.  
.  
1.339e-11

same for all the nine elements in a set of three lines.

3 Answers 3

1

Here is a pure awk solution:

awk '
  NR<=5 { next }
  { for(i=1; i<=NF; i++) print $i > "File"++c}
  NF==2 { c=0 }
'

It skippes five lines of header, prints the fields on the remaining data in files with names defined by a counter, which is reset on lines with only two fields.

0

Can be done in bare awk, but first what comes to my mind is sed+awk combo:

sed '1,5d;s/^\([[:digit:]]\)/\n\1/' file | 
awk 'BEGIN{RS="\n\n"}{for(i=1;i<=NF;i++){print $i > "file"i".txt"}}'

The point is to add (with sed) new line to all lines which start from digit, and after that redefine record separator in awk.

0

Here's another sed plus tr plus printf:

tr -s '[:space:]' \\n <infile |
sed "/^[0-9]/,${$(
    printf '\nw file%b\nn' \
           1 2 3 4 5 6 7 8 '9\n}\c'
)"

I ran it on my clipboard as piped out by xsel in /tmp after copying your example to the buffer and afterwards did...

head -n3 /tmp/file?

...which printed...

==> /tmp/file1 <==
1
2
3

==> /tmp/file2 <==
1.361e-11
1.339e-11
1.334e-11

==> /tmp/file3 <==
1.177e-11
1.178e-11
1.177e-11

==> /tmp/file4 <==
8.807e-12
8.805e-12
8.811e-12

==> /tmp/file5 <==
9.063e-12
8.867e-12
8.824e-12

==> /tmp/file6 <==
2.002e-08
2.002e-08
2.002e-08

==> /tmp/file7 <==
2.002e-08
2.002e-08
2.002e-08

==> /tmp/file8 <==
2.500e+01
2.500e+01
2.500e+01

==> /tmp/file9 <==
1
1
1

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .