Linux SED命令环境变量

huangapple go评论69阅读模式
英文:

Linux SED Command environment variables

问题

I have two variables and the outputs of them are like below.

echo $VAR_ONE
'https://url_one','https://url_two'

echo $VAR_TWO
'https://url_two'

I want to remove 'https://url_two' (Which is $VAR_TWO) from $VAR_ONE and set to another variable. I executed the below but gives an error.

VAR_THREE=$(echo $VAR_ONE | sed s/'$VAR_TWO'//)

sed: bad option in substitution expression

Added to a script as well but the same error

#!/bin/bash

VAR_ONE=$(echo ''https://url_one','https://url_two'')
VAR_TWO=$(echo ''https://url_two'')
VAR_THREE=$(echo $VAR_ONE | sed s/'$VAR_TWO'//)
英文:

I have two variables and the outputs of them are like below.

echo $VAR_ONE
'https://url_one','https://url_two'

echo $VAR_TWO
'https://url_two'

I want to remove 'https://url_two' (Which is $VAR_TWO) from $VAR_ONE and set to another variable. I executed the below but gives an error.

VAR_THREE=$(echo $VAR_ONE | sed s/"$VAR_TWO"//)

sed: bad option in substitution expression

Added to a script as well but the same error

#!/bin/bash

VAR_ONE=$(echo "'https://url_one','https://url_two'")
VAR_TWO=$(echo "'https://url_two'")
VAR_THREE=$(echo $VAR_ONE | sed s/"$VAR_TWO"//)

答案1

得分: 1

Here is the translated code:

VAR_ONE="'https://url_one','https://url_two'"
VAR_TWO="'https://url_two'"
VAR_THREE="$(echo "$VAR_ONE" | sed "s/$VAR_TWO//")"

And the modified code with a different delimiter:

VAR_THREE="$(echo "$VAR_ONE" | sed "s#$VAR_TWO##")"

And the alternative solution using awk:

VAR_THREE="$(echo "$VAR_ONE" | awk -v pattern="$VAR_TWO" '{sub(pattern,"");print}')"

Please note that the translations have been provided without the HTML entity codes (e.g., ") for better readability.

英文:
VAR_ONE="'https://url_one','https://url_two'"
VAR_TWO="'https://url_two'"
VAR_THREE="$(echo "$VAR_ONE" | sed s/"$VAR_TWO"//)"

Resolves to the call sed "s/'https://url_one','https://url_two'/'https://url_two'/", which is obviously incorrect, as the sed s command must be called with exactly 3 (unescaped) delimiters, yet your call is using 9 of them.

One solution would be using a delimiter which is not part of neither your pattern, nor your replacement, e.g. #:

VAR_THREE="$(echo "$VAR_ONE" | sed "s#$VAR_TWO##")"

Note that this will break as soon as your URLs contain the #. I would suggest not using sed, but invoking awk which can handle variables properly and can do so without the shell interpolating them into your program (this avoids all sorts of problems).

VAR_THREE="$(echo "$VAR_ONE" | awk -v pattern="$VAR_TWO" '{sub(pattern,"");print}')"

答案2

得分: 1

使用bash,无需进行外部调用。它的"参数扩展"中的"模式替换"功能已经足够强大:

# 生成包含所有合法的8位ASCII字符的变量
# (我认为在适当的区域设置下,也可以适用于utf等)
all=$(perl -E 'say map chr,1..255')

# 查看其包含的内容
echo "$all" | od -c
echo

# 创建包含两份副本的变量
var1="$all================$all"

new='----------------'

# 替换第一份副本
echo "${var1/"$all"/"$new"}" | od -c
echo

# 替换所有副本
echo "${var1//"$all"/"$new"}"

输出:

0000000 001 002 003 004 005 006  \a  \b  \t  \n  \v  \f  \r 016 017 020
0000020 021 022 023 024 025 026 027 030 031 032 033 034 035 036 037    
0000040   !   "   #   $   %   &   '   (   )   *   +   ,   -   .   /   0
0000060   1   2   3   4   5   6   7   8   9   :   ;   <   =   >   ?   @
0000100   A   B   C   D   E   F   G   H   I   J   K   L   M   N   O   P
0000120   Q   R   S   T   U   V   W   X   Y   Z   [   \   ]   ^   _   `
0000140   a   b   c   d   e   f   g   h   i   j   k   l   m   n   o   p
0000160   q   r   s   t   u   v   w   x   y   z   {   |   }   ~ 177 200
0000200 201 202 203 204 205 206 207 210 211 212 213 214 215 216 217 220
0000220 221 222 223 224 225 226 227 230 231 232 233 234 235 236 237 240
0000240 241 242 243 244 245 246 247 250 251 252 253 254 255 256 257 260
0000260 261 262 263 264 265 266 267 270 271 272 273 274 275 276 277 300
0000300 301 302 303 304 305 306 307 310 311 312 313 314 315 316 317 320
0000320 321 322 323 324 325 326 327 330 331 332 333 334 335 336 337 340
0000340 341 342 343 344 345 346 347 350 351 352 353 354 355 356 357 360
0000360 361 362 363 364 365 366 367 370 371 372 373 374 375 376 377  \n
0000400

0000000   -   -   -   -   -   -   -   -   -   -   -   -   -   -   -   -
0000020   =   =   =   =   =   =   =   =   =   =   =   =   =   =   =   =
0000040 001 002 003 004 005 006  \a  \b  \t  \n  \v  \f  \r 016 017 020
0000060 021 022 023 024 025 026 027 030 031 032 033 034 035 036 037    
0000100   !   "   #   $   %   &   '   (   )   *   +   ,   -   .   /   0
0000120   1   2   3   4   5   6   7   8   9   :   ;   <   =   >   ?   @
0000140   A   B   C   D   E   F   G   H   I   J   K   L   M   N   O   P
0000160   Q   R   S   T   U   V   W   X   Y   Z   [   \   ]   ^   _   `
0000200   a   b   c   d   e   f   g   h   i   j   k   l   m   n   o   p
0000220   q   r   s   t   u   v   w   x   y   z   {   |   }   ~ 177 200
0000240 201 202 203 204 205 206 207 210 211 212 213 214 215 216 217 220
0000260 221 222 223 224 225 226 227 230 231 232 233 234 235 236 237 240
0000300 241 242 243 244 245 246 247 250 251 252 253 254 255 256 257 260
0000320 261 262 263 264 265 266 267 270 271 272 273 274 275 276 277 300
0000340 301 302 303 304 305 306 307 310 311 312 313 314 315 316 317 320
0000360 321 322 323 324 325 326 327 330 331 332 333 334 335 336 337 340
0000400 341 342 343 344 345 346 347 350 351 352 353 354 355 356 357 360
0000420 361 362 363 364 365 366 367 370 371 372 373 374 375 376 377  \n
0000440

---------------================---------------
英文:

With bash, no external call is needed.
Its "pattern substitution" form of parameter expansion is powerful enough:

# generate a variable containing all legal 8-bit ASCII characters
# (I believe should work with utf, etc also, in appropriate locales)
all=$(perl -E &#39;say map chr,1..255&#39;)

# get an idea what it contains
echo &quot;$all&quot; | od -c
echo

# create variable containing two copies
var1=&quot;$all================$all&quot;

new=&#39;----------------&#39;

# replace first copy
echo &quot;${var1/&quot;$all&quot;/&quot;$new&quot;}&quot; | od -c
echo

# replace all copies
echo &quot;${var1//&quot;$all&quot;/&quot;$new&quot;}&quot;

output:

0000000 001 002 003 004 005 006  \a  \b  \t  \n  \v  \f  \r 016 017 020
0000020 021 022 023 024 025 026 027 030 031 032 033 034 035 036 037    
0000040   !   &quot;   #   $   %   &amp;   &#39;   (   )   *   +   ,   -   .   /   0
0000060   1   2   3   4   5   6   7   8   9   :   ;   &lt;   =   &gt;   ?   @
0000100   A   B   C   D   E   F   G   H   I   J   K   L   M   N   O   P
0000120   Q   R   S   T   U   V   W   X   Y   Z   [   \   ]   ^   _   `
0000140   a   b   c   d   e   f   g   h   i   j   k   l   m   n   o   p
0000160   q   r   s   t   u   v   w   x   y   z   {   |   }   ~ 177 200
0000200 201 202 203 204 205 206 207 210 211 212 213 214 215 216 217 220
0000220 221 222 223 224 225 226 227 230 231 232 233 234 235 236 237 240
0000240 241 242 243 244 245 246 247 250 251 252 253 254 255 256 257 260
0000260 261 262 263 264 265 266 267 270 271 272 273 274 275 276 277 300
0000300 301 302 303 304 305 306 307 310 311 312 313 314 315 316 317 320
0000320 321 322 323 324 325 326 327 330 331 332 333 334 335 336 337 340
0000340 341 342 343 344 345 346 347 350 351 352 353 354 355 356 357 360
0000360 361 362 363 364 365 366 367 370 371 372 373 374 375 376 377  \n
0000400

0000000   -   -   -   -   -   -   -   -   -   -   -   -   -   -   -   -
0000020   =   =   =   =   =   =   =   =   =   =   =   =   =   =   =   =
0000040 001 002 003 004 005 006  \a  \b  \t  \n  \v  \f  \r 016 017 020
0000060 021 022 023 024 025 026 027 030 031 032 033 034 035 036 037    
0000100   !   &quot;   #   $   %   &amp;   &#39;   (   )   *   +   ,   -   .   /   0
0000120   1   2   3   4   5   6   7   8   9   :   ;   &lt;   =   &gt;   ?   @
0000140   A   B   C   D   E   F   G   H   I   J   K   L   M   N   O   P
0000160   Q   R   S   T   U   V   W   X   Y   Z   [   \   ]   ^   _   `
0000200   a   b   c   d   e   f   g   h   i   j   k   l   m   n   o   p
0000220   q   r   s   t   u   v   w   x   y   z   {   |   }   ~ 177 200
0000240 201 202 203 204 205 206 207 210 211 212 213 214 215 216 217 220
0000260 221 222 223 224 225 226 227 230 231 232 233 234 235 236 237 240
0000300 241 242 243 244 245 246 247 250 251 252 253 254 255 256 257 260
0000320 261 262 263 264 265 266 267 270 271 272 273 274 275 276 277 300
0000340 301 302 303 304 305 306 307 310 311 312 313 314 315 316 317 320
0000360 321 322 323 324 325 326 327 330 331 332 333 334 335 336 337 340
0000400 341 342 343 344 345 346 347 350 351 352 353 354 355 356 357 360
0000420 361 362 363 364 365 366 367 370 371 372 373 374 375 376 377  \n
0000440

---------------================---------------

huangapple
  • 本文由 发表于 2023年5月11日 15:44:57
  • 转载请务必保留本文链接:https://go.coder-hub.com/76225207.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定