sed garbled error message in bash shell

Sed garbled error. Cannot determine why the sed command to insert a line at the beginning of a file will not work on declared variables.

outfile='DAR.V2.2012115.1.CSV'

testfile='totality_request.sql'

header_prefix='DATA FILE'

no_ext_file=`echo $outfile |sed 's/\(.*\)..../\1/'`

echo $no_ext_file

header_file="$header_prefix $no_ext_file"

echo $header_file

trailer_prefix='DATA RECORDS'

trailer_records=`wc -l $outfile| awk '{print $1}'`

echo $trailer_records

trailer_file="$trailer_prefix $trailer_records"

echo $trailer_file

sed "1i\ 
$header_file $outfile" > outfile_new

Here is the output from bash shell:

bash -x test3.sh
+ outfile=DAR.V2.2012115.1.CSV
+ testfile=totality_request.sql
+ header_prefix='DATA FILE'
++ echo DAR.V2.2012115.1.CSV
++ sed 's/\(.*\)..../\1/'
+ no_ext_file=DAR.V2.2012115.1
+ echo DAR.V2.2012115.1
DAR.V2.2012115.1
+ header_file='DATA FILE DAR.V2.2012115.1'
+ echo DATA FILE DAR.V2.2012115.1
DATA FILE DAR.V2.2012115.1
+ trailer_prefix='DATA RECORDS'
++ wc -l DAR.V2.2012115.1.CSV
++ awk '{print $1}'
+ trailer_records=2
+ echo 2
2
+ trailer_file='DATA RECORDS 2'
+ echo DATA RECORDS 2
DATA RECORDS 2
+ sed '1i\ 
DATA FILE DAR.V2.2012115.1 DAR.V2.2012115.1.CSV'
sed: command garbled: 1i\

In your sample there seems to be a space behind the \ that should not be there. The backslash should be the last character on the line.

I removed the trailing space and checked to make sure there were not others with a :set list, but the same message returned.

outfile='DAR.V2.2012115.1.CSV'$
$
testfile='totality_request.sql'$
$
header_prefix='DATA FILE'$
$
no_ext_file=`echo $outfile |sed 's/\(.*\)..../\1/'`$
$
echo $no_ext_file$
$
header_file="$header_prefix $no_ext_file"$
$
echo $header_file$
$
trailer_prefix='DATA RECORDS'$
$
trailer_records=`wc -l $outfile| awk '{print $1}'`$
$
echo $trailer_records$
$
trailer_file="$trailer_prefix $trailer_records"$
$
echo $trailer_file$
$
sed "1i\$
$header_file $outfile" > outfile_new$

It seems that it wants to treat the 1i as text and not part of the command?

smenago@eqewrslhp01$ bash -x test3.sh
+ outfile=DAR.V2.2012115.1.CSV
+ testfile=totality_request.sql
+ header_prefix='DATA FILE'
++ echo DAR.V2.2012115.1.CSV
++ sed 's/\(.*\)..../\1/'
+ no_ext_file=DAR.V2.2012115.1
+ echo DAR.V2.2012115.1
DAR.V2.2012115.1
+ header_file='DATA FILE DAR.V2.2012115.1'
+ echo DATA FILE DAR.V2.2012115.1
DATA FILE DAR.V2.2012115.1
+ trailer_prefix='DATA RECORDS'
++ wc -l DAR.V2.2012115.1.CSV
++ awk '{print $1}'
+ trailer_records=2
+ echo 2
2
+ trailer_file='DATA RECORDS 2'
+ echo DATA RECORDS 2
DATA RECORDS 2
+ sed '1iDATA FILE DAR.V2.2012115.1 DAR.V2.2012115.1.CSV'
+ sed 'DATA RECORDS 2'
sed: command garbled: 1iDATA FILE DAR.V2.2012115.1 DAR.V2.2012115.1.CSV
sed: command garbled: DATA RECORDS 2

Try:

sed "1i\\
$header_file
" "$outfile" > outfile_new

Worked. Thanks a bunch. I need to add a trailer to this file as well and tried to pipe in the 2nd sed command, but received the same error.

cat test3.sh
outfile='DAR.V2.2012115.1.CSV'

testfile='totality_request.sql'

header_prefix='DATA FILE'

no_ext_file=`echo $outfile |sed 's/\(.*\)..../\1/'`

echo $no_ext_file

header_file="$header_prefix $no_ext_file"

echo $header_file

trailer_prefix='DATA RECORDS'

trailer_records=`wc -l $outfile| awk '{print $1}'`

echo $trailer_records

trailer_file="$trailer_prefix $trailer_records"

echo $trailer_file

     sed "1i\\
$header_file
" "$outfile" | sed "$a\\
$trailer_file
" > outfile_new

Below is the sed garbled error again.

bash -x test3.sh
+ outfile=DAR.V2.2012115.1.CSV
+ testfile=totality_request.sql
+ header_prefix='DATA FILE'
++ echo DAR.V2.2012115.1.CSV
++ sed 's/\(.*\)..../\1/'
+ no_ext_file=DAR.V2.2012115.1
+ echo DAR.V2.2012115.1
DAR.V2.2012115.1
+ header_file='DATA FILE DAR.V2.2012115.1'
+ echo DATA FILE DAR.V2.2012115.1
DATA FILE DAR.V2.2012115.1
+ trailer_prefix='DATA RECORDS'
++ wc -l DAR.V2.2012115.1.CSV
++ awk '{print $1}'
+ trailer_records=2
+ echo 2
2
+ trailer_file='DATA RECORDS 2'
+ echo DATA RECORDS 2
DATA RECORDS 2
+ sed '1i\
DATA FILE DAR.V2.2012115.1
' DAR.V2.2012115.1.CSV
+ sed '\
DATA RECORDS 2
'
sed: command garbled: \
DATA RECORDS 2

Try: sed "\$a...

1 Like

Worked. Appreciate the help.