Remove ',' fromove last line of the file

58 Views Asked by At

I have flat file with data as

cast ( (emp_ID)  as varchar(2000) ),
cast ( (emp_name)  as varchar(2000) ),

I want to remove the , from the last line in this file. The output should be:

 cast ( (emp_ID)  as varchar(2000) ),
 cast ( (emp_name)  as varchar(2000) )

I am using the following command:

tail -1 select_stmnt.txt | sed 's/,/''/g' >> select_stmnt.txt

What I get is:

  cast ( (emp_ID)  as varchar(2000) ), 
  cast ( (emp_name)  as varchar(2000) ),
  cast ( (emp_name)  as varchar(2000) )

Removal of the , is working fine, but instead of the modified line replacing the original, it is appended, which I don't want.

2

There are 2 best solutions below

7
Rahul Tripathi On

You can try this:

sed '$ s/,$//g' yourInputFile

The first dollar($) will be used to select the last line.

1
midori On

Use -i to edit files in-place:

sed -i '$s/,$//' select_stmnt.txt