Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Removing every other character from lines 3

Status
Not open for further replies.

bigbalbossa

Programmer
Mar 21, 2002
87
US
Guys,

I have a txt file that has a space between every character in entire file. For example:

2 5 J L 1 7 / 3 3 T M N T A N

should be:

25 JL 17/33 TMN TAN

What's the best way to remove this space? I'm sure you awk guys have any answer :)
 
Regular expresssions. Search and replace.

Code:
my $str = "2 5   J L   1 7 / 3 3   T M N   T A N";
$str =~ s/(.)\s{1}(.)/$1$2/g;
$str =~ s/(.)\s{1}(.)/$1$2/g;
print $str;
 
Thanks raklet...perfect. I did come up with a similar solution:

$temp[10] =~ s/ {2}?/~/gi;
$temp[10] =~ s/ {1}?//gi;

Thanks for the reply and solution...star for you.
 
This'll avoid having to make two passes.
Code:
s/(.) /$1/g
 
iluvperl,

Two pieces of advice for you:
1) Always test everything you are going to post before you post it. You'll learn more that way.
2) If Ishnid gives a solution, trust it. If your life depends on it, trust it all the more! ;)



Trojan.
 
3) Only follow rule number 2 if ishnid has followed rule number 1. :-D
 
Nah, I don't buy rule 3.
Ishnid's guesses are better than most peoples tried and tested solutions.
;-)



Trojan.
 
<rant>
I come from a community that believes posting without error checking is fine. If the solution gives the user the right method to go about things but errors out due to a little problem (typo, no semi colon, forget to terminate and endless loop), that's not the point. You helped them by pushing them in the right direction.

As for "2) If Ishnid gives a solution, trust it. If your life depends on it, trust it all the more! ;)", that's quite bogus to say. He might know Perl pretty well. He might know it better than me. Perhaps he doesn't. In either case, how much can you trust someone elses code? Everyone codes differently and thus, everyone has a "better" approach at a certain problem than the next person depending on the OP needs and those who later search the topic and find the applied hack might help them, too.

Just one example is Randal Schwartz. He knows his stuff but occassionaly, you just have to disagree with his approach to problems.
</rant>

Outside of my original

Code:
s/\s{2}//g;

Split might be a better idea
Code:
$string = join(' ', split(/\s+/,$string));
 
Dang.
You took me seriously!
I thought the smileys might be a giveaway.
But since you raise the point, if you test your own code you'll learn much more than someone posting you a solution. You might actually find the answer yourself.
I am not suggesting that you don't post, quite the contrary, the more the merrier. But at least if you test your own code first then there are two possible benefits.
1) You learn more.
2) If others base any of their work on your post you know they'll be working with something that works and not something that might cause them a headache in debugging later.
That sounds like a criticism but it's not meant to be. After all, if they have to debug stuff they'll learn more too!
Time for another smiley!

;-)



Trojan.
 
If the solution gives the user the right method to go about things but errors out due to a little problem (typo, no semi colon, forget to terminate and endless loop), that's not the point.

I can agree with you about typos. Everybody makes them. However, when the code is just plain wrong, it is irresponsible to post it without having even tested it. I'm sorry to say, but both of your solutions fail to do the right thing. Both of your proposals output the following:

2 5 J L 1 7 / 3 3 T M N T A N

When in reality, the user wants it to look like this:

25 JL 17/33 TMN TAN


You would do well to watch and learn from Ishnid. He is a good teacher and good resource. In this thread, he not only posted a correct solution, but he also posted the most concise.


 
Guys,

How about this idea!

$perl -pi -d "s/ /:/g;s/ //g;s/:/ /g" <filename>


Hope it helps.

aixnag
IBM Certified Specialist - P-series AIX 5L Administration
IBM Certified Specialist - AIX V4 HACMP
IBM eServer Certified Specialist – p690 Technical Support
IBM Certified Solutions Expert - DB2 UDB V7.1 Database Administration for Unix, Linux, Windows and OS/2
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top