3

I have below string in my DDL file and i want to remove the last ) character.

DDL has the ) character in many places, but I want to remove only in below place indicated.

..
`event_sts` string,
  `rsn_code` string)
ROW FORMAT DELIMITED
  FIELDS TERMINATED BY '|'
ESCAPED BY '\\\\')                     <----- here
STORED AS INPUTFORMAT
  'org.apache.hadoop.hive.ql.io.orc.OrcInputFormat'

I tried using sed but somehow it's not working.

note: I have many DDLs with same requirement so the occurrence of '(' is not static.

  • 1
    awk '/ESCAPED/ {sub(/\)$/,"")}1' file – jasonwryan Aug 27 '16 at 23:20
  • thanks for reply.but sorry. it didnt work. – dileepvarma Aug 27 '16 at 23:23
  • @dileepvarma Do you want to remove ) only if it is the last character on the last line in the file? Or do you want to remove all ) whenever they on a line that contains ESCAPED? Or something else? Please clarify. – John1024 Aug 27 '16 at 23:43
  • i need whenever line contains ESCAPED only. – dileepvarma Aug 27 '16 at 23:49
  • What didn't work? Use gsub if it is for every instance... – jasonwryan Aug 28 '16 at 0:26
3

This will remove all ) from any line that contains ESCAPED:

sed '/ESCAPED/s/)//g' filename

To change the file in-place:

sed -i.bak '/ESCAPED/s/)//g' filename

How it works

  • /ESCAPED/

    This selects only those lines that contain ESCAPED.

  • s/)//g

    For the selected lines, this removes all ).

    The substutute command typically looks like s/old/new/ which looks for regular expression old and replaces it with new. In our case old is ) and new is an empty string. Consequently ) is removed. The g at the end of the command tells sed to do this for every occurrence of ) on the line, not just the first.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.