0

Need to remove extra space in sed command

I was able to get string after $$ENV$$_ in file with below command

sed '/.*$$ENV$$_/!d;s///;s/,.*//;s/^[ \t]*//' file.txt

am executing command as:

TAB=$(sed '/.*$$ENV$$_/!d;s///;s/,.*//;s/^[ \t]*//' $file.txt)

and this variable($TAB) i am passing on different command to replace value in different file with the command:

cat backup.txt | sed 's%\$\$BTABLE\$\$%"${TAB}"''%g' > $file_bkp1.txt

but in my output, I'm getting an extra space.

file.txt

CREATE MULTISET TABLE $$ENV$$_TEMP_ESM.EMPASGMTAMPTMPRTRSE , NO FALLBACK , 
    NO BEFORE JOURNAL, 
    NO AFTER JOURNAL, 
    CHECKSUM = DEFAULT, 
    DEFAULT MERGEBLOCKADSRATIO

Sed command to fetch value from file.txt

TAB=$(sed '/.*$$ENV$$_/!d;s///;s/,.*//;s/^[ \t]*//' file.txt)

Command to replace value (from: $$BTABLE$$) to (TEMP_ESM.EMPASGMTAMPTMPRTRSE) from backup.txt

cat backup.txt | sed 's%\$\$BTABLE\$\$%"${TAB}"''%g' > file_bkp.txt

backup.txt Input file

CREATE MULTISET TABLE $$ENV$$_$$BTABLE$$_NEW ,NO FALLBACK ,
     NO BEFORE JOURNAL,
     NO AFTER JOURNAL,
     CHECKSUM = DEFAULT,
     DEFAULT MERGEBLOCKRATIO
     (
      AR_ID INTEGER NOT NULL,
      AR_TO_RTE_TY_RL_TY_ID VARCHAR(32) CHARACTER SET LATIN NOT CASESPECIFIC NOT NULL,
      RTE_BASIS_TY_ID VARCHAR(32) CHARACTER SET LATIN NOT CASESPECIFIC NOT NULL,
      RTE_TY_ID VARCHAR(32) CHARACTER SET LATIN NOT CASESPECIFIC NOT NULL,
      RTE_ID INTEGER NOT NULL,

Output of file_bkp.txt with extra space at end which highlighted in bold letter Output file:

CREATE MULTISET TABLE $$ENV$$_TEMP_ESM.EMPASGMTAMPTMPRTRSE _NEW ,NO FALLBACK ,
     NO BEFORE JOURNAL,
     NO AFTER JOURNAL,
     CHECKSUM = DEFAULT,
     DEFAULT MERGEBLOCKRATIO
     (
      AR_ID INTEGER NOT NULL,
      AR_TO_RTE_TY_RL_TY_ID VARCHAR(32) CHARACTER SET LATIN NOT CASESPECIFIC NOT NULL,
      RTE_BASIS_TY_ID VARCHAR(32) CHARACTER SET LATIN NOT CASESPECIFIC NOT NULL,
      RTE_TY_ID VARCHAR(32) CHARACTER SET LATIN NOT CASESPECIFIC NOT NULL,
      RTE_ID INTEGER NOT NULL,
  • Can you give only clear input file and desired output? – Egor Vasilyev Dec 5 '17 at 10:29
  • update input and output file, let me know if you still not clear – Baranidharan Dec 5 '17 at 10:47
0

Get the tag with

TAB=$(sed -n 's%.*\$\$ENV\$\$_\(.*\)_NEW.*%\1%p' file.txt)

This uses -n to skip printing of all lines except those explicitly designated for printing. The p flag in the substitution says "print this line if a substitution occurred".

Furthermore, for the line we're interested in we filter out everything except the exact substring we're interested in by surrounding the substring with \(..\). Then in the substitution we can pull the text inside those parens with a "back-reference": \1.

With this value, do the substitution with:

sed 's%\$\$BTABLE\$\$%'"${TAB}"'%g'

This is just a slight variation of your command with some tweaks to the quoting.

0

Well, with the input here:

CREATE MULTISET TABLE $$ENV$$_TEMP_ESM.EMPASGMTAMPTMPRTRSE , NO FALLBACK ,

The sed script sed '/.*$$ENV$$_/!d;s///;s/,.*//;s/^[ \t]*//' would first remove that $$ENV$$_ (/.*$$ENV$$_/!d; s///), then remove anything after the comma (s/,.*//), and then remove any leading spaces (s/^[ \t]*//), but not the space before the comma. So the output is TEMP_ESM.EMPASGMTAMPTMPRTRSE␣, with a trailing space (). I think that space carries on in your output.

I think you could change that last substitution to s/[ \t]*//g (remove the ^ anchor, add the /g) to remove any and all remaining whitespace anywhere in the string.

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.