Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

I am trying to format a database script and I need to remove the last " , " from the lines that are before ")," but I have not been successful ...

This is the input file

DROP TABLE IF EXISTS attribute_type;
CREATE TABLE attribute_type (
  attribute_type_id int(11) NOT NULL,
  name varchar(30) COLLATE es_CO.UTF-8 NOT NULL,
  description text COLLATE es_CO.UTF-8,
  is_set smallint(6) DEFAULT NULL,    <-- Here is the last character I want to remove
),


DROP TABLE IF EXISTS attribute_type_options;
CREATE TABLE attribute_type_options (
  attribute_type_options_id int(11) NOT NULL,
  person_attribute_type_id int(11) DEFAULT NULL,
  name varchar(60) COLLATE es_CO.UTF-8 NOT NULL,
  default_value bit(1) DEFAULT NULL,    <-- Here is the last character I want to remove
),

This should be the output

DROP TABLE IF EXISTS attribute_type;
CREATE TABLE attribute_type (
  attribute_type_id int(11) NOT NULL,
  name varchar(30) COLLATE es_CO.UTF-8 NOT NULL,
  description text COLLATE es_CO.UTF-8,
  is_set smallint(6) DEFAULT NULL
),


DROP TABLE IF EXISTS attribute_type_options;
CREATE TABLE attribute_type_options (
  attribute_type_options_id int(11) NOT NULL,
  person_attribute_type_id int(11) DEFAULT NULL,
  name varchar(60) COLLATE es_CO.UTF-8 NOT NULL,
  default_value bit(1) DEFAULT NULL
),

I tried this solution but it does the change in all lines , not just where I need Thank you for your help !!!

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
232 views
Welcome To Ask or Share your Answers For Others

1 Answer

Try doing this :

perl -0pe 's/,s*
)/
)/g' file

NOTE

  • -0 permit perl to read the whole file instead of line by lines
  • -p is like using while (<>) {} construct and print each lines like sed
  • s/, )/ )/g is a regex substitution

Another solution

using pure :

while read line; do
    if [[ $line =~ ^),s* ]]; then
        echo "${lastline%,}"
    else
        echo "$lastline"
    fi

    lastline="$line"
done < file

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...