Home > front end >  How to pass backslash in variable in sed bash
How to pass backslash in variable in sed bash

Time:03-14

I have to edit a file to insert a variable containing backslashes () before a pattern match. The variable looks is something of the following sort:

0.0 0.0 0.0 \ \n 0.0 0.0 0.0 \ \n 0.0 0.0 0.0 \ \n

I am using sed to do this but I replace the usual s/// with s|||. So my command looks like:

sed -i "s|&END COORD|$a|g" run.inp

However the terminal still throws up a "unterminated `s' command" error.

I also tried escaping the variable with double backslashes, like:

0.0 0.0 0.0 \\ \\n

and so on, while using the regular s///. It still gave the same error.

Is there any other way I can do this. The variable needs to have a \ after every few numbers so I cannot change that.

Edit 1:

Interestingly, I did the same thing in a different file, by passing the backslashes escaped (so, using \\ in the variable), and it worked then! I have compared the two variables and they look exactly the same. The difference is that the one that worked was created inside the bash script by using a loop. The one now that doesn't work is the output of a command.

Edit 2:

Output of echo -E "$a"

17.023322524 18.1919433361 16.7144688195 \
16.3350559948 18.1520636911 17.7229592516 \
16.7244811297 16.7607812783 15.7447914535 \
15.1017368963 14.8607125118 14.4740577353 \
15.2015325696 14.5412214298 14.8098902788 \
15.5739160958 15.5619535434 13.502862893 \
13.9433508753 13.6522693741 13.4166565775 \
14.71938316 13.9310651101 14.1544706437 \
14.7531181899 14.8483729843 14.0851576176 \
14.1910703196 13.4631303391 14.1838136787 \
14.1860377739 15.6054459999 15.7094194115 \
14.6058405281 13.9768905159 14.605584991 \
14.428802429 14.6557292167 14.6753000064 \
14.3394492496 14.7393679038 14.4875569626 \
14.8204260046 14.4101334474 15.8329111383 \
24.4745323247 24.828100518 24.1880417519 \
25.2121296924 25.7806616148 25.5104228145 \
26.286329053 25.6051319794 24.9917789133 \
24.2288521309 24.4688873723 24.6498149122 \
24.683508781 24.2728488306 24.1967217135 \
24.2249333351 23.2772767748 23.6382832608 \
24.0145202936 23.2177286813 22.4240995143 \
22.3562484938 22.1468875482 22.3180692847 \
23.3161196112 23.918220609 24.2847234659 \
25.6088866529 24.3008207898 24.6986317195 \
26.0487217592 26.5993515956 26.6142225131 \
25.8826332654 26.4443429652 26.7693582413 \
25.7280203032 25.6095852746 24.6107161732 \
25.4457014389 24.7195000368 25.5341853997 \
25.4940511126 24.7564155167 24.6021493905 \
23.9953708765 24.9111072126 25.0027613085

Output of xxd <<< "$a"

