The UNIX and Linux Forums  


Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
.
google unix.com



Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Find/replace to new file: ksh -> perl McLan Shell Programming and Scripting 1 05-16-2008 04:14 AM
read space filled file and replace text at specific position COD Shell Programming and Scripting 6 04-21-2008 06:40 AM
replace the last delimiter jisha Shell Programming and Scripting 4 01-28-2008 06:26 AM
replace delimiter : with '\001' in unix data file spandu Shell Programming and Scripting 4 04-25-2006 09:51 AM
field delimiter with a space or more uphamtn UNIX for Dummies Questions & Answers 3 05-15-2003 05:22 PM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Bulgarian Greek Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 02-14-2008
meghana meghana is offline
Registered User
  
 

Join Date: Feb 2008
Posts: 56
replace space with delimiter in whole file -perl

Hi

I have a file which have say about 100,000 records..
the records in it look like

Some kind of text 1234567891 abcd February 14, 2008 03:58:54 AM lmnop

This is how it looks.. if u notice there is a 2byte space between each column.. and im planning to replace that with '|' ..

say ..

Some kind of text|1234567891|abcd|February 14, 2008 03:58:54 AM|lmnop

.. here is the code which i have written.. but someone should help me in completing it... thanks in advance

open(fh_tmp,"<","$chk_file");
while (my $line = <fh_tmp>)
{
$line =~ s/ /|/g ;
open(out,">>",tmpfile);
print out $line;
close(out);
}
close(fh_tmp);


please correct me if im wrong.. thanks
  #2 (permalink)  
Old 02-14-2008
meghana meghana is offline
Registered User
  
 

Join Date: Feb 2008
Posts: 56
small correction.. there can be more than 2 byte space between 2 columns...
it should replace with delimiter '|' if it has two consecutive spaces .. not just one..

as the first column has single spaces in it "Some kind of text" .. this is one single record...
-thanks
  #3 (permalink)  
Old 02-14-2008
jim mcnamara jim mcnamara is offline Forum Staff  
...@...
  
 

Join Date: Feb 2004
Location: NM
Posts: 5,794
why do you open tmpfile inside the loop? Move the open statement above the loop, close below the loop.
  #4 (permalink)  
Old 02-14-2008
sb008 sb008 is offline Forum Advisor  
Registered User
  
 

Join Date: Jan 2007
Posts: 384
sed -e "s/ [ ]*/|/g" infile > outfile


there are 2 spaces between the first "/" and the "[".
  #5 (permalink)  
Old 02-14-2008
meghana meghana is offline
Registered User
  
 

Join Date: Feb 2008
Posts: 56
i will correct the loop position..thanks for that jim...
hey sb008.. im writing a perl script.. not a shell.. but still thanks for ur suggestion.. i can use it when i do a shell ..

thanks for ur replies
  #6 (permalink)  
Old 02-14-2008
sb008 sb008 is offline Forum Advisor  
Registered User
  
 

Join Date: Jan 2007
Posts: 384
Quote:
Originally Posted by meghana View Post
i will correct the loop position..thanks for that jim...
hey sb008.. im writing a perl script.. not a shell.. but still thanks for ur suggestion.. i can use it when i do a shell ..

thanks for ur replies
aaaaaaaaaaaaaah, homework
  #7 (permalink)  
Old 02-19-2008
nj78 nj78 is offline
Registered User
  
 

Join Date: Jul 2005
Posts: 108
Quote:
Originally Posted by sb008 View Post
aaaaaaaaaaaaaah, homework

too funny, I would recommend sed for this too.
Closed Thread

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -4. The time now is 05:27 AM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0