Results 1 to 5 of 5

Thread: Code to only keep words that are 7 characters or less and delete all others?

  1. #1
    Join Date
    Feb 2008
    Location
    Munster, Ireland
    Beans
    2,467
    Distro
    Ubuntu Mate

    Question Code to only keep words that are 7 characters or less and delete all others?

    Hi.

    Can someone give me code that will take a text file with many words and only keep those that are 7 characters or less and delete all others?

    Thanks.
    1st Distro used (live CD): Knoppix in early 2007 ¦ 1st Distro Installed: Ubuntu 7.10 in Feb 2008
    GNU/Linux User #470660 – Ubuntu User #28226
    Isaac Asimov: "I do not fear computers. I fear the lack of them."

  2. #2
    Join Date
    Dec 2009
    Beans
    195

    Re: Code to only keep words that are 7 characters or less and delete all others?

    Try this perl code:

    Code:
    perl -pe 's/\b\w{8,}\b//g'  filename

  3. #3
    Join Date
    Feb 2008
    Location
    Munster, Ireland
    Beans
    2,467
    Distro
    Ubuntu Mate

    Thumbs up Re: Code to only keep words that are 7 characters or less and delete all others?

    Quote Originally Posted by erind View Post
    Try this perl code:

    Code:
    perl -pe 's/\b\w{8,}\b//g'  filename
    Cool, nice one erind.
    I added "> file2.txt" to copy the selected text to a new file.

    Code:
    perl -pe 's/\b\w{9,}\b//g' file1.txt > file2.txt
    Would it be possible to leave all words of 7 chars or less in the original file and delete all other words? No problem if not as the initial code is great.
    1st Distro used (live CD): Knoppix in early 2007 ¦ 1st Distro Installed: Ubuntu 7.10 in Feb 2008
    GNU/Linux User #470660 – Ubuntu User #28226
    Isaac Asimov: "I do not fear computers. I fear the lack of them."

  4. #4
    Join Date
    Dec 2009
    Beans
    195

    Re: Code to only keep words that are 7 characters or less and delete all others?

    Quote Originally Posted by Rytron View Post
    [...]

    Would it be possible to leave all words of 7 chars or less in the original file and delete all other words? No problem if not as the initial code is great.
    Yes, use the -i (inplace) option, but take extra care when using it - it overwrites the original file:

    Code:
    perl -i -pe 's/\b\w{8,}\b//g'  file1.txt
    Another option is keeping a backup of the original file (file1.txt.bak), while modifying the original (file1.txt):

    Code:
    perl -i.bak -pe 's/\b\w{8,}\b//g'  file1.txt
    Yet another common (shell) way is using a temporary file:

    Code:
    perl -pe '...'  file1.txt > temp && mv temp file1.txt

  5. #5
    Join Date
    Feb 2008
    Location
    Munster, Ireland
    Beans
    2,467
    Distro
    Ubuntu Mate

    Re: Code to only keep words that are 7 characters or less and delete all others?

    Cheers erind. You are a code wizard.
    1st Distro used (live CD): Knoppix in early 2007 ¦ 1st Distro Installed: Ubuntu 7.10 in Feb 2008
    GNU/Linux User #470660 – Ubuntu User #28226
    Isaac Asimov: "I do not fear computers. I fear the lack of them."

Tags for this Thread

Bookmarks

Posting Permissions

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