Find the answer to your Linux question:
Results 1 to 5 of 5
Enjoy an ad free experience by logging in. Not a member yet? Register.
  1. #1

    find string in a list of files

    hello all..
    Could anyone help me on this stuff ?

    i have a directory with a bounch of files.. every file has a lot of text and i need to grab a part like this. customName: sdfasdfff@country

    line 1 bla la bla bla bla
    bla la bla bla bla
    customName: sdfasdfff@country fasdf
    bla la bla bla bla
    bla la bla bla bla
    -- END OF FILE --

    wich is the best way to do this?

    thanks in advance,

  2. #2
    sed -n -e '/^customName: sdfasdfff@country fasdf$/p' *

    or maybe

    grep -h "^customName: sdfasdfff@country fasdf$" *

  3. #3
    thanks that work.
    i only have one more little question if you don't mind.

    I dont have much expertise on regex so could you point me on how to grab only the text after customName til end of line

    for instance:
    text sample: customName: sdfasdfff@country fasdf

    grep -h "^customName: [grabStuff *.* ] $" *

    i only need this return sdfasdfff@country

    best regards,
    and thank so much for your help.
    really appreciated.


  4. $spacer_open
  5. #4
    Try cut, the delimiter is a space and print the second field.
    Check the man page.


  6. #5
    I'm not sure what you want from your last message.

    Given target lines of
    customName: sdfasdfff@country fasdf

    If you only want the address (neither "customName: " nor " fasdf")
    sed -n -e 's/^customName: *\(.*@.*\) *.*$/\1/p' *
    Note there are exactly 2 spaces then the * in BOTH cases above.

    If you want sdfasdfff@country fasdf
    sed -n -e 's/^customName: *//p' *
    Note there are exactly 2 spaces before the *

    If you want to have some fun, there are (at least) 2 solutions using awk:
    awk '/^customName: / { print $2, $3 }' *
    awk '/^customName: / { print $2 }' *

    The comma in the first awk (after $2) gives you a space between fields.
    print $2 $3 will jam the fields together, which you do not want.

    Note the final * in all the examples is the list of files you wish to scan.

    sed is generally much faster than awk. The best book I've ever read (several times over the years) is the O'Reilly book, "sed & awk" by Dale Dougherty and Arnold Robbins.

    O'Reilly Media -- Bookstore: sed & awk, Second Edition

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts