Convert specific column of file into upper case in unix (without using awk and sed)
You can use tr
to transform from lowercase to uppercase. cut
will extract the single columns and paste
will combine the separated columns again.
Assumption: Columns are delimited by tabs.
paste <(cut -f1 file) <(cut -f2 file | tr '[:lower:]' '[:upper:]')
Replace file
with your file name (that is test
in your case).
Can I use awk to convert all the lower-case letters into upper-case?
Try this:
awk '{ print toupper($0) }' <<< "your string"
Using a file:
awk '{ print toupper($0) }' yourfile.txt
Changing the case of a string with awk
The issue is that \B
(zero-width non-word boundary) only seems to match at the beginning of the line, so $1
works but $2
and following fields do not match the regex, so they are not substituted and remain uppercase. Not sure why \B
doesn't match except for the first field... B should match anywhere within any word:
echo 'ABCD EFGH IJKL MNOP' | awk '{for (i=1; i<=NF; ++i) { print match($i, /\B/); }}'
2 # \B matches ABCD at 2nd character as expected
0 # no match for EFGH
0 # no match for IJKL
0 # no match for MNOP
Anyway to achieve your result (capitalize only the first character of the line), you can operate on $0
(the whole line) instead of using a for
loop:
echo 'ABCD EFGH IJKL MNOP' | awk '{print toupper(substr($0,1,1)) tolower(substr($0,2)) }'
Or if you still wanted to capitalize each word separately but with awk
only:
awk '{for (i=1; i<=NF; ++i) { $i=toupper(substr($i,1,1)) tolower(substr($i,2)); } print }'
SED- Change column from lower to upper case based on 2 conditions from script
sed
is not the right tool for this. You should consider awk
.
You may use a awk
solution like this:
awk 'BEGIN {FS=OFS=","} NR > 1 && $8 ~ /^(shooting|judo)$/ {
$2 = toupper($2)} 1' file.csv
id,name,nationality,sex,date_of_birth,height,weight,sport,gold,silver,bronze,info
388896171,ABDELRAOUF BENGUIT,ALG,male,1985-07-03,1.83,90,judo,0,0,0,
285603057,Abderrahmane Mansouri,ALG,male,1995-01-13,1.72,66,cycling,0,0,0,
545134894,Abderrahmane Meziane,ALG,male,1994-03-07,1.68,62,football,0,0,0,
969824503,ABDULLAH ALRASHIDI,IOA,male,1963-08-21,1.83,84,shooting,0,0,1,
897549624,ABDULLAH HEL BAKI,BAN,male,1989-08-01,,,shooting,0,0,0,
153457,Abdullahi Shehu,NGR,male,1993-03-12,1.70,,football,0,0,1,
To save changes inline to input file use this gnu-awk
solution:
awk -i inplace 'BEGIN {FS=OFS=","}
NR > 1 && $8 ~ /^(shooting|judo)$/ {$2 = toupper($2)} 1' file.csv
Or if you don't have gnu-awk
then use:
awk 'BEGIN {FS=OFS=","} NR > 1 && $8 ~ /^(shooting|judo)$/ {
$2 = toupper($2)} 1' file.csv > _tmp_ && mv _tmp_ file.csv
This gnu-sed
solution should work for you:
sed -E 's/^([^,]*,)([^,]+)((,[^,]*){5},(shooting|judo),)/\1\U\2\L\3/' file.csv
id,name,nationality,sex,date_of_birth,height,weight,sport,gold,silver,bronze,info
388896171,ABDELRAOUF BENGUIT,alg,male,1985-07-03,1.83,90,judo,0,0,0,
285603057,Abderrahmane Mansouri,ALG,male,1995-01-13,1.72,66,cycling,0,0,0,
545134894,Abderrahmane Meziane,ALG,male,1994-03-07,1.68,62,football,0,0,0,
969824503,ABDULLAH ALRASHIDI,ioa,male,1963-08-21,1.83,84,shooting,0,0,1,
897549624,ABDULLAH HEL BAKI,ban,male,1989-08-01,,,shooting,0,0,0,
153457,Abdullahi Shehu,NGR,male,1993-03-12,1.70,,football,0,0,1,
Working Demo
How to lower case part of file with awk?
why the right hand side of the equal side isn't converted to lower case?
tolower(string)
function does
Return a copy of string, with each uppercase character in the string
replaced with its corresponding lowercase character. Nonalphabetic
characters are left unchanged.
So you need to assign it back to get visible effect, i.e. $2=tolower($2)
rather than just tolower($2)
and also set FS to OFS to avoid replacing =
with
i.e.
'BEGIN{FS=OFS="="}/^private/{gsub(/[-/]/, "_");$2=tolower($2)} 1'
Related Topics
How to Save the Output of This Awk Command to File
Debugging Shared Libraries with Gdbserver
How Much Does Using Htaccess Files Slow Down Website Performance (Especially with Solid State Disks)
Docker Behind Proxy That Changes Ssl Certificate
How to Develop Opengl Es (Gles) 2.0 Applications on Linux
Getting Current Path in Variable and Using It
How to Toggle Cr/Lf in Gnu Screen
Sed Help: Matching and Replacing a Literal "\N" (Not the Newline)
Check If a Program in a Specific Path Is Running
Prohibit Unaligned Memory Accesses on X86/X86_64
Use a C Library in Swift on Linux
Getting "I Won't Open a Connection To" When Connecting to Ftp Server from Google Compute Engine
Bash: Head & Tail Behavior with Bash Script
Where Does Output of Print in Kernel Go
How to Forward Localhost Port on My Container to Localhost on My Host
Maintain File and Folder Permissions Inside Archives
How Is the Address of the Text Section of a Pie Executable Determined in Linux