How to remove double spaces in string using sed - bash

How to remove the spaces in 22th and 23th position of the specific string in a file?
Example below and expected outcome.
XXXSA3FFESS3052599004 L +
Expected result will be:
XXXS3DFFESS3052599004L +
My code seemd inaccurate as below. Please advise.
sed 's/[A-Z0-9] //g' $file
Truly appreciate your help. I‘m not sure exactly the code. But my basic understanding is that sed could be able to do this.

echo 'XXXSA3FFESS3052599004 L +' | sed 's/ *//'
Output:
XXXSA3FFESS3052599004L +
See: The Stack Overflow Regular Expressions FAQ

or like this:
echo 'XXXSA3FFESS3052599004 L +' | sed 's/.//22;s/.//22'

If your spaces are in a fixed position, maybe it would make more sense to use cut:
echo 'XXXSA3FFESS3052599004 L +' | cut -c1-21,24-
or
echo 'XXXSA3FFESS3052599004 L +' | cut --complement -c22,23

Related

Using sed to replace some lower case letters to upper case

I'm trying to replace _[lowercase] to [uppercase] using sed in bash.
So far I've tried this code:
new_arr=$( echo $old_arr | sed -e 's%_\(.\)%\1\U%g' )
With input of
this_is_a_function()
i expected the output to be
thisIsAFunction()
but i got
thisisafunction
Do you have a suggestion for what I might be doing wrong?
Could you please try following.
sed 's/_\([a-z]\)/\U\1/g' Input_file
So in OP's case it should be something like:
new_arr=$( echo "$old_arr" | sed 's%_\([a-z]\)%\U\1%g' )

Extract values from a property file using bash

I have a variable which contains key/values separated by space:
echo $PROPERTY
server_geo=BOS db.jdbc_url=jdbc\:mysql\://mysql-test.com\:3306/db02 db.name=db02 db.hostname=/mysql-test.com datasource.class.xa=com.mysql.jdbc.jdbc2.optional.MysqlXADataSource server_uid=BOS_mysql57 hibernate33.dialect=org.hibernate.dialect.MySQL5InnoDBDialect hibernate.connection.username=db02 server_labels=mysql57,mysql5,mysql db.jdbc_class=com.mysql.jdbc.Driver db.schema=db02 hibernate.connection.driver_class=com.mysql.jdbc.Driver uuid=a19ua19 db.primary_label=mysql57 db.port=3306 server_label_primary=mysql57 hibernate.dialect=org.hibernate.dialect.MySQL5InnoDBDialect
I'd need to extract the values of the single keys, for example db.jdbc_url.
Using one code snippet I've found:
echo $PROPERTY | sed -e 's/ db.jdbc_url=\(\S*\).*/\1/g'
but that returns also other properties found before my key.
Any help how to fix it ?
Thanks
If db.name always follow db.jdbc_url, then use grep lookaround,
$ echo "${PROPERTY}" | grep -oP '(?<=db.jdbc_url=).*(?=db.name)'
jdbc\:mysql\://mysql-test.com\:3306/db02
or add the VAR to an array,
$ myarr=($(echo $PROPERTY))
$ echo "${myarr[1]}" | grep -oP '(?<=db.jdbc_url=).*(?=$)'
jdbc\:mysql\://mysql-test.com\:3306/db02
This is caused because you are using the substitute command (sed s/.../.../), so any text before your regex is kept as is. Using .* before db\.jdbc_url along with the begin (^) / end ($) of string marks makes you match the whole content of the variable.
In order to be totaly safe, your regex should be :
sed -e 's/^.*db\.jdbc_url=\(\S*\).*$/\1/g'
You can use grep for this, like so:
echo $PROPERTY | grep -oE "db.jdbc_url=\S+" | cut -d'=' -f2
The regex is very close to the one you used with sed.
The -o option is used to print the matched parts of the matching line.
Edit: if you want only the value, cut on the '='
Edit 2: egrep say it is deprecated, so use grep -oE instead, same result. Just to cover all bases :-)

Parsing Numbers from a File and using those to calculate

