Solved

Remove single characters, extra spaces, insert brackets around keyword

Posted on 2010-09-05
6
898 Views
Last Modified: 2012-05-10
Hi,
I'm looking for a way to remove single characters, extra spaces and insert brackets around keywords:

data:
a word a word
b words words
words z words
z z z words
i words i words etc

output I'm interested in:
word word
words words
words words
words
words words
Thanks
0
Comment
Question by:faithless1
6 Comments
 
LVL 1

Expert Comment

by:TiCpu
ID: 33608218
Made this little script, seems like it works, it is in python though.

import sys;

hFile = open("a.txt", 'r');

line = hFile.readline();
iCnt = 0;
iLast = str();
iNLine = str();
while len(line) > 0:
        for by in line:
                if by == " " or by == "\n":
                        if iCnt > 1:
                                sys.stdout.write(iLast + " ");
                        iCnt = 0;
                        iLast = "";
                else:
                        iLast += by;
                        iCnt += 1;
        sys.stdout.write("\n");
        line = hFile.readline();
0
 
LVL 84

Accepted Solution

by:
ozo earned 500 total points
ID: 33608238
I don't see any brackets in the output you say you are interested in
perl -lpe 's/etc//g;s/\b\w\b//g;s/\s+/ /g;s/^\s+//;s/\s+$//;' << ENDHERE
a word a word
b words words
words z words
z z z words
i words i words etc
ENDHERE
0
 
LVL 65

Expert Comment

by:rockiroads
ID: 33609160
this procedure takes a line and shows all the words with length > 1

sub SplitString {

      my($line) = shift;

      @myStrings = split(' ', $line);

      foreach (@myStrings) {
            my($lineLen) = length($_);
            if ($lineLen > 1) {
                  print $_ . ' ';
            }
      }
}

Only issue is what you mean about brackets as you say insert around keywords. it so then you might need to handle them and wrap as you iterate
0
Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

 
LVL 13

Expert Comment

by:Superdave
ID: 33611877
To put ozo's regular expressions into PHP it would be something like this, if your original file is in $string:

$string = preg_replace('/\b\w\b/','',$string)
$string = preg_replace('/\s+/',' ',$string)
$string = preg_replace('/^\s+/','',$string)
$string = preg_replace('/\s+$/','',$string)

0
 
LVL 15

Expert Comment

by:mish33
ID: 33612408
Python solution is just:
for line in open('data'):
  print ' '.join(word for word line.strip().split() if len(word)>1)

Open in new window

0
 
LVL 5

Expert Comment

by:vikaskhoria
ID: 33633893
I think your example is not very clear for the question. Can you give some more?
0

Featured Post

Backup Your Microsoft Windows Server®

Backup all your Microsoft Windows Server – on-premises, in remote locations, in private and hybrid clouds. Your entire Windows Server will be backed up in one easy step with patented, block-level disk imaging. We achieve RTOs (recovery time objectives) as low as 15 seconds.

Join & Write a Comment

Suggested Solutions

Setting up Secure Ubuntu server on VMware 1.      Insert the Ubuntu Server distribution CD or attach the ISO of the CD which is in the “Datastore”. Note that it is important to install the x64 edition on servers, not the X86 editions. 2.      Power on th…
I have been pestered over the years to produce and distribute regular data extracts, and often the request have explicitly requested the data be emailed as an Excel attachement; specifically Excel, as it appears: CSV files confuse (no Red or Green h…
This video shows how to set up a shell script to accept a positional parameter when called, pass that to a SQL script, accept the output from the statement back and then manipulate it in the Shell.
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.

743 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now