?
Solved

Remove single characters, extra spaces, insert brackets around keyword

Posted on 2010-09-05
6
Medium Priority
?
904 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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 2000 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
NEW Veeam Agent for Microsoft Windows

Backup and recover physical and cloud-based servers and workstations, as well as endpoint devices that belong to remote users. Avoid downtime and data loss quickly and easily for Windows-based physical or public cloud-based workloads!

 
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

Microsoft Certification Exam 74-409

Veeam® is happy to provide the Microsoft community with a study guide prepared by MVP and MCT, Orin Thomas. This guide will take you through each of the exam objectives, helping you to prepare for and pass the examination.

Question has a verified solution.

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

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…
In part one, we reviewed the prerequisites required for installing SQL Server vNext. In this part we will explore how to install Microsoft's SQL Server on Ubuntu 16.04.
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.
Connecting to an Amazon Linux EC2 Instance from Windows Using PuTTY.
Suggested Courses
Course of the Month11 days, 12 hours left to enroll

752 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