Cutting a part of line till delimiter

here are the few scenarios...

isoSizeKB text NOT NULL,
 reserved1 varchar(255),
      KEY `deviceId` (`deviceId`)
`d5` varchar(255) COLLATE utf8_unicode_ci DEFAULT NULL,
`dHead` enum('HistoryInfo','Diversion') COLLATE utf8_unicode_ci,
`ePR` int(11) DEFAULT '0',
  PRIMARY KEY (`id`)

what i want to achieve is if the line has ',' at the end it deletes it... and also spaces(or tabs) before or after the line....

i tried echo "reserved1 varchar(255)," | cut -d ',' -f1 | sed 's/^[ ]//g;s/[ ]$//g'
but this willl not work for all conditions say if there is , inbetweeen the line...

---------- Post updated at 02:38 PM ---------- Previous update was at 02:07 PM ----------

i was using

linexT=$( echo $linexT  | sed 's/^[ ]*//g;s/[ ]*$//g' )
                                                 len=${#linexT}
                                                echo "len: $len"
                                                 charlast=${linexT:$len-1:$len}
                                                  if [[ "$charlast" = "," ]]
                                                 then
                                                        line11=${linexT:0:$len-1}
                                                 else
                                                      line11=$linexT
                                                 fi

the above since many days but the above doesnt work for few scenarios.. so is there any one liner for this.. which works for all the scenarios mentioned in my previous post?

 perl -lne 's/^\s*|\s*$//;print unless /,$/' infile

ur code will work fine for spaces not for tabs, can u give us some i/p, expected and obtained o/p.

Hi vivek d r,

Try if this could work for you. It deletes all lines which end with ',' and for those which not, deletes dealing and trailing spaces:

$ perl -ne 'unless ( /,\s*$/ ) { s/^\s*//; s/\s*$//; printf qq[%s\n], $_ }' infile
KEY `deviceId` (`deviceId`)
PRIMARY KEY (`id`)

Regards,
Birei

consider the content of the variable $line11 is any of the below

line11="isoSizeKB text NOT NULL,"
similarly below ones....
 reserved1 varchar(255),
or
      KEY `deviceId` (`deviceId`)
or
`d5` varchar(255) COLLATE utf8_unicode_ci DEFAULT NULL,
or
`dHead` enum('HistoryInfo','Diversion') COLLATE utf8_unicode_ci,
or
`ePR` int(11) DEFAULT '0',
or
  PRIMARY KEY (`id`)

expected output is

isoSizeKB text NOT NULL
 reserved1 varchar(255)
KEY `deviceId` (`deviceId`)
`d5` varchar(255) COLLATE utf8_unicode_ci DEFAULT NULL
`dHead` enum('HistoryInfo','Diversion') COLLATE utf8_unicode_ci
`ePR` int(11) DEFAULT '0'
  PRIMARY KEY (`id`)

respectively...
that is it should delte ',' (comma) present at the end of the line if present else not.. also it should delete empty spaces before the line (either n spaces or tabs)

in ur snippet change as below

echo $linexT  | sed 's/^[ \t]*//g;s/[ \t]*$//g'

if u need one liner

echo $linexT  | sed 's/^[ \t]*//g;s/[ \t]*$//g;s/,$//g'

else

sed 's/^[ \t]*//g;s/[ \t]*$//g;s/,$//g' input_file

Try these...

1 Like

i dont want to input any file to the command... the lines will be stored in a variable say $line11 i want to modify it then and there itself.. or modify it and store it in another variable

---------- Post updated at 03:22 PM ---------- Previous update was at 03:18 PM ----------

thanks shiva_shankar.. your code works for me :slight_smile:

Redirection (computing) - Wikipedia, the free encyclopedia

echo "${linexT}"|perl -lne 's/^\s*|\s*$//;print unless /,$/'