Unix/Linux Go Back    


Fedora is an operating system centered on the Linux open source kernel and is developed by the community-supported Fedora Project. Fedora is sponsored by Red Hat. Fedora contains software distributed under a free and open-source license. Fedora focuses on innovation, integrating and adopting new technologies and working closely with various Linux communities. The default desktop in Fedora is the GNOME desktop environment and the default user interface is the GNOME Shell. Other desktop environments include KDE Plasma, Xfce, LXDE, MATE and Cinnamon. Security is an important aspect of Fedora with one specific security feature, Security-Enhanced Linux, implementing a variety of software security policies, including mandatory access controls.

Use of regular expression

Fedora


Tags
awk, linux, regular expressions

Closed    
 
Thread Tools Search this Thread Display Modes
    #1  
Old Unix and Linux 12-07-2014
John_Light John_Light is offline
Registered User
 
Join Date: Dec 2014
Last Activity: 7 December 2014, 6:11 PM EST
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
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:

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

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

Code:
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":

Code:
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... Linux

Can someone perhaps help me?
And sorry for my very bad english Linux
Sponsored Links
    #2  
Old Unix and Linux 12-07-2014
Don Cragun's Unix or Linux Image
Don Cragun Don Cragun is offline Forum Staff  
Administrator
 
Join Date: Jul 2012
Last Activity: 20 September 2017, 7:56 PM EDT
Location: San Jose, CA, USA
Posts: 10,502
Thanks: 542
Thanked 3,675 Times in 3,135 Posts
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?
Sponsored Links
    #3  
Old Unix and Linux 12-07-2014
John_Light John_Light is offline
Registered User
 
Join Date: Dec 2014
Last Activity: 7 December 2014, 6:11 PM EST
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Thx for your answer Linux
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.
    #4  
Old Unix and Linux 12-07-2014
Don Cragun's Unix or Linux Image
Don Cragun Don Cragun is offline Forum Staff  
Administrator
 
Join Date: Jul 2012
Last Activity: 20 September 2017, 7:56 PM EDT
Location: San Jose, CA, USA
Posts: 10,502
Thanks: 542
Thanked 3,675 Times in 3,135 Posts
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:

Code:
#!/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).
Sponsored Links
Closed

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Linux More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
sed: -e expression #1, char 0: no previous regular expression InduInduIndu UNIX for Advanced & Expert Users 11 08-22-2012 01:13 PM
Perl: How to read from a file, do regular expression and then replace the found regular expression jessy83 Programming 1 12-05-2011 03:10 PM
Integer expression expected: with regular expression ketkee1985 Shell Programming and Scripting 3 03-14-2011 02:49 PM
Regular expression to extract "y" from "abc/x.y.z" .... i need regular expression rag84dec Linux 2 05-29-2008 04:05 AM
Regular Expression + Aritmetical Expression Z0mby Shell Programming and Scripting 2 05-21-2002 10:59 AM



All times are GMT -4. The time now is 04:54 AM.