Instead of ugly sed regex patterns, If all the lines have the same fixed format like your two lines (6*x;cmd="...";4*x), bellow alternative commands does the job in the file, without requiring a file read loop.
Key point (if format is steady across all lines) is the use of the "
as delimiter, cutting the lines in 3 separate fields.
$ sed 's/lbk_addcolumn/ColumnAdd/;s/lbk_dropcolumn/ColumnDrop/' <(awk -F'"' '{if (($0=="")) print $0;else print $1"\""$2" || "$2"\""$3}' file.txt)
Or :
$ a=$(awk -F'"' '{if (($0=="")) print $0;else print $1"\""$2" || "$2"\""$3}' file.txt)
$ sed 's/lbk_addcolumn/ColumnAdd/;s/lbk_dropcolumn/ColumnDrop/' <<<"$a"
awk splits each line of file with "
as delimiter.
if $0=""
then it is a blank line, print the blank line and go on
else print all line fields in required order.
Finally sed replaces the first found text lbk_addcolumn
with ColumnAdd
.
Test:
$ cat e.txt
x;x;x;x;x;x;cmd="lbk_addcolumn TABLE_NAME_1 COLUMN_X";x;x;x;x
x;x;x;x;x;x;cmd="lbk_dropcolumn TABLE_NAME_2 COLUMN_Y";x;x;x;x
# sed 's/lbk_addcolumn/ColumnAdd/;s/lbk_dropcolumn/ColumnDrop/' <(awk -F'"' '{if (($0=="")) print $0;else print $1"\""$2" || "$2"\""$3}' e.txt)
x;x;x;x;x;x;cmd="ColumnAdd TABLE_NAME_1 COLUMN_X || lbk_addcolumn TABLE_NAME_1 COLUMN_X";x;x;x;x
x;x;x;x;x;x;cmd="ColumnDrop TABLE_NAME_2 COLUMN_Y || lbk_dropcolumn TABLE_NAME_2 COLUMN_Y";x;x;x;x
Same result can be achieved even only with awk and the use of the gsub awk function:
$ awk -F'"' '{if ($0=="") print $0;else {a=$2;gsub("lbk_addcolumn","ColumnAdd",$2);gsub("lbk_dropcolumn","ColumnDrop",$2);print $1"\""$2" || "a"\""$3;}}' e.txt
x;x;x;x;x;x;cmd="ColumnAdd TABLE_NAME_1 COLUMN_X || lbk_addcolumn TABLE_NAME_1 COLUMN_X";x;x;x;x
x;x;x;x;x;x;cmd="ColumnDrop TABLE_NAME_2 COLUMN_Y || lbk_dropcolumn TABLE_NAME_2 COLUMN_Y";x;x;x;x
awk better presentation:
$ awk -F'"' '{
if ($0=="") print $0;
else {
a=$2;
gsub("lbk_addcolumn","ColumnAdd",$2);
gsub("lbk_dropcolumn","ColumnDrop",$2);
print $1"\""$2" || "a"\""$3;
}
}' e.txt