Find the answer to your Linux question:
Results 1 to 4 of 4
Hey, I know the title was too long but basically, what I'm trying to do is take a specific string from a text file and print the strings to a ...
Enjoy an ad free experience by logging in. Not a member yet? Register.
  1. #1
    Just Joined!
    Join Date
    Dec 2008
    Posts
    35

    Searching .txt file for (specific) strings and printing them to new file


    Hey, I know the title was too long but basically, what I'm trying to do is take a specific string from a text file and print the strings to a seperate file and on a new line. May seem confusing but here's an example.

    Name: Bob, PhoneNumber: *****, MobileNumber: *****, Email: ***@**.com,

    And I have a file with this type of formatting; if I was using the above example, I would like to take only the Emails from the file and print them to a completely seperate file by > seperate-file.txt but the problem is, I don't know how to go about that.

    I tried using grep to search for a string of "***@***.com" but that would only print the entire line including Name, PhoneNumber, etcetera and after reading the man pages, found out that it's for whole lines.

    I was wondering if anybody knew of a command that I could do this with? It's beginning to give me a bit of a headache.

    Thanks for any help in advanced.

  2. #2
    Trusted Penguin Irithori's Avatar
    Join Date
    May 2009
    Location
    Munich
    Posts
    3,356
    Code:
    cut -d "," -f4 original_file > new_file
    This assumes
    - comma is not used in the "datafields", only as a delimiter
    - commas are set, even if the datafield(s) should be empty
    You must always face the curtain with a bow.

  3. #3
    Trusted Penguin Irithori's Avatar
    Join Date
    May 2009
    Location
    Munich
    Posts
    3,356
    P.S.:
    Writing "Name", "PhoneNumber", etc in every line is a waste of space.
    But even worse, it makes parsing harder.
    If you -for example- want to send emails to all these Email: fields, you would have to strip the "Email:" part first.
    You must always face the curtain with a bow.

  4. #4
    Just Joined!
    Join Date
    Dec 2008
    Posts
    35
    Great, I've managed to do it now. Thank you for your help.

Posting Permissions

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