Bash - Echo is splitting my string result [duplicate] - bash

This question already has answers here:
How to remove a newline from a string in Bash
(11 answers)
Closed 4 years ago.
I have the following:
VERSION=$(curl -Is https://qa.me.com.br | sed -n '/^x-powered-by:/Ip' | sed '/x-powered-by:/I s/x-powered-by: //Ig')
Expected variable result (but it has one more character that broke my result):
MEWeb - QA - 267_4_2548
After, I'm showing by the following:
echo "##teamcity[progressMessage 'Version is $VERSION']"
Expected (without '*'):
*##teamcity[progressMessage 'Version is MEWeb - QA - 267_4_2548']
Actual:
']##teamcity[progressMessage 'Version is MEWeb - QA - 267_4_2548
I don't know what is breaking my result.
Thanks for help and sorry for my english!

Add
| tr -d '\r'
to the end of the curl command (just before the ")").

The response has a carriage return.
When you get the VERSION, put it in a file.
echo $VERSION > test.txt
Now, to see the hidden characters, use:
cat -v test.txt
You'll see:
MEWeb - QA - 267_4_2548**^M**
You need to handle that character, which is causing the trouble.

Related

How to remove last n character from string in shell script [duplicate]

This question already has answers here:
Extract filename and extension in Bash
(38 answers)
Closed 21 days ago.
I have a variable reponame in Shell Script(bash) holding a string
echo $reponame
"testrepo.git"
I want to remove the last 4 character of this string and assign the result to a new variable repo
echo $repo
"testrepo"
How can I do this?
If I understand correctly, you want to get rid of the .git extension. Then the correct expression would be
repo=${reponame%.*}
or
repo=${reponame%.git}
for that very specific case.
For substrings in general, the expression removing last 4 characters would go like
repo=${reponame:0:-4}
Very nice resource on Bash string operations:
https://tldp.org/LDP/abs/html/string-manipulation.html
For shell in general you might use various approaches such as
repo=$(echo -n "$reponame" | sed 's/\.git$//')
or
repo=$(echo -n "$reponame" | rev | cut -f 2- -d '.' | rev)

How to add double quotation marks ("") around pipe-separated fields [duplicate]

This question already has answers here:
How can I add quotation marks to fields in a CSV file?
(4 answers)
Closed 2 years ago.
The community reviewed whether to reopen this question 1 year ago and left it closed:
Original close reason(s) were not resolved
Let's say I have a file with this structure:
1|2|3|4|
5|6|7|8|
9|10|11|12|
However, I want my file to look like this (expected output):
"1"|"2"|"3"|"4"|
"5"|"6"|"7"|"8"|
"9"|"10"|"11"|"12"|
I am trying to used sed command in the following way:
sed 's/^/"/g'
Unfortunately, it only adds quotation marks at the beginning of each line:
"1|2|3|4|
"5|6|7|8|
"9|10|11|12|
^ means "the beginning of a line". Use [^|] instead which means "anything but |". If your implementation of sed supports +, you can use
sed -E 's/[^|]+/"&"/g'
otherwise, you need to be more verbose
sed 's/[^|][^|]*/"&"/g'
& represents the matched part.
You can use
sed -E 's/[^|]+/"&"/g' file > newfile
The -E option enables the POSIX ERE syntax and [^|]+ thus matches one or more chars other than |, and "&" replaces each with its copy enclosed with " on both sides.
See the online sed demo:
s='1|2|3|4|
5|6|7|8|
9|10|11|12|'
sed -E 's/[^|]+/"&"/g' <<< "$s"
Output:
"1"|"2"|"3"|"4"|
"5"|"6"|"7"|"8"|
"9"|"10"|"11"|"12"|
Here is a gnu awk way of doing the same:
awk -v RS="[|\n]+" '{ORS=RT; print "\"" $0 "\""}' file
"1"|"2"|"3"|"4"|
"5"|"6"|"7"|"8"|
"9"|"10"|"11"|"12"|

How to truncate extraneous output using shell script? [duplicate]

This question already has answers here:
How to insert strings containing slashes with sed? [duplicate]
(11 answers)
Why it's not possible to use regex to parse HTML/XML: a formal explanation in layman's terms
(10 answers)
Closed 3 years ago.
I am trying to eliminate everything before and after the JSON contained in a specific part of a webpage so I can send that to a PHP script. I've tried a number of ways to get rid of the container content but all of them so far have failed, including one method that has worked in the exact same syntax for related purposes:
The characters that are between the two asterisks (**) at the beginning and end I need removed:
**var songs = [**{"timestamp":1555176393000,"title":"Enter Sandman","trackId":"ba_5cbb546d-5c1c-490e-9908-761b89dd5166","artist":"Metallica","artistId":"52_65f4f0c5-ef9e-490c-aee3-909e7ae6b2ab","album":"Metallica","albumId":"d0_6e729716-c0eb-3f50-a740-96ac173be50d","npe_id":"3cc5fe24d0ffcbb9152d861f27ae801660"},{"timestamp":1555176702000,"title":"Start Me Up","trackId":"76_d0b86399-11e5-4d11-b4fe-ce4b3f9a4736","artist":"The Rolling Stones","artistId":"1b_b071f9fa-14b0-4217-8e97-eb41da73f598","album":"Tattoo You","albumId":"d1_778b345b-e8a1-4054-b5ba-c611d3fda421","npe_id":"f0dc0ab12ef99a6e0087cad12886509b7b"},{"timestamp":1555176909000,"title":"Fame","trackId":"4e_cdef4b88-7314-431a-9cdd-d457296a65b7","artist":"David Bowie","artistId":"ab_5441c29d-3602-4898-b1a1-b77fa23b8e50","album":"Best of Bowie","albumId":"21_3709ee5a-d087-370f-afb4-f730092c7a94","npe_id":"2b8b3a170baa77125891d72a0474d3343a"},{"timestamp":1555177158000,"title":"Rocket","trackId":"34_aa5b9053-849e-4788-972f-7941303175b6","artist":"Def Leppard","artistId":"c1_7249b899-8db8-43e7-9e6e-22f1e736024e","album":"Hysteria","albumId":"06_de5cf055-d875-41f8-9261-89b11b7ff145","npe_id":"0d87b580f140a85feaebc7d77f75db2a3d"},{"timestamp":1555177826000,"title":"Mama, I'm Coming Home","trackId":"cb_e5b09171-9527-4d24-8ab6-1e922fdd66d3","artist":"Ozzy Osbourne","artistId":"4b_8aa5b65a-5b3c-4029-92bf-47a544356934","album":"No More Tears","albumId":"66_8f3d5a65-036c-3260-b9bb-36f1d0d80c11","npe_id":"6b766464fe945f275bf478192dcd33cfdc"},{"timestamp":1555178076000,"title":"Gold Dust Woman","trackId":"a4_ef8c1eca-f344-4bfb-82ea-763aa8aeaad9","artist":"Fleetwood Mac","artistId":"66_bd13909f-1c29-4c27-a874-d4aaf27c5b1a","album":"2010-01-08: The Rock Boat X, Lido Deck, Carnival Inspiration","albumId":"80_4f229af0-2afc-431d-87ff-f7f6af66268e","npe_id":"f6417d98fd1fefcca227d82a8ac9b84197"},{"timestamp":1555178363000,"title":"With or Without You","trackId":"79_6b9a509f-6907-4a6e-9345-2f12da09ba4b","artist":"U2","artistId":"26_a3cb23fc-acd3-4ce0-8f36-1e5aa6a18432","album":"The Joshua Tree","albumId":"0c_d287c703-5c25-3181-85d4-4d8c1a7d8ecd","npe_id":"23b19420196b28e2156ecda87c11b882e0"},{"timestamp":1555178654000,"title":"Who Are You","trackId":"7d_431b9746-c6ec-489d-9199-c83676171ae8","artist":"The Who","artistId":"22_f2fa2f0c-b6d7-4d09-be35-910c110bb342","album":"Who Are You","albumId":"40_b255da2c-6583-35f9-95e3-ef5f9c14e868","npe_id":"e01896f74f24968bb7727eaafbf6250b8f"},{"timestamp":1555179031000,"title":"Authority Song","trackId":"31_f5ff19f7-95f3-4a22-8996-3788c264e0b8","artist":"John Mellencamp","artistId":"4d_0aad6b52-fd93-4ea4-9c5d-1f66e1bc9f0a","album":"Words & Music: John Mellencamp's Greatest Hits","albumId":"9e_1240c510-7015-4484-baac-ce17f5277ea1","npe_id":"244785e3b1d75effb9fdecbb6df76b009f"},{"timestamp":1555179256000,"title":"Touch Me","trackId":"9d_1dd1f86c-2120-45f3-ac9f-3c87257fe414","artist":"The Doors","artistId":"13_9efff43b-3b29-4082-824e-bc82f646f93d","album":"The Soft Parade","albumId":"db_c29d7552-b5df-42b8-aae7-03d1e250cb3a","npe_id":"1b5d155eb2eeee6fc1fdb50a94b100669c"}]**; <ol class="songs tracks"></ol>**
Here is the shell script which produces the above at present:
#!/bin/sh
curl -v --silent http://player.listenlive.co/41851/en/songhistory >/var/tmp/wklh$1.a.txt
pta=`cat /var/tmp/wklh$1.a.txt | grep songs > /var/tmp/wklh$1.b.txt`
ptb=`cat /var/tmp/wklh$1.b.txt | sed -n -e '/var songs = /,/; <span title/ p' > /var/tmp/wklh$1.c.txt`
ptc=`cat /var/tmp/wklh$1.c.txt | grep songs > /var/tmp/wklh$1.d.txt`
#ptd=`cat /var/tmp/wklh$1.d.txt | sed -i 's/var songs = [//g' /var/tmp/wklh$1.d.txt`
#ptd=`cat /var/tmp/wklh$1.d.txt | sed -i 's/}]; <ol class="songs tracks"></ol>//g' /var/tmp/wklh$1.d.txt`
json=`cat /var/tmp/wklh$1.d.txt`
echo $json
metadata=`php /etc/asterisk/scripts/music/wklh.php $json`
echo $metadata
The commented out lines are what I was trying to use to remove the extraneous content, since it is predictable every time. However, when uncommented, I get the following errors:
sed: -e expression #1, char 18: unterminated `s' command
sed: -e expression #1, char 38: unknown option to `s'
I've examined my sed statement, but I can't find any discrepancies between how I use it here and in other working shell scripts.
Is there actually a syntax error here (or unallowed characters)? Or is there a better way I can do this?
Your shell script has serious issues.
The syntax
variable=`commands`
takes the output of commands and assigns it to variable. But in every case, you are redirecting all output to a file; so the variable will always be empty.
Unless you need the temporary files for reasons which are not revealed in your question (such as maybe being able to check how many bytes of output you got in each temporary file for a monitoring report, or something like that), a pipeline would be much superior.
#!/bin/sh
curl -v --silent http://player.listenlive.co/41851/en/songhistory |
grep songs |
sed -n -e '/var songs = /,/; <span title/ p' |
grep songs |
php /etc/asterisk/scripts/music/wklh.php
This also does away with the useless uses of cat and the useless uses of echo and so also coincidentally removes the quoting errors. The grep x | sed -n 's/y/z/p' is a useless use of grep which can easily be refactored to sed -n '/x/s/y/z/p'
Square brackets are special to sed. Simply escape them.
s/var songs = \[//g
If you use slash / as the regex delimiter, it becomes special. Either escape it or use a different delimiter.
s/}]; <ol class="songs tracks"><\/ol>//g
s|}]; <ol class="songs tracks"></ol>||g
if your data in 'd' file, try gnu sed,
sed -Ez 's/^\*\*[^\*]+\*\*(.+)]\*\*[^\*]+\*\*\s*$/\1/' d
remove last ] too, to correctly balance the Json

