I am trying to modify a whole bunch of text files (*.sh)
with a small shell script.
$ cat yy
SRC="new_time(tx.create_dt_tm,'GMT','TIMEZONE')" DEST="cast(from_tz(cast(tx.create_dt_tm as timestamp), 'Etc/GMT0') at time zone 'Canada/Pacific' as date)"
for i in *.sh
do
echo modifying $i ...
sed -i "s/$SRC/$DEST/g" $i
done
I got errors like these ... what am I missing ?
modifying xxx.sh ...
sed: -e expression #1, char 100: unknown option to `s'
Note: both $SRC and $DEST have no double quotes in the string contents.
Run it with "bash -x -v yy"
+ for i in *.sh
+ echo modifying newcerts.sh ...
modifying newcerts.sh ...
+ echo sed -i 's/new_time(tx.create_dt_tm,'\''GMT'\'','\''TIMEZONE'\'')/cast(from_tz(cast(tx.create_dt_tm as timestamp), '\''Etc/GMT0'\'') at time zone '\''Canada/Pacific'\'' as date)/g' newcerts.sh
sed -i s/new_time(tx.create_dt_tm,'GMT','TIMEZONE')/cast(from_tz(cast(tx.create_dt_tm as timestamp), 'Etc/GMT0') at time zone 'Canada/Pacific' as date)/g newcerts.sh
Notice how the generated sed command contains too many slash characters.
Replace the delimiter slash characters with some other character not present in the
strings.
On Tuesday, November 9, 2021 at 1:14:37 PM UTC-8, David W. Hodgins wrote:
Run it with "bash -x -v yy"
+ for i in *.sh
+ echo modifying newcerts.sh ...
modifying newcerts.sh ...
+ echo sed -i 's/new_time(tx.create_dt_tm,'\''GMT'\'','\''TIMEZONE'\'')/cast(from_tz(cast(tx.create_dt_tm as timestamp), '\''Etc/GMT0'\'') at time zone '\''Canada/Pacific'\'' as date)/g' newcerts.sh
sed -i s/new_time(tx.create_dt_tm,'GMT','TIMEZONE')/cast(from_tz(cast(tx.create_dt_tm as timestamp), 'Etc/GMT0') at time zone 'Canada/Pacific' as date)/g newcerts.sh
Notice how the generated sed command contains too many slash characters.
Replace the delimiter slash characters with some other character not present in theThanks, I replace / with # in the sed cmd and it works now.
strings.
On Tuesday, November 9, 2021 at 5:25:28 PM UTC-5, Harry wrote:
On Tuesday, November 9, 2021 at 1:14:37 PM UTC-8, David W. Hodgins wrote: >>> [ slash characters in sed substitution and as sed separators ]
Notice how the generated sed command contains too many slash characters. >>>Thanks, I replace / with # in the sed cmd and it works now.
Replace the delimiter slash characters with some other character not present in the
strings.
anytime there's a potential file path involved mine looks like
sed "s=$this=$that="
suggesting a function:
psed () { : path-protected sed; sed "s=$1=$2=g" ; }
On 13.11.2021 16:35, applemcg wrote:
On Tuesday, November 9, 2021 at 5:25:28 PM UTC-5, Harry wrote:
On Tuesday, November 9, 2021 at 1:14:37 PM UTC-8, David W. Hodgins wrote: >>> [ slash characters in sed substitution and as sed separators ]
Notice how the generated sed command contains too many slash characters. >>>Thanks, I replace / with # in the sed cmd and it works now.
Replace the delimiter slash characters with some other character not present in the
strings.
anytime there's a potential file path involved mine looks like
sed "s=$this=$that="
suggesting a function:
psed () { : path-protected sed; sed "s=$1=$2=g" ; }
Functions should be suggested if they are generally applicable.
Your code shows the same issue with '=' characters that the OP
had with '/' characters. (Consider path-component data with '='
and $this or $that containing a '='.)
Taking another rare character might delay the failure further but
not solve the task generally.
Janis
[ slash characters in sed substitution and as sed separators ]
Taking another rare character might delay the failure further but
not solve the task generally.
of course; one might defend oneself against a range of unlikely characters using a CASE statement.
my last gainful employ was as trainer at a wall st fin svcs company. [...]
On 13.11.2021 18:19, applemcg wrote:
[ slash characters in sed substitution and as sed separators ]
Taking another rare character might delay the failure further but
not solve the task generally.
of course; one might defend oneself against a range of unlikely characters using a CASE statement.Or dynamically checking the $this and $that values in the function.
Even using binary separator values might fail in pathological cases,
though using something like sed s$'\1'"$this"$'\1'"$that"$'\1'
certainly fails much rarer.
my last gainful employ was as trainer at a wall st fin svcs company. [...]
(I cannot decipher "st fin svcs". - Ah, wait! Now I've got it.)
Janis
If I may, I'll disagree slightly with the idea that "Functions should
be suggested if they are generally applicable"
Stonebridge Mens Club <sbmc08831@gmail.com> writes: [...]
"cawk" _never_ gets used. There seems to be no standard for the CSV
file. i.e. what if a field has a comma in it?
As I recall there are multiple standards for CSV files.
The most nearly definitive is probably RFC 4180.
https://www.ietf.org/rfc/rfc4180.txt https://datatracker.ietf.org/doc/html/rfc4180
"cawk" _never_ gets used. There seems to be no standard for the CSV
file. i.e. what if a field has a comma in it?
I've a small family of awk wrappers: cawk, pawk, and tawk. a clue to
their usage:
tawk () { awk -F'\t' "$@"; }
"pawk" got quite a bit of use when i was at the T (at&~), since pipe-separated tables were widely in use there.
"cawk" _never_ gets used.
Sysop: | Keyop |
---|---|
Location: | Huddersfield, West Yorkshire, UK |
Users: | 296 |
Nodes: | 16 (2 / 14) |
Uptime: | 70:57:52 |
Calls: | 6,656 |
Calls today: | 2 |
Files: | 12,201 |
Messages: | 5,332,212 |
Posted today: | 1 |