Change to capital first letter of every word over specific column
Hi guys,
Trying to change to upper case first letter of every word over a specific column. The source file is as follow: Code:
PRODUCT No.|SCIENCE BOOKS|DESCRIPTION Code:
PRODUCT No.,SCIENCE BOOKS,DESCRIPTION Code:
sed -e 's/.*/\L&/' -e 's/\<./\u&/g' file where: I´ve trying with AWK either with the next script: Code:
awk 'BEGIN{FS=OFS="|"} NR>1{$2=tolower($2);$2=gensub(/\<[A-Za-z]/,"X","g",$2)} {print $0} file' Maybe somebody could give a suggestion. Thanks in advance. |
Here's code that does the core of what you want but it
Code:
#!/bin/bash |
Hmm ... Well I am sure someone else would be able to work out where my slip up is, but the following will set ALL first letters to capital:
Code:
sed -r -e '2,$s/\|([^|]*)/\|\L\1/' -e 's@(\b[a-z])@\u\1@g' in.txt Code:
awk 'BEGIN{OFS=FS="|"} |
Well this one may be a little more readable:
Code:
awk 'BEGIN{OFS=FS="|"} |
Hi catlin,
Many thanks for your help, I tested your solution and works, the issue is the real files have 40 columns. But I´m certainly will try to learn from the regexp you used in sed commands!. Hi grail, Your awk solutions is work! thanks again for your help. The SED script work over complete line, not only over column 2. Well, your solutions come to me several questions like always, sorry :-) May you help me this doubts: I´ve recently learned that "\1", "\2".."\9" is the way to remember patterns, but: 1-) What does these parts in SED script mean 1.1) "sed -r -e '2,$s/..."? 1.2) "s@(.." and ..\1@g.. "? 1.3) It looks like is not possible say to SED that work over specific column considering a determined field separator, only using a regexp, right? 1.4) Do you know about some Unix style regexp (like use SED or AWK) tester to use on windows? 2) The first awk script works nice, but I´ve been trying without success to remove the last space character that is introduced in column 2 after finish the processing. 3) Similarly to 1st awk script, in the 2nd one, I´ve been trying without success to remove the extra "(" and ")" that is introduced surround the years. Many thanks in advance, thanks both guys. Regards, |
#!/bin/sh
while read line do echo $(echo "$line" | cut -d'|' -f1)'|'$(echo "$line" | cut -d'|' -f3 | convert first letter to uppercase)'|'$(echo "$line" | cut -d'|' -f3) done |
Quote:
1.2)Here you are confused by the "@" symbol? - if so, you can have pretty much any delimeter you like, the norm is 's///' where I have used s'@@@'. If I have lots of other slashes/sloshes ie "/" or "\" then I sometimes use this symbol 1.3)No it is possible, I just haven't worked out the kinks to get the capitalisation to also work within the delimetered boundary 2) I might need more information here as based on the example I have no extra spaces? 3) Sorry about that one, my bad ... this fixes it: Code:
awk 'BEGIN{OFS=FS="|"} |
Hi MTK358,
Thanks for your help, I´ve tried to execute your script, but just don´t know how, I put the "inpufile" name at the end with SCRIPT inputfile but doesn´t work. How is the way to run it? grail, Again and again thanks. Quote:
Quote:
Quote:
Quote:
Code:
. In your awk scripts, what does the "1" at the end mean? } Code:
awk '... ...}1' in.txt |
Quote:
last post should remedy this. Quote:
Code:
awk '0' inputfile As for MTK358's example: Quote:
Code:
done<inputfile |
Quote:
Your help is really appreciated. Best regards. |
My script was NOT a fully working program.
The part in bold had to be replaced with something I didn't know how to do, and it would be used like this: Code:
SCRIPT < file.txt |
All times are GMT -5. The time now is 12:32 AM. |