3

We have a file containing 6 columns:

FILEDESCRIPTOR . DESCR00001     FILEDESCRIPTOR  .  DESCRIPTIVENAME
FILEDESCRIPTOR . LSTUPDNAM      FILEDESCRIPTOR  .  LASTUPDATENAME
FILEDESCRIPTOR . LOCAT00001     FILEDESCRIPTOR  .  LOCATION_ID
FILEDESCRIPTOR . RETAILPRC      FILEDESCRIPTOR  .  RETAILPRICE
FILEDESCRIPTOR . LSTUPDTIME     FILEDESCRIPTOR  .  UPDTIMESTAMP
INDUSTRYDIVISION . DESCR00001   INDUSTRYDIVISION  .  DESCRIPTION
INDUSTRYDIVISION . DIVIS00001   INDUSTRYDIVISION  .  DIVISIONCODE
INDUSTRYGROUP . DESCR00001      INDUSTRYGROUP  .  DESCRIPTION
INDUSTRYGROUP . DIVIS00001      INDUSTRYGROUP  .  DIVISION_ID

All I need to do is treat the first 3 columns separated by a single space and store it in a variable as a single column, and then store the next three that are separated by a single space into another variable. Then, use sed to replace the first variable with the second.

I tried the below script but threw errors:

#!/bin/bash
while 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.txt
done < file

The script is not working. The errors I got are:

sed: -e expression #1, char 23: unterminated `s' command
sed: -e expression #1, char 23: unterminated `s' command
sed: -e expression #1, char 20: unterminated `s' command
sed: -e expression #1, char 23: unterminated `s' command
sed: -e expression #1, char 23: unterminated `s' command
sed: -e expression #1, char 23: unterminated `s' command
sed: -e expression #1, char 23: unterminated `s' command
sed: -e expression #1, char 22: unterminated `s' command
sed: -e expression #1, char 22: unterminated `s' command
sed: -e expression #1, char 22: unterminated `s' command
sed: -e expression #1, char 23: unterminated `s' command
sed: -e expression #1, char 30: unterminated `s' command

What can be done?

1
  • That doesn't really matter. That is just a file, and this command prints it with single space column2="$(echo -e "$line \n" | awk '{print $4, $5, $6}')" When you echo $column2 it is printing with single space. Oct 9, 2015 at 14:17

3 Answers 3

3

No need to while read...
You could simply process your input file:

sed 's|[[:blank:]]\{1,\}|/|3;s|.*|s/&/g|' infile

and turn it into a sed script:

s/FILEDESCRIPTOR . DESCR00001/FILEDESCRIPTOR . DESCRIPTIVENAME/g
s/FILEDESCRIPTOR . LSTUPDNAM/FILEDESCRIPTOR . LASTUPDATENAME/g
s/FILEDESCRIPTOR . LOCAT00001/FILEDESCRIPTOR . LOCATION_ID/g
s/FILEDESCRIPTOR . RETAILPRC/FILEDESCRIPTOR . RETAILPRICE/g
s/FILEDESCRIPTOR . LSTUPDTIME/FILEDESCRIPTOR . UPDTIMESTAMP/g
s/INDUSTRYDIVISION . DESCR00001/INDUSTRYDIVISION . DESCRIPTION/g
s/INDUSTRYDIVISION . DIVIS00001/INDUSTRYDIVISION . DIVISIONCODE/g
s/INDUSTRYGROUP . DESCR00001/INDUSTRYGROUP . DESCRIPTION/g
s/INDUSTRYGROUP . DIVIS00001/INDUSTRYGROUP . DIVISION_ID/g

and pipe that to a second sed -f- to process your IMS_Procedures.txt:

sed 's|[[:blank:]]\{1,\}|/|3;s|.*|s/&/g|' infile | sed -f- IMS_Procedures.txt

I don't quite understand what "this is a file and this command prints it with single space" means so if fields in your infile are delimited by more than one space then you could use tr first:

tr -s '[[:blank:]]' ' ' <infile | sed 's| |/|3;s|.*|s/&/g|' | sed -f- IMS_Procedures.txt
0

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.txt

sed 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.txt
done <file
  • read store the 6 fields in the variables $a to $f
    • Then 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.

3
  • hey @chaos this one i have tried earlier and worked.but the just wondering y the other one dint work though. Oct 9, 2015 at 14:35
  • can u pls tell me how to sort that out. "sed recognizes this as an unterminated s command". pls just curious" Oct 9, 2015 at 14:43
  • @VishalArukil change column2="$(echo -e "$line \n" | awk '{print $4, $5, $6}')" to column2="$(echo "$line" | awk '{print $4, $5, $6}')" (without the \n)
    – chaos
    Oct 9, 2015 at 15:29
0

I would try the obvious :

awk '{printf "s/%s%s%s/%s%s%s/\n",$1,$2,$3,$4,$5,$6} ' file |
sed -i -f - IMS_Procedures.txt

where sed file is generate by awk.

you can adjust space using

awk '{printf "s/%s %s %s/%s %s %s/\n",$1,$2,$3,$4,$5,$6}

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .