Assume a multi-line text file file
in which some lines start with whitespaces.
$ cat filefoo Baz baz QUX QUx QuuxBaZ QuxBazaaR
Further assume that I wish to convert all those lines that start with a keyword (e.g. "baz") to lowercase letters, irrespective if (a) that keyword is written in lower- or uppercase letters (or any combination thereof) itself, and (b) that keyword is preceeded by whitespaces.
$ cat file | sought_commandfoo Baz # not to lowercase (line does not start with keyword) baz qux # to lowercase QUx Quuxbaz qux # to lowercaseBazaaR # not to lowercase (line does not start with keyword, but merely with a word containing the keyword)
I believe that awk is the tool to do it, but I am uncertain how to implement the case-insensitivity for the keyword matching.
$ cat file | awk '{ if($1 ~ /^ *baz/) print tolower($0); else print $0}'foo Baz baz qux QUx QuuxBaZ Qux # ERROR HERE: was not replaced, b/c keyword not recognized.BazaaR
EDIT 1:Adding IGNORECASE=1
appears to resolve the case-insensitivity, but now incorrectly converts the last line to lowercase.
$ cat file | awk '{IGNORECASE=1; if($1~/^ *baz/) print tolower($0); else print $0}'foo Baz baz qux QUx Quuxbaz quxbazaar # ERROR HERE: should not be converted to lowercase, as keyword not present (emphasis on word!).
I have a dataframe like the one displayed below:
# Create an example dataframe about a fictional armyraw_data = {'regiment': ['Nighthawks', 'Nighthawks', 'Nighthawks', 'Nighthawks'], 'company': ['1st', '1st', '2nd', '2nd'], 'deaths': ['kkk', 52, '25', 616], 'battles': [5, '42', 2, 2], 'size': ['l', 'll', 'l', 'm']}df = pd.DataFrame(raw_data, columns = ['regiment', 'company', 'deaths', 'battles', 'size'])
My goal is to transform every single string inside of the dataframe to upper case so that it looks like this:
Notice: all data types are objects and must not be changed; the output must contain all objects. I want to avoid to convert every single column one by one... I would like to do it generally over the whole dataframe possibly.
What I tried so far is to do this but without success
df.str.upper()
Is there a function built into Java that capitalizes the first character of each word in a String, and does not affect the others?
Examples:
jon skeet
-> Jon Skeet
miles o'Brien
-> Miles O'Brien
(B remains capital, this rules out Title Case)old mcdonald
-> Old Mcdonald
**(Old McDonald
would be find too, but I don't expect it to be THAT smart.)
A quick look at the Java String Documentation reveals only toUpperCase()
and toLowerCase()
, which of course do not provide the desired behavior. Naturally, Google results are dominated by those two functions. It seems like a wheel that must have been invented already, so it couldn't hurt to ask so I can use it in the future.
I have to rename a complete folder tree recursively so that no uppercase letter appears anywhere (it's C++ sourcecode, but that shouldn't matter). Bonus points for ignoring CVS and SVN control files/folders. Preferred way would be a shell script, since shell should be available at any Linux box.
There were some valid arguments about details of the file renaming.
I think files with same lowercase names should be overwritten, it's the user's problem. When checked out on a case-ignoring file system would overwrite the first one with the latter, too.
I would consider A-Z characters and transform them to a-z, everything else is just calling for problems (at least with source code).
The script would be needed to run a build on a Linux system, so I think changes to CVS or SVN control files should be omitted. After all, it's just a scratch checkout. Maybe an "export" is more appropriate.
I have a database table with a code
column that uses a lowercase index to prevent code values that only differ in case (e.g. 'XYZ' = 'xYZ' = 'xyz'). The typical way in Postgresql is to create a function based index, like this: CREATE UNIQUE INDEX mytable_lower_code_idx ON mytable (lower(code))
.
Now I have a case where I need upsert behaviour on that column:
-- first insertINSERT INTO mytable (code) VALUES ('abcd');-- second insert, with upsert behaviourINSERT INTO mytable (code) VALUES ('Abcd') ON CONFLICT (code) DO UPDATE SET code='Abcd';
For the second insert I get a unique key violation: ERROR: duplicate key value violates unique constraint "mytable_lower_code_idx"
(I also tried to use ON CONFLICT ON CONSTRAINT mytable_lower_code_idx
but Postgresql tells me that this constraint does not exist so maybe it doesn't treat the index as a constraint.)
My final question: Is there any way to make INSERT ... ON CONFLICT work together with indexes on expressions? Or must I introduce a physical indexed lowercase column to accomplish the task?
Please note that by viewing our site you agree to our use of cookies (see Bảo mật for details). You will only see this message once.