Bera
Bera

Reputation: 1282

Here strings in ksh

What is wrong with the script below?

#!/bin/bash
a="\\\"P1\\\"\\\"P2\\\"\\\"P3\\\"\\\"P4\\\"\\\"P5\\\""
awk 'BEGIN{FS=OFS="\\\\\\\""} {$10="";NF-=2}1' <<< "$a"

Output:

\\\"P1\\\"\\\"P2\\\"\\\"P3\\\"\\\"P4\\\"

It works in bash but I tested in ksh and got the following error message:

#!/usr/bin/ksh
a="\\\"P1\\\"\\\"P2\\\"\\\"P3\\\"\\\"P4\\\"\\\"P5\\\""
b=$(awk 'BEGIN{FS=OFS="\\\\\\\""} {$10="";NF-=2}1' <<< "$a")
 .sh: syntax error: `< ' unexpected

I got problems in ksh shell, but this works

b=` echo $a | sed -e 's/\\\\"[^"]*\\\\"$//g' `

Upvotes: 1

Views: 1501

Answers (3)

jlliagre
jlliagre

Reputation: 30823

You might be using ksh88 or some ksh clone.

The posted code works fine with a current ksh release, and by current I mean newer than ksh93m+ released in 2002.

Upvotes: 0

konsolebox
konsolebox

Reputation: 75488

Use here docs instead:

awk 'BEGIN{FS=OFS="\\\\\\\""} {$10="";NF-=2}1' <<EOD
$a
EOD

Upvotes: 2

Chris Seymour
Chris Seymour

Reputation: 85795

The here string syntax <<< is a bash feature not supported by ksh. Just change your command to:

b=$(echo "$a" | awk 'BEGIN{FS=OFS="\\\\\\\""} {$10="";NF-=2}1')

Upvotes: 2

Related Questions