I seem to be to stupid to parse some HTML Files with Bash. We have some files which have lines like:
var A4_total = 2018 + 4730;
var Other1_total = 3242 + 3828;
(They tell us how many pages the Printers have printed).
I need to calculate the first two Values together (2018 and 3242). My Approach is:
hilfsvar1=$(echo `grep -F var\ A4_total StatCntMedia.htm | sed 's/var\ A4\_total\ \=\ //g'` | sed -ne "s/^[^=]\++//p" | sed 's/;//g'); hilfsvar2=$(echo `grep -F var\ Other1_total StatCntMedia.htm | sed 's/var\ Other1\_total\ \=\ //g'` | sed -ne "s/^[^=]\++//p" | sed 's/;//g'); echo "$hilfsvar1 + $hilfsvar2" | bc
This will fail. The two variables do have the right content:
[User]# echo $hilfsvar1
4730
[User]# echo $hilfsvar2
3828
But this is where I can't get forward:
[User]# echo "$hilfsvar1 + $hilfsvar2"
+ 3828
(Sorry for my scripting, I don't have deeper knowlede of Script languages :) ) - I would be happy to resolve this in another way if someone does have a solution.
Thanks in advance, Jonas
It sounds like this might be what you're looking for:
$ awk '{sum[4]+=$4; sum[6]+=$6} END{print sum[4], sum[6]}' file
5260 8558
If not, update your question to show expected output.
It seems like you're probably on completely the wrong track though and trying to do somethign in shell that should be done entirely in awk.
Try something like:
awk '/A4_total|Other1_total/ {print $4+$6}' StatCntMedia.htm
Output:
6748
7070
search for either A4_total or Other1_total, add 4th and 6th field separated by space and display the output.

Get string between strings in bash

I want to get the string between <sometag param=' and '>
I tried to use the method from Get any string between 2 string and assign a variable in bash to get the "x":
echo "<sometag param='x'><irrelevant stuff='nonsense'>" | tr "'" _ | sed -n 's/.*<sometag param=_\(.*\)_>.*/\1/p'
The problem (apart from low efficiency because I just cannot manage to escape the apostrophe correctly for sed) is that sed matches the maximum, i.e. the output is:
x_><irrelevant stuff=_nonsense
but the correct output would be the minimum-match, in this example just "x"
Thanks for your help
You are probably looking for something like this:
sed -n "s/.*<sometag param='\([^']*\)'>.*/\1/p"
Test:
echo "<sometag param='x'><irrelevant stuff='nonsense'>" | sed -n "s/.*<sometag param='\([^']*\)'>.*/\1/p"
Results:
x
Explanation:
Instead of a greedy capture, use a non-greedy capture like: [^']* which means match anything except ' any number of times. To make the pattern stick, this is followed by: '>.
You can also use double quotes so that you don't need to escape the single quotes. If you wanted to escape the single quotes, you'd do this:
-
... | sed -n 's/.*<sometag param='\''\([^'\'']*\)'\''>.*/\1/p'
Notice how that the single quotes aren't really escaped. The sed expression is stopped, an escaped single quote is inserted and the sed expression is re-opened. Think of it like a four character escape sequence.
Personally, I'd use GNU grep. It would make for a slightly shorter solution. Run like:
... | grep -oP "(?<=<sometag param=').*?(?='>)"
Test:
echo "<sometag param='x'><irrelevant stuff='nonsense'>" | grep -oP "(?<=<sometag param=').*?(?='>)"
Results:
x
You don't have to assemble regexes in those cases, you can just use ' as the field separator
in="<sometag param='x'><irrelevant stuff='nonsense'>"
IFS="'" read x whatiwant y <<< "$in" # bash
echo "$whatiwant"
awk -F\' '{print $2}' <<< "$in" # awk

Find 1st Letter of every word in a string

How would I find the first letter of a word contained within a string using bash.
For example
Code:
str="my-custom-string'
I would want to find m,c,s. I know how to find the very first letter, but this is slightly more complicated.
Many thanks,
$ echo 'my-custom-string' | egrep -o '\b\w'
m
c
s
Pure Bash using parameter substitution. Remove minus, select first character of each word:
str="my-custom-string"
for word in ${str//-/ }; do
echo "${word:0:1}"
done
Result
m
c
s
Here's a sed version:
echo 'my-custom-string' | sed 's/\(^\|-\)\(.\)[^-]*/\2\n/g'
This might work for you (GNU sed);
echo 'my-custom-string' | sed 's/\B.//g;y/-/,/'
m,c,s
or:
echo 'my-custom-string' | sed 's/\B.//g;y/-/\n/'
m
c
s

Resources