LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 10-01-2014, 12:51 AM   #1
micyew
LQ Newbie
 
Registered: Jan 2011
Posts: 21

Rep: Reputation: 0
grep leading space in text file


Hi,

Need some advice from some regex gurus...

I've this text file and would like to grep the line with 3 leading spaces...How would I do it?

I tried grep "^\s[3]" a.txt but fail....Tried grep "^[:space]{3}" a.txt also fail...

Any idea??

cat <<< 3 leading spaces
cat
cat
cat <<< 3 leading spaces
cat
cat
 
Old 10-01-2014, 01:01 AM   #2
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 21,840

Rep: Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308
would be nice to read about regexps. But this one is quite easy:
Code:
grep '^   '     ( that is a ^ and 3 spaces ). 
      ....
Why do you want to make it difficult?
 
Old 10-01-2014, 01:09 AM   #3
astrogeek
Moderator
 
Registered: Oct 2008
Distribution: Slackware [64]-X.{0|1|2|37|-current} ::12<=X<=15, FreeBSD_12{.0|.1}
Posts: 6,263
Blog Entries: 24

Rep: Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194
Quote:
Originally Posted by micyew View Post
Hi,

Need some advice from some regex gurus...

I've this text file and would like to grep the line with 3 leading spaces...How would I do it?

I tried grep "^\s[3]" a.txt but fail....Tried grep "^[:space]{3}" a.txt also fail...
If that is the only criteria, how about:

Code:
 grep '^   ' a.txt
...where there are three spaces after the caret?

*** pan64 types faster than I do!

Last edited by astrogeek; 10-01-2014 at 01:11 AM.
 
Old 10-01-2014, 01:09 AM   #4
syg00
LQ Veteran
 
Registered: Aug 2003
Location: Australia
Distribution: Lots ...
Posts: 21,126

Rep: Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120
Read the manpage.
Character classes need two square brackets - also pays to try "grep -E ..." when regex seems to be mis-behaving.
 
Old 10-01-2014, 01:21 AM   #5
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 21,840

Rep: Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308
Quote:
Originally Posted by astrogeek View Post
If that is the only criteria, how about:

Code:
 grep '^   ' a.txt
...where there are three spaces after the caret?

*** pan64 types faster than I do!
(or less...)

Otherwise
Code:
grep -E '^\s{3}'
grep -E '^[[:space:]]{3}'
should work too. you can try online regexp checkers, nowadays they also explain that expression
 
Old 10-01-2014, 04:49 PM   #6
micyew
LQ Newbie
 
Registered: Jan 2011
Posts: 21

Original Poster
Rep: Reputation: 0
Hi all,

thanks for the response. ...I did try all recommendation. ..but it out all 'cat', not the 3 leading space cat....Did you try out? If yes could cut/paste thr result so that I can see the difference. ...thanks. ..
 
Old 10-01-2014, 05:55 PM   #7
micyew
LQ Newbie
 
Registered: Jan 2011
Posts: 21

Original Poster
Rep: Reputation: 0
Hi all,

Only this works....Thanks alot for the hints....

grep -E '^[[:space:]]{3}cat' a.txt
 
Old 10-02-2014, 12:34 AM   #8
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 21,840

Rep: Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308
next time please use [code]here comes your code[/code] to keep formatting. I have just understood your first post.
From the other hand:
Code:
grep -E '^   cat'
grep -E '^\s{3}cat'
grep -E '^[[:space:]]{3}cat'
all should work - at least works for me worked. Probably you mistyped something or there is no space but something else....?

If you really want to say thanks just press YES.
 
  


Reply



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
[SOLVED] Replace text lines identified by leading text in line within multiple files juergen852 Linux - Newbie 9 09-21-2014 04:54 PM
grep text from remote log file, enter text and rename on remote server vanish78 Linux - Newbie 1 07-30-2012 05:32 PM
grep a text in files and print the file name who don't contain such text whossa Linux - Newbie 5 04-13-2012 07:49 AM
How can I grep text from file? @ngelot Linux - Software 6 06-13-2007 04:44 AM
Using grep, etc to modify a text file dangerousdave Linux - Newbie 7 02-23-2005 02:49 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 03:33 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
Open Source Consulting | Domain Registration