Solved

Remove single characters, extra spaces, insert brackets around keyword

Posted on 2010-09-05
6
900 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
Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

 
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

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Why Shell Scripting? Shell scripting is a powerful method of accessing UNIX systems and it is very flexible. Shell scripts are required when we want to execute a sequence of commands in Unix flavored operating systems. “Shell” is the command line i…
The purpose of this article is to demonstrate how we can use conditional statements using Python.
Connecting to an Amazon Linux EC2 Instance from Windows Using PuTTY.
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.

831 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