Remove trailing tilde("~")

Hi,

I have file where i need to remove only the leading and trailing tilde(~) charaters from file. Please seebelow for sample file:

~~OLD PRODUCT RECORD COUNT         ~ 15476~100.00%~~~~~~~~~~~~~~~~~~~~~~~~
~~NEW PRODUCT RECORD COUNT         ~ 15609~  0.85%~~~~~~~~~~~~~~~~~~~~~~~~
~~NEW PRODUCT RECORDS INSERTED     ~   133~  0.85%~~~~~~~~~~~~~~~~~~~~~~~~
~~OLD PRODUCT RECORDS DELETED      ~     0~  0.00%~~~~~~~~~~~~~~~~~~~~~~~~
~~OLD PRODUCT RECORDS UNCHANGED    ~ 15249~ 98.53%~~~~~~~~~~~~~~~~~~~~~~~~
~~OLD PRODUCT RECORDS UPDATED      ~   227~  1.46%~~~~~~~~~~~~~~~~~~~~~~~~
~~CHANGES TO PRODUCT NAME          ~     0~  0.00%~~~~~~~~~~~~~~~~~~~~~~~~
~~CHANGES TO PRODUCT TYPE          ~     0~  0.00%~~~~~~~~~~~~~~~~~~~~~~~~
~~CHANGES TO PRODUCT LEVEL         ~     0~  0.00%~~~~~~~~~~~~~~~~~~~~~~~~

Thanks in advance,

Arun

Try:

sed 's/^~*//; s/~*$//' file
1 Like

It doesn't work . Please refer below for results:

[B955991_ABBVIE_HCV_DEV@cdtsorl239p B955991_ABBVIEHPC_DAS_DEV]$ head CHGSUM.CSV | sed 's/^~*//; s/~*$//'
                              ~~~ COUNT   ~PERCENT~~~~~~~~~~~~~~~~~~~~~~~~
OLD PRODUCT RECORD COUNT         ~ 15476~100.00%~~~~~~~~~~~~~~~~~~~~~~~~
NEW PRODUCT RECORD COUNT         ~ 15609~  0.85%~~~~~~~~~~~~~~~~~~~~~~~~
NEW PRODUCT RECORDS INSERTED     ~   133~  0.85%~~~~~~~~~~~~~~~~~~~~~~~~
OLD PRODUCT RECORDS DELETED      ~     0~  0.00%~~~~~~~~~~~~~~~~~~~~~~~~
OLD PRODUCT RECORDS UNCHANGED    ~ 15249~ 98.53%~~~~~~~~~~~~~~~~~~~~~~~~
OLD PRODUCT RECORDS UPDATED      ~   227~  1.46%~~~~~~~~~~~~~~~~~~~~~~~~
CHANGES TO PRODUCT NAME          ~     0~  0.00%~~~~~~~~~~~~~~~~~~~~~~~~
CHANGES TO PRODUCT TYPE          ~     0~  0.00%~~~~~~~~~~~~~~~~~~~~~~~~
CHANGES TO PRODUCT LEVEL         ~     0~  0.00%~~~~~~~~~~~~~~~~~~~~~~~~

Is your file in DOS format? Try converting it to UNIX format first:

tr -d '\r' < file > newfile
1 Like

can you try with below.... for me it working

>sed 's/~//g' GPS_input.txt
       COUNT   PERCENT
OLD PRODUCT RECORD COUNT          15476100.00%
NEW PRODUCT RECORD COUNT          15609  0.85%
NEW PRODUCT RECORDS INSERTED        133  0.85%
OLD PRODUCT RECORDS DELETED           0  0.00%
OLD PRODUCT RECORDS UNCHANGED     15249 98.53%
OLD PRODUCT RECORDS UPDATED         227  1.46%
CHANGES TO PRODUCT NAME               0  0.00%
CHANGES TO PRODUCT TYPE               0  0.00%
CHANGES TO PRODUCT LEVEL              0  0.00%

Except, of course, that that removes all tildes instead of only those at the start of a line and those at the end of a line. The OP did not want to remove the tildes before and after the COUNT field.

Try if you like awk

$ awk 'gsub(/^~+|~+$/,x) + 1' file
1 Like

Dear krupasindhu18,

you have dropped all the tilde characters including those mid-line, which is not what was requested. Scrutinizer and Akshay Hegde have the best answers.

Robin

or, when there may also be a carriage return character before the newline (which seems to be a problem in the OP's input):

awk 'gsub(/^~+|~*\r?$/,x) + 1'
1 Like

Thanks Don, can be combined.. no need of converting to unix format using tr before the use..