Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 907
  • Last Modified:

Remove single characters, extra spaces, insert brackets around keyword

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
faithless1
Asked:
faithless1
1 Solution
 
TiCpuCommented:
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
 
ozoCommented:
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
 
rockiroadsCommented:
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
Get your Disaster Recovery as a Service basics

Disaster Recovery as a Service is one go-to solution that revolutionizes DR planning. Implementing DRaaS could be an efficient process, easily accessible to non-DR experts. Learn about monitoring, testing, executing failovers and failbacks to ensure a "healthy" DR environment.

 
SuperdaveCommented:
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
 
mish33Commented:
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
 
vikaskhoriaCommented:
I think your example is not very clear for the question. Can you give some more?
0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now