Search code examples
bashawk

awk Getting ALL line but last field with the delimiters


I have to make a one-liner that renames all files in the current directory that end in ".hola" to ".txt".

For example:

sample.hola and name.hi.hola will be renamed to sample.txt and name.hi.txt respectively

I was thinking about something like:

ls -1 *.hola | awk '{NF="";print "$0.hola $0.txt"}' (*)

And then passing the stdin to xargs mv -T with a |

But the output of (*) for the example would be sample and name hi.

How do I get the output name.hi for name.hi.hola using awk?


Solution

  • Why would you want to involve awk in this?

    $ for f in *.hola; do echo mv "$f" "${f%hola}txt"; done
    mv name.hi.hola name.hi.txt
    mv sample.hola sample.txt
    

    Remove the echo when you're happy with the output.