1

Not a duplicate question, as I am asking for awk specifically.

I have the following command to convert everything to lowercase, and then reprint out substrings in a certain order.

cat words |tr '[:upper:]' '[:lower:]' |awk '{ print substr($1,1,1)$2."texttoappend"}'

It works perfectly, but I can't help but think it would be cleaner if I could do it all within awk. Is this possible?

John Sanders
  • 71
  • 1
  • 7
  • http://stackoverflow.com/questions/14021899/can-i-use-awk-to-convert-all-the-lower-case-letters-into-upper-case – muru Nov 21 '15 at 20:18
  • 2
    Possible duplicate of [bash - how to uppercase the command line argument?](http://unix.stackexchange.com/questions/51983/bash-how-to-uppercase-the-command-line-argument) – Thomas Dickey Nov 21 '15 at 20:38
  • This isn't a duplicate. I'm not asking to use bash or any solution, I'm asking specifically to use awk. @muru when I try to use tolower on a substr I get an error, can they be used together? – John Sanders Nov 21 '15 at 20:40
  • 4
    `echo FOO bar | awk '{print tolower(substr($1,1,1)$2)}'` works fine for me. – muru Nov 21 '15 at 20:41
  • 1
    Which version of awk? – muru Nov 21 '15 at 20:41
  • @muru erm...nevermind, working for me now also. – John Sanders Nov 21 '15 at 21:07
  • @muru, I found the problem I think, when attempting to append text I get an error about a runaway string constant. I'm trying { print tolower(substr($1,1,1) $2)."texttoappend"}' -- I'm finding it hard to get the version of awk, but it is whatever shipped with ubuntu recently. – John Sanders Nov 22 '15 at 19:33

1 Answers1

2

If you want everything to be in lower text, this should work

echo FIELD1 FIELD2 | awk '{print tolower(substr($1,1,1)$2".""TextToAppend")}'

output is ffield2.texttoappend

If you want texttoappend as it is, this should work

echo FIELD1 FIELD2 | awk '{print tolower(substr($1,1,1)$2)".""TextToAppend"}'

output is ffield2.TextToAppend

don_crissti
  • 79,330
  • 30
  • 216
  • 245