Difference between space and [[:space:]] in regular expression


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Difference between space and [[:space:]] in regular expression
# 1  
Old 07-25-2013
Difference between space and [[:space:]] in regular expression

May I know the difference between space in keyboard and [[:space:]] in regular expression

I entered the following
Code:
find . -type f -print | xargs grep -n 'dt=' | cut -d":" -f3 | sed 's/^ *dt=/dt=/g'
After "^" there is a space.
and the result is...
dt=`date +%Y%m%d%H%M%S`
        dt=`date +%Y%m%d`
dt=`date +%Y%m%d`
        dt=`date +%Y%m%d%H%M%S`
        dt=`date +%Y%m%d%H%M%S`
dt=`date +%Y%m%d%H%M%S`
        dt=`date +%Y%m%d%H%M%S`
        dt=`date +%Y%m%d%H%M%S`
        dt=`date +%Y%m%d%H%M%S`
dt=`date +%Y%m%d%H%M%S`
        dt=`date +%Y%m%d%H%M%S`
        dt=`date +%Y%m%d%H%M%S`

and when i used [[:space:]]

Code:
find . -type f -print | xargs grep -n 'dt=' | cut -d":" -f3 | sed 's/^[[:space:]]*dt=/dt=/g'
and the I got the expected result
dt=`date +%d%m%y_%H%M%S`
dt=`date +%d%m%y_%H%M%S`
dt=`date +%d%m%y_%H%M%S`
dt=`date +%d%m%y_%H%M%S`

---------- Post updated at 11:35 AM ---------- Previous update was at 11:31 AM ----------

By including space it did trimmed spaces for few of them.
# 2  
Old 07-25-2013
[[:space:]] means any whitespace, so it matches tabs as well as spaces.
These 2 Users Gave Thanks to Corona688 For This Post:
# 3  
Old 07-25-2013
Your sed is redundant by the way,
Code:
sed 's/^[[:space:]]*//g'

is all that's needed.

But whenever you find yourself doing stuff | cut | sed | grep | kitchen | sink, you might as well roll it up into one simple awk.

Code:
find . -type f -print | xargs awk -F: '/dt=/ { sub(/^[ \t]*/, "", $3); print $3 }'

# 4  
Old 07-25-2013
Actually character class [:blank:] matches spaces and tabs.

Character class [:space:] matches spaces and tabs as well as newline, vertical tab, form feed, carriage return.

For further reference:

Character Classes and Bracket Expressions

POSIX Bracket Expressions
These 2 Users Gave Thanks to Yoda For This Post:
# 5  
Old 07-25-2013
Quote:
Originally Posted by Yoda
Actually character class [:blank:] matches spaces and tabs.

Character class [:space:] matches spaces and tabs as well as newline, vertical tab, form feed, carriage return.
Which makes little difference in a usual regex, being that sed removes newlines before doing regex matching..
This User Gave Thanks to Corona688 For This Post:
# 6  
Old 07-25-2013
I always feel comfortable doing in bits as I do not understand awk quite well. I only use awk for printing or for simpler awk code. So I use combination of all grep, cut, etc...
Thanks for providing in awk.
Code:
find . -type f -print | xargs awk -F: '/dt=/ { sub(/^[ \t]*/, "", $3); print $3 }'

Got the following result
Code:
awk: Cannot find or open file nn.

 The input line number is 1. The file is nn.
 The source line number is 1.













awk: Cannot find or open file ./XX/XXXXXXXXXX.

 The input line number is 113. The file is ./XX/XXXXXXXX.
 The source line number is 1.

There is no filename with nn in my unix folders.
However if I use my code above posts, i get good results
# 7  
Old 07-25-2013
No matter what I do I cannot induce awk to do that, so I figure you have some very strange filenames fed into it, possibly ones with spaces or =. Or perhaps a typo in the awk section.

cut | sed | grep | kitchen | sink code is slow, is the thing. And half the threads we get here are 'why is my cat | grep | sed | kitchen | sink so slow', so I think it often does matter.
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Replacing Multiple spaces with a single space but excluding few regular expressions

