2

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.

5
  • 1
    Should be easy to do in awk. Save the first field in a variable. If the current row only has 2 fields, print the variable followed by the fields in the row. Commented Mar 6, 2018 at 5:48
  • Welcome to Stack Overflow! StackOverflow expects you to try to solve your own problem first. Please update your question to show what you have already tried in a minimal reproducible example. For further information, please see How to Ask, and take the tour :) Commented Mar 6, 2018 at 5:48
  • Does the number of spaces between the column matters? Commented Mar 6, 2018 at 5:50
  • 3
    the number of spaces doesn't matter Commented Mar 6, 2018 at 5:56
  • Stack Overflow is not a code writing service. Please show your code. Since Stack Overflow hides the Close reason from you: Questions seeking debugging help ("why isn't this code working?") must include the desired behavior, a specific problem or error and the shortest code necessary to reproduce it in the question itself. Questions without a clear problem statement are not useful to other readers. See: How to create a Minimal, Complete, and Verifiable example. Commented Mar 6, 2018 at 11:44

3 Answers 3

5

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
Sign up to request clarification or add additional context in comments.

Comments

0

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

Comments

0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.