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.
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."
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.
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.Code:perl -pe 's/\b\w{9,}\b//g' file1.txt > file2.txt
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."
Yes, use the -i (inplace) option, but take extra care when using it - it overwrites the original file:
Another option is keeping a backup of the original file (file1.txt.bak), while modifying the original (file1.txt):Code:perl -i -pe 's/\b\w{8,}\b//g' file1.txt
Yet another common (shell) way is using a temporary file:Code:perl -i.bak -pe 's/\b\w{8,}\b//g' file1.txt
Code:perl -pe '...' file1.txt > temp && mv temp file1.txt
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."
Bookmarks