Using grep to filter real time output of a process? If so, how to get the line after a match? [duplicate]

This question already has answers here:
How to show only next line after the matched one?
(14 answers)
grep: show lines surrounding each match
(14 answers)
Read from a endless pipe bash [duplicate]
(1 answer)
Closed 4 years ago.
Should I use grep to filter a real time output? I'm not sure if this is what I should use for a real time output.
Example: command -option | grep --color 'string1\|string2'
If so, how to get also the lines after string1 and string2?
As #shellter mentioned, from man grep:
-A num, --after-context=num
Print num lines of trailing context after each match. See also the -B and -C options.
so you would use command -option | grep -A 1 --color 'string1\|string2' to print matched lines and the line right after them.
There are plenty of other options in the manual for grep, and most other command-line programs, so I suggest getting used to running man cmd as a quick first check.

Can I replace hours in sed command with variable? [Case Closed] [duplicate]

This question already has answers here:
Replace a string in shell script using a variable
(12 answers)
Closed 6 years ago.
I'm trying to get a certain timestamps (in my case every 15 minute) in logfile using sed command in bash scripting.
My question is, can I replace the hours in the command with a variable?
This is script that I want:
#!/bin/bash
hour=`date +%H` #Current Hours e.g 14:00
sed -n '/$hour:15:00/,/$hour:30:00/p' file.log | nice grep Event
The result will print the logfile from 14:15:00 until 14:30:00. But there's a problem when the range is from 45 minute to 60 minute which is 14:45 - 15:00. Is there any solution for this?
UPDATE
This issue is already solved, the command below work for me.
sed -n "/${hour}:15:00/,/${hour}:30:00/p" file.log | nice grep Event
Other reference: Replace a string in shell script using a variable
Thank you.
== Case closed ==
Well, to answer the question - yes, you would take the variable out of the quotes and then it should use the value:
sed -n '/'$hour:15:00/,/'$hour':30:00/p' file.log | nice grep Event
You could also just use double quotes around the expression
sed -n "/${hour}:15:00/,/${hour}:30:00/p" file.log | nice grep Event

Resources