1

I have an output like this:

a/foo bar /
b/c/foo sth /xyz
cc/bar ghj /axz/byz

What i want is just this line:

a/foo bar /

To be more clear, I want those line ending with a specific string. I want to grep lines that have a / character at their last column.

fedorqui
  • 275,237
  • 103
  • 548
  • 598
AloneInTheDark
  • 938
  • 4
  • 15
  • 36
  • What is your criteria for picking that line and not the others? – beny23 Feb 26 '14 at 15:03
  • Edited the question. I want a line, ends with some special pattern – AloneInTheDark Feb 26 '14 at 15:04
  • if the line is "a/foo bar bb/", do you want to show it? your requirement "I want to grep lines that has a "/" character at their last column" actually you have "/" character at last column of all three lines output you showed......may be your requirement can be corrected to more clear way? thanks – V-SHY Feb 26 '14 at 16:24
  • may be I misunderstood the definition of column.... Is all lines of your outputs 3 columns that delimited by whitespace? – V-SHY Feb 26 '14 at 16:47
  • maybe yes, maybe not. the important thing is, the line should end with "/" character. – AloneInTheDark Feb 26 '14 at 17:29

2 Answers2

10

You can use $ like this:

$ grep '/$' file
a/foo bar /

As $ stands for end of line, /$ matches those lines whose last character is a /.

fedorqui
  • 275,237
  • 103
  • 548
  • 598
2
grep '/$'

slash is not special character for grep and $ means match expression at the end of a line.


You can even grep the last column with only backlash at last column (but not the only column in the line)

I assumed tha the last column of a line is a string with more than one white space in front the string and no more character after the string. This assumption does not fulfill the requirement if there has only one column in that line because it does not need space in front of it to show it is last column if there has only one column.

By enable perl regular expressions (-P),

grep -P '\s+/$'

\s means matches any whitespace character (space, tab, newline)

plus sign means match 1 or more times for preceding element

$ means end of string

OR refer to Character Classes and Bracket Expressions

grep '[[:space:]]\+/$'

OR

grep '[[:blank:]]\+/$'

‘[:blank:]’ Blank characters: space and tab.

‘[:space:]’ Space characters: in the ‘C’ locale, this is tab, newline, vertical tab, form feed, carriage return, and space. It is a synonym for '\s'.

Refer to @fedorqui, the backslash after ]] is used to distinguish with the literal +. Thanks for the explanations.

Sorry if wrong for perl answer because I never use or learn Perl expression but really hope can help you find the last column slash so may be you can read these for more information for searching backspace with slash at end of line

grep with regexp: whitespace doesn't match unless I add an assertion

Regular expressions in Perl

Community
  • 1
  • 1
V-SHY
  • 3,925
  • 4
  • 31
  • 47
  • anyone can help me check if 2nd,3rd and 4th commands can get the last column with slash character or not? because I don't have any Linux machine nearby now and hunger to know the answer.thanks – V-SHY Feb 26 '14 at 15:58
  • 1
    Yes, all of them are working to me on `GNU bash, version 4.2.45`. – fedorqui Feb 26 '14 at 16:06
  • 1
    cool, I tried with the grep online tool but it gave negative result, by the way, thanks for your time to help me test. – V-SHY Feb 26 '14 at 16:09
  • Actually I have one thing not understand, about the Character Classes and Bracket Expressions answers.....why need to have backslash after ]] ? Is it possible to omit the backslash after ]]? I don't know its actual meaning in that expression. – V-SHY Feb 26 '14 at 16:13
  • 1
    Now I see that your attempts are checking if there is a slash *and* also if there is any number of spaces before. This is something the OP did not ask for. Regarding the `\` itself, it is to distinguish with the literal `+`. That is, if you don't add it, it will look for a `+/` ending. – fedorqui Feb 26 '14 at 16:16
  • because I understood "has a "/" character at their last column" as it has only '/' at the last column....so OP want "a/foo bar bb/" also his result? – V-SHY Feb 26 '14 at 16:21
  • if follow the requirement sentence for last column, all three lines of output are fulfilling the requirement...is it? – V-SHY Feb 26 '14 at 16:26
  • @fedorqui the + is for preceding element if not wrong,right? – V-SHY Feb 26 '14 at 16:50
  • It is OK, but in case you need to check for multiple spaces. Note that `[a]+` indicates "one or more values". – fedorqui Feb 26 '14 at 16:53