# Use of regular expression

**URL:** <https://community.unix.com/t/use-of-regular-expression/348709>\
**Category:** Fedora\
**Created:** [December 7, 2014, 4:33pm UTC](https://community.unix.com/t/use-of-regular-expression/348709 "2014-12-07T16:33:10Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![John\_Light](https://community.unix.com/letter_avatar/john_light/32/6_449c533f586939b6e5f246632a46421f.png) [@John\_Light](https://community.unix.com/u/John_Light)\
**Post date:** [December 7, 2014, 4:33pm UTC](https://community.unix.com/t/use-of-regular-expression/348709/1 "2014-12-07T16:33:10Z")

</div>

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:

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

```

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

```nohighlight
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":

```nohighlight
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... ☹

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

---

<div class="post-metadata">

**Author:** ![Don\_Cragun](https://community.unix.com/user_avatar/community.unix.com/don_cragun/32/2682_2.png) [@Don\_Cragun](https://community.unix.com/u/Don_Cragun)\
**Post date:** [December 7, 2014, 5:03pm UTC](https://community.unix.com/t/use-of-regular-expression/348709/2 "2014-12-07T17:03:41Z")

</div>

Is this a homework assignment? If so, note that the homework can only be posted in a special forum following the rules noted [here](http://www.unix.com/homework-and-coursework-questions/113751-rules-homework-coursework-questions-forum.html).

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?

---

<div class="post-metadata">

**Author:** ![John\_Light](https://community.unix.com/letter_avatar/john_light/32/6_449c533f586939b6e5f246632a46421f.png) [@John\_Light](https://community.unix.com/u/John_Light)\
**Post date:** [December 7, 2014, 5:31pm UTC](https://community.unix.com/t/use-of-regular-expression/348709/3 "2014-12-07T17:31:51Z")

</div>

Thx for your answer 🙂  
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.

---

<div class="post-metadata">

**Author:** ![Don\_Cragun](https://community.unix.com/user_avatar/community.unix.com/don_cragun/32/2682_2.png) [@Don\_Cragun](https://community.unix.com/u/Don_Cragun)\
**Post date:** [December 7, 2014, 8:30pm UTC](https://community.unix.com/t/use-of-regular-expression/348709/4 "2014-12-07T20:30:07Z")

</div>

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:

```nohighlight
#!/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` ).
