LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 05-05-2010, 05:14 PM   #1
Drigo
Member
 
Registered: Jun 2009
Posts: 111

Rep: Reputation: 17
Angry Regular Expressions in Batch Script


Well...I am pretty new at this topic but I would like to learn it from example.

The first thing I am working on is to modify the command date to be shown as DD/MM/YY only using regular expression but I dont know how to combine what there is in the regexp tutorials online and the syntax for batch scripting. Any help?

Here is what I what.

run a file test: ~# ./test

Where file test is:
#!/bin/bash
#

DATE=$( date )
echo $DATE


#Here i want to echo the date but in format DD/MM/YY using regular expressions.
echo ${DATE/_[1-9]}??????????



THANKS IN ADVANCE. Also if you can point me to good regexp tutorials (directed towards batch scripting), that will be great.
 
Old 05-05-2010, 05:25 PM   #2
smoker
Senior Member
 
Registered: Oct 2004
Distribution: Fedora Core 4, 12, 13, 14, 15, 17
Posts: 2,279

Rep: Reputation: 249Reputation: 249Reputation: 249
The term is bash scripting (or shell scripting)
Code:
date=$(date +%d/%m/%y)

echo $date
or just

Code:
date +%d/%m/%y
from a command line.

man date

http://tldp.org/LDP/abs/html/

Last edited by smoker; 05-05-2010 at 05:27 PM.
 
Old 05-05-2010, 05:34 PM   #3
pixellany
LQ Veteran
 
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Arch/XFCE
Posts: 17,802

Rep: Reputation: 738Reputation: 738Reputation: 738Reputation: 738Reputation: 738Reputation: 738Reputation: 738
And that of course is not regular expressions...

Regular expressions (regexes) are used to match some portion of the contents of a data stream---not for formatting output data.

A simple example is that "." matches any character. Thus "grep '.' filename" will return every line of a file. By contrast: "grep '[0-9]' filename" returns every line containing at least one number.

Good tutorials here:
http://www.grymoire.com/Unix/
and here:
http://tldp.org (get the Bash Guide for Beginners)
 
Old 05-05-2010, 05:53 PM   #4
Drigo
Member
 
Registered: Jun 2009
Posts: 111

Original Poster
Rep: Reputation: 17
Thanks to all! I have not looked at your replied but any small detailed explanation (if needed) will be great!
 
Old 05-05-2010, 05:53 PM   #5
Drigo
Member
 
Registered: Jun 2009
Posts: 111

Original Poster
Rep: Reputation: 17
regards!
 
Old 05-05-2010, 05:59 PM   #6
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Debian sid + kde 3.5 & 4.4
Posts: 6,823

Rep: Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957
Bash itself doesn't really have a full regex feature anyway. It has a basic level of functionality that's mostly there for pattern matching in tests and file globbing. It's not really designed to do direct string reformatting like the above. There are no \1, \2, etc. storage buffers, for example.

You could probably use it to extract each portion of your input date string into separate variables, which you could then use to build the desired output string. But for real regex operations you really need to use sed or awk.
 
Old 05-05-2010, 06:56 PM   #7
pixellany
LQ Veteran
 
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Arch/XFCE
Posts: 17,802

Rep: Reputation: 738Reputation: 738Reputation: 738Reputation: 738Reputation: 738Reputation: 738Reputation: 738
Quote:
Originally Posted by Drigo View Post
Thanks to all! I have not looked at your replied but any small detailed explanation (if needed) will be great!
???
 
Old 05-05-2010, 10:11 PM   #8
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Centos 6.8, Centos 5.10
Posts: 17,240

Rep: Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324
You may find these useful
http://tldp.org/LDP/Bash-Beginners-G...l/chap_04.html
http://tldp.org/LDP/abs/html/regexp.html
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
\{a,b\} regular expressions sycamorex Linux - General 10 10-18-2008 07:38 PM
Regular expressions in script elmu Programming 1 11-28-2005 09:51 AM
Regular Expressions markjuggles Programming 2 05-05-2005 12:39 PM
shell script and regular expressions Ripper951 Linux - Newbie 7 01-07-2003 11:38 PM


All times are GMT -5. The time now is 10:53 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration