I have text file as follows and would like to remove the last occurance of "UNION ALL" string and replace @@ with single quote (').
Input text in file is
with temp as (
( select ----------- where OPERATION = @@B@@ and OBJECTTYPE = @@P@@ and start_time desc ) UNION ALL
( select ----------- where OPERATION = @@B@@ and OBJECTTYPE = @@P@@ and start_time desc ) UNION ALL
( select ----------- where OPERATION = @@B@@ and OBJECTTYPE = @@P@@ and start_time desc ) UNION ALL
( select ----------- where OPERATION = @@B@@ and OBJECTTYPE = @@P@@ and start_time desc ) UNION ALL
)
select xxxx, yyyyyy from temp with ur ;
Output expected is
with temp as (
( select ----------- where OPERATION = 'B' and OBJECTTYPE = 'P' and start_time desc ) UNION ALL
( select ----------- where OPERATION = 'B' and OBJECTTYPE = 'P' and start_time desc ) UNION ALL
( select ----------- where OPERATION = 'B' and OBJECTTYPE = 'P' and start_time desc ) UNION ALL
( select ----------- where OPERATION = 'B' and OBJECTTYPE = 'P' and start_time desc )
)
select xxxx, yyyyyy from temp with ur ;
with temp as (
( select ----------- where OPERATION = @@B@@ and OBJECTTYPE = @@P@@ and start_time desc ) UNION ALL
( select ----------- where OPERATION = @@B@@ and OBJECTTYPE = @@P@@ and start_time desc ) UNION ALL
( select ----------- where OPERATION = @@B@@ and OBJECTTYPE = @@P@@ and start_time desc ) UNION ALL
( select ----------- where OPERATION = @@B@@ and OBJECTTYPE = @@P@@ and start_time desc ) UNION ALL <------- remove this
)
select xxxx, yyyyyy from temp with ur ;
Output file text_output.txt looks like:
with temp as (
( select ----------- where OPERATION = @@B@@ and OBJECTTYPE = @@P@@ and start_time desc ) UNION ALL
( select ----------- where OPERATION = @@B@@ and OBJECTTYPE = @@P@@ and start_time desc ) UNION ALL
( select ----------- where OPERATION = @@B@@ and OBJECTTYPE = @@P@@ and start_time desc ) UNION ALL
( select ----------- where OPERATION = @@B@@ and OBJECTTYPE = @@P@@ and start_time desc ) UNION ALL
)
select xxxx, yyyyyy from temp with ur
The "awk" solution will delete the last line for the input:
select xxxx, yyyyyy from temp with ur ;
( select ----------- where OPERATION = '5' and OBJECTTYPE = 'P' and start_time desc ) UNION ALL
( select ----------- where OPERATION = '6' and OBJECTTYPE = 'P' and start_time desc ) UNION ALL
( select ----------- where OPERATION = '7' and OBJECTTYPE = 'P' and start_time desc ) UNION ALL )
The following code will remove the last occurrence of "UNION ALL":
#!/usr/bin/ksh
mLast='FIRST'
mUA='UNION ALL'
mLastFound='N'
while read mLine; do
mCnt=$(echo ${mLine} | egrep -c "${mUA}")
if [[ "${mCnt}" = "0" ]]; then
mFound='N'
else
mFound='Y'
fi
if [[ "${mFound}" = "N" ]]; then
if [[ "${mLastFound}" = "Y" ]]; then
mLast=$(echo ${mLast} | sed "s/${mUA}//")
fi
fi
if [[ "${mLast}" != "FIRST" ]]; then
echo ${mLast}
fi
mLast=${mLine}
mLastFound=${mFound}
done < Input_File
if [[ "${mLast}" != "FIRST" ]]; then
if [[ "${mLastFound}" = "Y" ]]; then
mLast=$(echo ${mLast} | sed "s/${mUA}//")
fi
echo ${mLast}
fi