00000000: 3135 2e34 3730 3735 3430 3939 3420 3135  15.4707540994 15
00000010: 2e39 3733 3036 3934 3632 3320 3136 2e34  .9730694623 16.4
00000020: 3639 3830 3335 3336 3420 5c0a 3137 2e30  698035364 \.17.0
00000030: 3233 3332 3235 3234 2031 382e 3139 3139  23322524 18.1919
00000040: 3433 3333 3631 2031 362e 3731 3434 3638  433361 16.714468
00000050: 3831 3935 205c 0a31 362e 3333 3530 3535  8195 \.16.335055
00000060: 3939 3438 2031 382e 3135 3230 3633 3639  9948 18.15206369
00000070: 3131 2031 372e 3732 3239 3539 3235 3136  11 17.7229592516
00000080: 205c 0a31 362e 3732 3434 3831 3132 3937   \.16.7244811297
00000090: 2031 362e 3736 3037 3831 3237 3833 2031   16.7607812783 1
000000a0: 352e 3734 3437 3931 3435 3335 205c 0a31  5.7447914535 \.1
000000b0: 352e 3130 3137 3336 3839 3633 2031 342e  5.1017368963 14.
000000c0: 3836 3037 3132 3531 3138 2031 342e 3437  8607125118 14.47
000000d0: 3430 3537 3733 3533 205c 0a31 352e 3230  40577353 \.15.20
000000e0: 3135 3332 3536 3936 2031 342e 3534 3132  15325696 14.5412
000000f0: 3231 3432 3938 2031 342e 3830 3938 3930  214298 14.809890
00000100: 3237 3838 205c 0a31 352e 3537 3339 3136  2788 \.15.573916
00000110: 3039 3538 2031 352e 3536 3139 3533 3534  0958 15.56195354
00000120: 3334 2031 332e 3530 3238 3632 3839 3320  34 13.502862893 
00000130: 5c0a 3133 2e39 3433 3335 3038 3735 3320  \.13.9433508753 
00000140: 3133 2e36 3532 3236 3933 3734 3120 3133  13.6522693741 13
00000150: 2e34 3136 3635 3635 3737 3520 5c0a 3134  .4166565775 \.14
00000160: 2e37 3139 3338 3331 3620 3133 2e39 3331  .71938316 13.931
00000170: 3036 3531 3130 3120 3134 2e31 3534 3437  0651101 14.15447
00000180: 3036 3433 3720 5c0a 3134 2e37 3533 3131  06437 \.14.75311
00000190: 3831 3839 3920 3134 2e38 3438 3337 3239  81899 14.8483729
000001a0: 3834 3320 3134 2e30 3835 3135 3736 3137  843 14.085157617
000001b0: 3620 5c0a 3134 2e31 3931 3037 3033 3139  6 \.14.191070319
000001c0: 3620 3133 2e34 3633 3133 3033 3339 3120  6 13.4631303391 
000001d0: 3134 2e31 3833 3831 3336 3738 3720 5c0a  14.1838136787 \.
000001e0: 3134 2e31 3836 3033 3737 3733 3920 3135  14.1860377739 15
000001f0: 2e36 3035 3434 3539 3939 3920 3135 2e37  .6054459999 15.7
00000200: 3039 3431 3934 3131 3520 5c0a 3134 2e36  094194115 \.14.6
00000210: 3035 3834 3035 3238 3120 3133 2e39 3736  058405281 13.976
00000220: 3839 3035 3135 3920 3134 2e36 3035 3538  8905159 14.60558
00000230: 3439 3931 205c 0a31 342e 3432 3838 3032  4991 \.14.428802
00000240: 3432 3920 3134 2e36 3535 3732 3932 3136  429 14.655729216
00000250: 3720 3134 2e36 3735 3330 3030 3036 3420  7 14.6753000064 
00000260: 5c0a 3134 2e33 3339 3434 3932 3439 3620  \.14.3394492496 
00000270: 3134 2e37 3339 3336 3739 3033 3820 3134  14.7393679038 14
00000280: 2e34 3837 3535 3639 3632 3620 5c0a 3134  .4875569626 \.14
00000290: 2e38 3230 3432 3630 3034 3620 3134 2e34  .8204260046 14.4
000002a0: 3130 3133 3334 3437 3420 3135 2e38 3332  101334474 15.832
000002b0: 3931 3131 3338 3320 5c0a 3234 2e34 3734  9111383 \.24.474
000002c0: 3533 3233 3234 3720 3234 2e38 3238 3130  5323247 24.82810
000002d0: 3035 3138 2032 342e 3138 3830 3431 3735  0518 24.18804175
000002e0: 3139 205c 0a32 352e 3231 3231 3239 3639  19 \.25.21212969
000002f0: 3234 2032 352e 3738 3036 3631 3631 3438  24 25.7806616148
00000300: 2032 352e 3531 3034 3232 3831 3435 205c   25.5104228145 \
00000310: 0a32 362e 3238 3633 3239 3035 3320 3235  .26.286329053 25
00000320: 2e36 3035 3133 3139 3739 3420 3234 2e39  .6051319794 24.9
00000330: 3931 3737 3839 3133 3320 5c0a 3234 2e32  917789133 \.24.2
00000340: 3238 3835 3231 3330 3920 3234 2e34 3638  288521309 24.468
00000350: 3838 3733 3732 3320 3234 2e36 3439 3831  8873723 24.64981
00000360: 3439 3132 3220 5c0a 3234 2e36 3833 3530  49122 \.24.68350
00000370: 3837 3831 2032 342e 3237 3238 3438 3833  8781 24.27284883
00000380: 3036 2032 342e 3139 3637 3231 3731 3335  06 24.1967217135
00000390: 205c 0a32 342e 3232 3439 3333 3333 3531   \.24.2249333351
000003a0: 2032 332e 3237 3732 3736 3737 3438 2032   23.2772767748 2
000003b0: 332e 3633 3832 3833 3236 3038 205c 0a32  3.6382832608 \.2
000003c0: 342e 3031 3435 3230 3239 3336 2032 332e  4.0145202936 23.
000003d0: 3231 3737 3238 3638 3133 2032 322e 3432  2177286813 22.42
000003e0: 3430 3939 3531 3433 205c 0a32 322e 3335  40995143 \.22.35
000003f0: 3632 3438 3439 3338 2032 322e 3134 3638  62484938 22.1468
00000400: 3837 3534 3832 2032 322e 3331 3830 3639  875482 22.318069
00000410: 3238 3437 205c 0a32 332e 3331 3631 3139  2847 \.23.316119
00000420: 3631 3132 2032 332e 3931 3832 3230 3630  6112 23.91822060
00000430: 3920 3234 2e32 3834 3732 3334 3635 3920  9 24.2847234659 
00000440: 5c0a 3235 2e36 3038 3838 3636 3532 3920  \.25.6088866529 
00000450: 3234 2e33 3030 3832 3037 3839 3820 3234  24.3008207898 24
00000460: 2e36 3938 3633 3137 3139 3520 5c0a 3236  .6986317195 \.26
00000470: 2e30 3438 3732 3137 3539 3220 3236 2e35  .0487217592 26.5
00000480: 3939 3335 3135 3935 3620 3236 2e36 3134  993515956 26.614
00000490: 3232 3235 3133 3120 5c0a 3235 2e38 3832  2225131 \.25.882
000004a0: 3633 3332 3635 3420 3236 2e34 3434 3334  6332654 26.44434
000004b0: 3239 3635 3220 3236 2e37 3639 3335 3832  29652 26.7693582
000004c0: 3431 3320 5c0a 3235 2e37 3238 3032 3033  413 \.25.7280203
000004d0: 3033 3220 3235 2e36 3039 3538 3532 3734  032 25.609585274
000004e0: 3620 3234 2e36 3130 3731 3631 3733 3220  6 24.6107161732 
000004f0: 5c0a 3235 2e34 3435 3730 3134 3338 3920  \.25.4457014389 
00000500: 3234 2e37 3139 3530 3030 3336 3820 3235  24.7195000368 25
00000510: 2e35 3334 3138 3533 3939 3720 5c0a 3235  .5341853997 \.25
00000520: 2e34 3934 3035 3131 3132 3620 3234 2e37  .4940511126 24.7
00000530: 3536 3431 3535 3136 3720 3234 2e36 3032  564155167 24.602
00000540: 3134 3933 3930 3520 5c0a 3233 2e39 3935  1493905 \.23.995
00000550: 3337 3038 3736 3520 3234 2e39 3131 3130  3708765 24.91110
00000560: 3732 3132 3620 3235 2e30 3032 3736 3133  72126 25.0027613
00000570: 3038 350a                                085.

CodePudding user response:

Thanks to the hint by user1934428, I figured the issue out.

As I mentioned in my first edit, the variable is generated by using the output of a python print command. This leads to a hidden new line character, because of how python prints out its lists.

I just changed the python code to print the list out without line breaks, and voila! My sed command works. I have used the s/// version and just escaped all the \ in the variable.

Thanks a lot!

  • Related