Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Copy first row to the last in file

Tags:

linux

bash

awk

The purpose here is to copy the first row in the file to the last

Here the input file

335418.75,2392631.25,36091,38466,1
335418.75,2392643.75,36092,38466,1
335418.75,2392656.25,36093,38466,1
335418.75,2392668.75,36094,38466,1
335418.75,2392681.25,36095,38466,1
335418.75,2392693.75,36096,38466,1
335418.75,2392706.25,36097,38466,1
335418.75,2392718.75,36098,38466,1
335418.75,2392731.25,36099,38466,1

Using the following code i got the output desired. Is there other easy option?

  awk 'NR==1 {print}' FF1-1.csv > tmp1
  cat FF1-1.csv tmp1

Output desired

335418.75,2392631.25,36091,38466,1
335418.75,2392643.75,36092,38466,1
335418.75,2392656.25,36093,38466,1
335418.75,2392668.75,36094,38466,1
335418.75,2392681.25,36095,38466,1
335418.75,2392693.75,36096,38466,1
335418.75,2392706.25,36097,38466,1
335418.75,2392718.75,36098,38466,1
335418.75,2392731.25,36099,38466,1
335418.75,2392631.25,36091,38466,1

Thanks in advance.

like image 225
OXXO Avatar asked Dec 01 '22 14:12

OXXO


2 Answers

Save the line in a variable and print at end using the END block

$ seq 5 | awk 'NR==1{fl=$0} 1; END{print fl}'
1
2
3
4
5
1
like image 126
Sundeep Avatar answered Dec 04 '22 05:12

Sundeep


headcan produce the same output as your awk, so you can cat that instead.

You can use process substitution to avoid the temporary file.

cat FF1-1.csv <(head -n 1 FF1-1.csv)

As mentionned by Sundeep if process substitution isn't available you can simply cat the file then head it sequentially to obtain the same result, putting both in a subshell if you need to redirect the output :

(cat FF1-1.csv; head -n1 FF1-1.csv) > dest

Another alternative would be to pipe the output of head to cat and refer to it with - which for cat represents standard input :

head -1 FF1-1.csv | cat FF1-1.csv -
like image 38
Aaron Avatar answered Dec 04 '22 04:12

Aaron