I need to replace value with new_value in strings
key:value
key: value
key: value
To get
key:new_value
key: new_value
key: new_value
Number of spaces after : is arbitrary. The regex to match would be key: *value.
How do I replace value with new_value in such strings preserving number of spaces with sed?
To clarify, key1:value or key:value1 do not match due to different key/value, so such strings are not altered.
Try -
sed 's/^\(key: *\)value/\1new_vale/' file
You may use this sed to match all cases:
sed -E 's/^([[:blank:]]*key[[:blank:]]*:[[:blank:]]*)value([[:blank:]]|$)/\1new_value/' file
key:new_value
key: new_value
key: new_value
This allows 0 or more whitespaces at:
before key at start
between key and :
between : and value
after value before end
[[:blank:]] matches a space or a tab
Related
I have an integer field coming and I want to extract the first digit from the field, how can I do it. I cannot cast the field since the data is coming from a dataset, is there a way to extract first digit from the transformer stage in IBM datastage?
Example:
Input:
ABC = 1234
Output: 1
Can anyone please help me with the same?
Thanks!
Use a transformer, define a stage variable as varchar and use this formula to get the substring
ABC[1,1]
Alternatively you can also convert your numeric value by using the DecimalToString
You CAN convert to string within the context of your expression, and back again if the result needs to be an integer.
AsInteger(Left(ln_jn_ENCNTR_DTL.CCH,1)
This solution has used implicit conversion from integer to string. It assumes that the value of CCH is always an integer.
I would say- if ABC has type int, you can define a stage variable of type char having length 1.
then you need to convert Number to string first.And use Left function to extract the first char.
Left(DecimalToString(ABC),1).
If you are getting ABC as string, you can directly apply left function.
You can first define a stage variable (name say SV) of varchar type (to convert input integer column into varchar) :
Stage variable definition
Now assign the input integer column to stage variable SV and derive output integer column as AsInteger(SV[1,1]) : Column definition
i.e. input integer => (Type conversion to varchar) Stage variable => Substring[1,1] and Substring Conversion to Integer using AsInteger.
DecimalToString is an implicit conversion, so all you need is the Left() function. Left(MyString,1)
I have a field that is a string that I want to turn into Numerics. Problem is that the field can contain characters and it's hard for me to do a simple regular expression replace. Here is an example,
field1
1
1.5
2adfk3
2. Section 2
0'#%0ls23
3.987
What I essentially want is if the field can be natively casted as a Numeric, then return the Numeric, otherwise return NULL. For example, I want another field, to be
field2
1
1.5
NULL
NULL
NULL
3.987
where field2 is now a numeric. What is the best way to do this?
In Excel I suppose this would be closest to IFERROR
You can do this with a case expression and a regex:
select column1,
case
when column1 ~ '^[0-9\.]+$' then column1::numeric
else null
end as column2
from the_table
The regex checks if the contents of the column consists only of numbers and a dot. If that is the case, the value can be cast to numeric value, otherwise it returns null
String contains numeric and alphabetic data. what is the way to pick up only number? for example:
for the string "abc-123a-66" select should return "123"
You could use regexp_matches
CREATE table foo (
test VARCHAR);
INSERT INTO foo VALUES('abc-123a-66');
SELECT (regexp_matches(test, '\d+'))[1] FROM foo;
Example at SQLFiddle
In PostgreSQL this can be done with:
SELECT regexp_matches(regexp_replace(whatever_columnn,'\D*',''),'\d+') FROM whatever_table;
The first function (regexp_replace) deletes every non digit from the beginning of the string, the second (regexp_matches) extracts one or more occurrences of any digit from the output of the first function.
I need to remove non-numeric characters in a column (character varying) and keep numeric values in postgresql 9.3.5.
Examples:
1) "ggg" => ""
2) "3,0 kg" => "3,0"
3) "15 kg." => "15"
4) ...
There are a few problems, some values are like:
1) "2x3,25"
2) "96+109"
3) ...
These need to remain as is (i.e when containing non-numeric characters between numeric characters - do nothing).
Using regexp_replace is more simple:
# select regexp_replace('test1234test45abc', '[^0-9]+', '', 'g');
regexp_replace
----------------
123445
(1 row)
The ^ means not, so any character that is not in the range 0-9 will be replaced with an empty string, ''.
The 'g' is a flag that means all matches will be replaced, not just the first match.
For modifying strings in PostgreSQL take a look at The String functions and operators section of the documentation. Function substring(string from pattern) uses POSIX regular expressions for pattern matching and works well for removing different characters from your string.
(Note that the VALUES clause inside the parentheses is just to provide the example material and you can replace it any SELECT statement or table that provides the data):
SELECT substring(column1 from '(([0-9]+.*)*[0-9]+)'), column1 FROM
(VALUES
('ggg'),
('3,0 kg'),
('15 kg.'),
('2x3,25'),
('96+109')
) strings
The regular expression explained in parts:
[0-9]+ - string has at least one number, example: '789'
[0-9]+.* - string has at least one number followed by something, example: '12smth'
([0-9]+.\*)* - the string similar to the previous line zero or more times, example: '12smth22smth'
(([0-9]+.\*)*[0-9]+) - the string from the previous line zero or more times and at least one number at the end, example: '12smth22smth345'
Suppose I have a string like:
abc.efg.hijk.lmnop.leaf
I want the substring: abc.efg.hijk.lmnop.
Means: Find out the first comma . from right, then get the substring from left to this comma
How to use t-sql string function return the substring with one expresssion?
First your'll need to reverse the string and find the character index of the first period, then subtract this number from the length of the entire string. This value needs to be used at the length parameter of the sub-string function.
Try this:
DECLARE #S VARCHAR(55) = 'abc.efg.hijk.lmnop.leaf'
SELECT SUBSTRING(#S, 1, LEN(#S) - CHARINDEX('.', REVERSE(#S)))