We have a file containing 6 columns.FILEDESCRIPTOR . DESCR00001     FILEDESCRIPTOR  .  DESCRIPTIVENAMEFILEDESCRIPTOR . LSTUPDNAM      FILEDESCRIPTOR  .  LASTUPDATENAMEFILEDESCRIPTOR . LOCAT00001     FILEDESCRIPTOR  .  LOCATION_IDFILEDESCRIPTOR . RETAILPRC      FILEDESCRIPTOR  .  RETAILPRICEFILEDESCRIPTOR . LSTUPDTIME     FILEDESCRIPTOR  .  UPDTIMESTAMPINDUSTRYDIVISION . DESCR00001   INDUSTRYDIVISION  .  DESCRIPTIONINDUSTRYDIVISION . DIVIS00001   INDUSTRYDIVISION  .  DIVISIONCODEINDUSTRYGROUP . DESCR00001      INDUSTRYGROUP  .  DESCRIPTIONINDUSTRYGROUP . DIVIS00001      INDUSTRYGROUP  .  DIVISION_IDAll I need to do, is treat first 3 columns seperated by a single space and store it in a variable as single column, and then the next 3 separated by a single space and store it in another variable. Then use sed to replace the first variable with the second.I tried the below script but threw errors:#!/bin/bashwhile IFS='' read -r line || [[ -n $line ]];do        column2=$(echo -e $line \n | awk '{print $4, $5, $6}')        #column3=$(echo -e $line \n | awk '{print $3}')        column1=$(echo -e $line \n | awk '{print $1, $2, $3}' )        #echo  $column1        #echo  $column2                 sed  s/${column1}/${column2}/g IMS_Procedures.txtdone < fileThe script is not working. The errors I got are:sed: -e expression #1, char 23: unterminated `s' commandsed: -e expression #1, char 23: unterminated `s' commandsed: -e expression #1, char 20: unterminated `s' commandsed: -e expression #1, char 23: unterminated `s' commandsed: -e expression #1, char 23: unterminated `s' commandsed: -e expression #1, char 23: unterminated `s' commandsed: -e expression #1, char 23: unterminated `s' commandsed: -e expression #1, char 22: unterminated `s' commandsed: -e expression #1, char 22: unterminated `s' commandsed: -e expression #1, char 22: unterminated `s' commandsed: -e expression #1, char 23: unterminated `s' commandsed: -e expression #1, char 30: unterminated `s' commandWhat can be done?
Passing variables to the sed command,
text processing;sed;awk
In your $column1 and $column2 variable there is a newline at the end and the sed command looks then like this:sed  s/FILEDESCRIPTOR . DESCR00001  /FILEDESCRIPTOR . DESCRIPTIVENAME  /g IMS_Procedures.txtsed recognizes this as an unterminated s command.Use this instead:while read -r a b c d e f; do  sed -i s/$a $b $c/$d $e $f/g IMS_Procedures.txtdone <fileread store the 6 fields in the variables $a to $fThen sed replaces the first 3 fields as a space separated stringwith the last 3 in the file IMS_Procedures.txt. -i activates in-place editing, that the changes are written to the file, instead of printed to the screen.Notice the . in sed will match any character not only the dot. That has to be esacped with \. in the input file.