Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to remove a character at the end of each line in UNIX

Tags:

unix

sed

awk

I would like to remove comma , at the end of each line in my file. How can I do it other than using substring function in awk?

Sample Input:

        SUPPLIER_PROC_ID BIGINT NOT NULL,         BTCH_NBR INTEGER NOT NULL,         RX_BTCH_SUPPLIER_SEQ_NBR INTEGER NOT NULL,         CORRN_ID INTEGER NOT NULL,         RX_CNT BYTEINT NOT NULL,         DATA_TYP_CD BYTEINT NOT NULL,         DATA_PD_CD BYTEINT NOT NULL,         CYC_DT DATE NOT NULL,         BASE_DT DATE NOT NULL,         DATA_LOAD_DT DATE NOT NULL,         DATA_DT DATE NOT NULL,         SUPPLIER_DATA_SRC_CD BYTEINT NOT NULL,         RX_CHNL_CD BYTEINT NOT NULL,         MP_IMS_ID INTEGER NOT NULL,         MP_LOC_ID NUMERIC(3,0),         MP_IMS_ID_ACTN_CD BYTEINT NOT NULL,         NPI_ID BIGINT, 
like image 970
Teja Avatar asked Feb 12 '13 20:02

Teja


People also ask

How do I remove the last character from an output?

The easiest way is to use the built-in substring() method of the String class. In order to remove the last character of a given String, we have to use two parameters: 0 as the starting index, and the index of the penultimate character.


1 Answers

Try doing this :

awk '{print substr($0, 1, length($0)-1)}' file.txt 

This is more generic than just removing the final comma but any last character

If you'd want to only remove the last comma with awk :

awk '{gsub(/,$/,""); print}' file.txt 
like image 139
Gilles Quenot Avatar answered Oct 10 '22 14:10

Gilles Quenot