5

I have file of following format:

 1.0
 2.0
 3.0
 4.0
 5.0

Is it possible to copy value in first line 1.0 to the beginning of every line in awk? Like this:

 1.0 1.0
 1.0 2.0
 1.0 3.0
 1.0 4.0
 1.0 5.0
  • With GNU sed: sed -n '1{h;G;s/\n/ /p}; :j;g;N;s/\n/ /p;bj' file In this case, awk is more transparent. – Cyrus Dec 25 '17 at 11:31
8
awk 'NR==1 {f=$1} {print f,$1}' file

Output:

1.0 1.0
1.0 2.0
1.0 3.0
1.0 4.0
1.0 5.0

If current line number (NR) is 1 then save column 1 ($1) to variable f. For every line print content of variable f and content of column 1.

  • i though like you ! lol but he want different thing. :D i edited my answer to achieve his target. – αԋɱҽԃ αмєяιcαη Dec 25 '17 at 7:56
1

I have done this by sed command working as expected


k=`sed -n 1p file`;sed "s/^/$k /g" file

output


1.0 1.0
1.0 2.0
1.0 3.0
1.0 4.0
1.0 5.0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.