Hi I'm trying to change the content of the multiple files in the folder. I encountered sed error while trying to iterate through array where I store names of the files I need to edit. I tried to use the specific element of the array "${len_1[0]}
" and it worked perfectly.
Here's what I've done so far:
len_1=($(find . -name "*.dita"))
len=${#len_1[@]}
echo $len
for ((i=0; i<=len; i++)); do
apps=$(grep -Po 'appname="\K[^"]+' ${len_1[$i]}) &&
title=$(grep -Po '<title>\K[^</title>]+' ${len_1[$i]}) &&
sed -i "s/_[0-9]\+/_$apps.$title/g" ${len_1[$i]} &&
sed -i "s/id=\"[0-9]\+\"\+/id=\"$apps.$title\"/g" ${len_1[$i]};
done
As stated before
apps=$(grep -Po 'appname="\K[^"]+' ${len_1[0]}) &&
title=$(grep -Po '<title>\K[^</title>]+' ${len_1[0]}) &&
sed -i "s/_[0-9]\+/_$apps.$title/g" ${len_1[0]} &&
sed -i "s/id=\"[0-9]\+\"\+/id=\"$apps.$title\"/g" ${len_1[0]};
works perfectly.
The error I get is:
sed: -e expression #1, char 37: unterminated `s' command
sed: -e expression #1, char 33: unterminated `s' command
sed: -e expression #1, char 36: unterminated `s' command
sed: -e expression #1, char 36: unterminated `s' command
sed: -e expression #1, char 37: unterminated `s' command
sed: -e expression #1, char 39: unterminated `s' command
sed: -e expression #1, char 34: unterminated `s' command
If you have multiple matches, grep -o
will produce multiple lines of output. And newlines end sed
commands:
$ echo abcabbcd | grep -o 'ab*'
ab
abb
$ repl=$(echo abcabcd | grep -o 'ab*')
$ sed -e "s/foo/$repl/"
sed: -e expression #1, char 8: unterminated `s' command
Also, [^</title>]+
means "one or more characters that are not any of ^
, <
, /
, t
, i
, l
, e
, or >
". It's probably not what you actually want:
$ echo '<title>abcdefgh</title>' | grep -Po '<title>\K[^</title>]+'
abcd
You could use the equivalent when the separator is just one character, e.g. "[^"]*"
is ok. But here something like <title>\K.*?(?=</title>)'
might work better:
$ echo '<title>abcdefgh</title> <title>foobar</title>' | grep -Po '<title>\K.*?(?=</title>)'
abcdefgh
foobar
(Though I would avoid tricks like \K
and lookaheads, and use the simpler perl -lne 'print $1 if m,<title>(.*?)</title>,'
or post-process the output instead, but that's my preference.)
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments