Use of regular expression

Hi,
I need some help.

My task is, to write a "one-line" command, which must use ls and awk.
Task: Write a command-line, which should rename all files in dir from form "value1.dok" to "value2.doc". And value2=value1+1.

For example:

ls | awk -F: '{print "mv "$0" "$1+1".doc"}' | sh

But this doesn't work with a filename with characters. Therefore i tested this:

ls | awk -F: '{print "mv "$0" value"substr($1 ,6)+1".doc"}' | sh

This worked, but my taskprogram say "wrong" and want see a other command (perhaps with different string?)
So i tested it with regular expressions, but my console say always "syntax error":

ls | awk -F: '{print "mv "$0" "${1%[!0-9]*}""${1%[0-9]*}+1".doc"}' | sh

I havent any idea how i could it bring to work in one line... :frowning:

Can someone perhaps help me?
And sorry for my very bad english :o

Is this a homework assignment? If so, note that the homework can only be posted in a special forum following the rules noted here.

If it is not a homework assignment, why is a one-liner a requirement (most computer professionals prefer readable, maintainable code)?
Why are ls and awk requirements when this can all (except for the mv ) be done with just shell built-ins?
What shell and OS (including version) are you using?

Thx for your answer :slight_smile:
And no, its only a exercise (optional) with a program, to prepare for comming next year linux-course.

And I use Fedora(20) 64bit with KDE and bash.

I don't understand why you were using -F: in your awk scripts when there aren't any colons in your filenames. Here are a couple of ways to do it; one using ls , awk , rm , and bash ; and the other one just using bash and rm . Both are presented in readable format and in 1-liner format:

#!/bin/bash
echo 'ls | awk | bash:'
ls | awk '
/[0-9][.]dok$/ {
	match($0, "[0-9]*[.]dok$")
	printf("echo mv \"%s\" \"%s%s.doc\"\n", $0, substr($0, 1, RSTART-1),
		substr($0, RSTART, length($0) - RSTART - 3) + 1)
}' | bash
printf '\n\nbash/ksh built-ins:\n'
for f in *[0-9].dok
do	b=${f%.dok}
	s=${b##*[!0-9]}
	b=${b%%[[0-9]*}
	echo mv "$f" "$b$((s+1)).doc"
done
printf '\n\nls | awk | bash 1-liner:\n'
ls|awk '/[0-9][.]dok$/{match($0,"[0-9]*[.]dok$");printf("echo mv \"%s\" \"%s%s.doc\"\n",$0,substr($0,1,RSTART-1),substr($0,RSTART,length($0)-RSTART-3)+1)}'|bash
printf '\n\nbash/ksh built-ins 1-liner:\n'
for f in *[0-9].dok;do	b=${f%.dok};s=${b##*[!0-9]};b=${b%%[[0-9]*};echo mv "$f" "$b$((s+1)).doc";done

If you find one that prints the mv commands you want to execute, remove the echo shown in red to actually rename the files.

The parameter expansion extracting the sequence number from the pathname works as long as there is only one string of digits in the filenames being processed (just before the .dok ). The ERE in the awk script doesn't have this limitation, but your description didn't indicate that there could be multiple strings of digits; so I assume both methods do what you need.

Neither of these will do what you want if there are leading zeros on the digit strings in your filenames (such as value0012.dok ).