Hi All. Attached are two files. I ran a query and have the output as in the file with name "FILEWITHFOURRECORDS.txt " I didn't want all the spaces between the columns so I squeezed the spaces with the "tr" command and also added a carriage return at the end of every line. But in two... (3 Replies)
Discussion started by: sparks
3 Replies

2. UNIX for Dummies Questions & Answers

Changing only the first space to a tab in a space delimited text file

Hi, I have a space delimited text file but I only want to change the first space to a tab and keep the rest of the spaces intact. How do I go about doing that? Thanks! (3 Replies)
Discussion started by: evelibertine
3 Replies

3. Fedora

Need to incrwase PHYSICAL VOLUME space on hard drive with free space on it

Hi, I run Fedora 17. I created a physical volume of 30GB on a disk with 60GB of space so there is 30GB of free space. On the physical volume, I created my volume group and logical volumes. I assigned all the space in the physical volume to my volume group. I need to add the 30GB of free space... (1 Reply)
Discussion started by: mojoman
1 Replies

4. Shell Programming and Scripting

ignore space regular expression

I just wrote a modsecurity rule that blocks execution on "cat /etc/passwd" from webshell. But when I use cat /etc/passwd it works. Ie when I add space after cat. What I need is a regular expression to ignore additional space than the first single space after cat. (2 Replies)
Discussion started by: anil510
2 Replies

5. Programming

Perl: How to read from a file, do regular expression and then replace the found regular expression

Hi all, How am I read a file, find the match regular expression and overwrite to the same files. open DESTINATION_FILE, "<tmptravl.dat" or die "tmptravl.dat"; open NEW_DESTINATION_FILE, ">new_tmptravl.dat" or die "new_tmptravl.dat"; while (<DESTINATION_FILE>) { # print... (1 Reply)
Discussion started by: jessy83
1 Replies

6. Shell Programming and Scripting

What's the difference between \d , [:digit:], and [0-9] in regular expression ?

Hello, $ ] && echo "ok" || echo "error"; error $ ]] && echo "ok" || echo "error"; error $ ]] && echo "ok" || echo "error"; ok $ It seems that \d , , and are not the same.According to the regular expression reference, \d , , and have the same meaning, which represent a digit, but why... (8 Replies)
Discussion started by: 915086731
8 Replies

7. Shell Programming and Scripting

Calculate total space, total used space and total free space in filesystem names matching keyword

Good afternoon! Im new at scripting and Im trying to write a script to calculate total space, total used space and total free space in filesystem names matching a keyword (in this one we will use keyword virginia). Please dont be mean or harsh, like I said Im new and trying my best. Scripting... (4 Replies)
Discussion started by: bigben1220
4 Replies

8. Red Hat

add lvm space from a regular partition

Hi, I have red hat enterprise 4. I would like to add more space on my lvm from the first partition that is not lvm type. Here's the config # fdisk -l Disk /dev/sda: 73.4 GB, 73406611456 bytes 255 heads, 63 sectors/track, 8924 cylinders Units = cylinders of 16065 * 512 = 8225280 bytes... (6 Replies)
Discussion started by: itik
6 Replies

9. UNIX for Advanced & Expert Users

wake up user space thread from kernel space ISR

Hello, I'm searching for a proper way to let the kernel space ISR(implemented in a kernel module) wake up a user space thread on a hardware interrupt. Except for sending a real-time signal, is it possible to use a semaphore? I've searched it on google, but it seems impossible to share a... (0 Replies)
Discussion started by: aaronwong
0 Replies

10. Shell Programming and Scripting

Regular Expression For Space

Hi all, I want a regular expression that will check for the space. I mean, it should accept everything but no space should be there in the variable. e.g. var1="aaaa" >>> OK var2='aaa vv' >> Not OK So it should not validate the second variable. Please help. Thanks in Advance.... (3 Replies)
Discussion started by: patelamit009
3 Replies
Login or Register to Ask a Question