Darshan M Darshan M - 3 months ago 8
Bash Question

Rename the footer content in the data file(.csv file) from shell script

I get a .csv file, in which the last line has the footer value
For Example:

896312,3251040212,3251040212,P5,7052016,7072016,0,$0.00,N,1
896312,3251067611,3251067611,P5,7052016,7072016,0,$0.00,N,1
**WOR_FOOTER_20160705100007_000000010_WOR_896312_20160705_100007.csv**--This is footer.


I need to replace the footer with
VWOR_FOOTER_20160705100007_000000010_VWOR896312_20160705_100007.csv


in unix. Please help. Thanks

Answer
sed -i.bak '$s/WOR/VWOR/g' file

should do it.

Explanation

  • Here $ picks the last line.
  • s/WOR/VWOR/g does the global substitution.
  • -i stands for inplace edit which means directly modify the file. A backup with .bak will be auto-created.

Edit

If your shell doesn't support inplace edit do

sed '$s/WOR/VWOR/g' file >tempfile && mv tempfile file