midi
midi

Reputation: 39

How to duplicate string until another string in bash

I have a text file that is structure like below :

 293.0                             2305.3     1508.0                         
                                   2466.3     1493.0                         
                                   2669.5     1578.6                         
                                   3497.2     1768.9                         
                                   4265.5     2092.4                         
                                   5940.8     2558.6                         
                                   7308.7     3015.4                         
                                   9377.7     3814.6                         
 295.0                             2331.4     1498.1                         
                                   3617.0     1893.2           

I'm still new in Linux, is there anyway for it to be output as desire like an example below :

 293.0                             2305.3     1508.0                         
 293.0                             2466.3     1493.0                         
 293.0                             2669.5     1578.6                         
 293.0                             3497.2     1768.9                         
 293.0                             4265.5     2092.4                         
 293.0                             5940.8     2558.6                         
 293.0                             7308.7     3015.4                         
 293.0                             9377.7     3814.6                         
 295.0                             2331.4     1498.1                         
 295.0                             3617.0     1893.2           

So basically, I want it to duplicate until it meets another variable.

Upvotes: 2

Views: 78

Answers (3)

Trocoldite
Trocoldite

Reputation: 36

You can also use pure bash style thanks to its array capacities:

$ while read -a f; do \
if [ ! -z ${f[2]} ]; then \
c=${f[0]}; echo ${f[@]}; \
else \
echo $c ${f[@]}; \
fi; \
done <file

I have cut lines for reading purpose.

Line by line (while read), I save fields in an array "f". If the third field is not null (if [ ! -z...]), I save first column and print all fields, else, I print c and the 2 other fields.

Upvotes: 0

Allan
Allan

Reputation: 12438

You can also use the following command:

$ sed 's/ \+/|/g' dupl.in | awk 'BEGIN{FS="|"}{if($1){buff=$1; printf "%.1f ", buff;} else {printf "%.1f ", buff;} printf "%1.f %1.f \n",$2, $3}'                                                                                                                                   
293.0 2305 1508 
293.0 2466 1493 
293.0 2670 1579 
293.0 3497 1769 
293.0 4266 2092 
293.0 5941 2559 
293.0 7309 3015 
293.0 9378 3815 
295.0 2331 1498 
295.0 3617 1893

Upvotes: 0

Cyrus
Cyrus

Reputation: 88583

With Barmar's idea:

If row contains three columns, save first column to a variable and print all three columns. If row contains two columns, print variable and column one and two:

awk 'NF==3{c=$1; print $1,$2,$3}; NF==2{print c,$1,$2}' file

Output:

293.0 2305.3 1508.0
293.0 2466.3 1493.0
293.0 2669.5 1578.6
293.0 3497.2 1768.9
293.0 4265.5 2092.4
293.0 5940.8 2558.6
293.0 7308.7 3015.4
293.0 9377.7 3814.6
295.0 2331.4 1498.1
295.0 3617.0 1893.2

Upvotes: 5

Related Questions