Joana Carvalho
Joana Carvalho

Reputation: 99

How to repeat the first value of a column until the end of the file in bash?

I have several files like this:

-400.0 238.963 -6.598192e-07

-399.5 (blankspace) -3.041492e-07

-399.0 (blakspace) -1.541584e-07

-398.5 (blankspace) 7.299802e-08

-398.0 (blankspace) 1.602192e-07

...

All of them goes until row number 1601.

I want to print the value of the second column until the end of the file.

Like this:

-400.0 238.963 -6.598192e-07

-399.5 238.963 -3.041492e-07

-399.0 238.963 -1.541584e-07

-398.5 238.963 7.299802e-08

-398.0 238.963 1.602192e-07

...

(blankspace) means that it is a column but with no value from row 2 on.

I tried this:

for file in f do awk '(NR<=1601) {print $2} $f > test.dat done

But something is missing.

Can anyone help me?

Thanks

Upvotes: 0

Views: 74

Answers (1)

sjsam
sjsam

Reputation: 21965

awk 'NR==1{col2=$2}NR>1{$2=col2 OFS $2}1'  file

would do

Explanation :

  • NR==1{col2=$2} grabs the second column from the first record and stores it to col2
  • NR>1{$2=col2 OFS $2} - From record two onwards, concatenate the grabbed column col2 to the second field.
  • 1 is the most simple command to awk. It just prints the record.

Upvotes: 1

Related Questions