![]() |
|
|
|
|
|||||||
| Forums | Portal | Register | Forum Rules | FAQ | Contribute | Members List | Arcade | Search | Today's Posts | Mark Forums Read |
| Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts here. |
|
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Find/replace to new file: ksh -> perl | McLan | Shell Programming and Scripting | 1 | 05-16-2008 12:14 AM |
| read space filled file and replace text at specific position | COD | Shell Programming and Scripting | 6 | 04-21-2008 02:40 AM |
| replace the last delimiter | jisha | Shell Programming and Scripting | 4 | 01-28-2008 03:26 AM |
| replace delimiter : with '\001' in unix data file | spandu | Shell Programming and Scripting | 4 | 04-25-2006 05:51 AM |
| field delimiter with a space or more | uphamtn | UNIX for Dummies Questions & Answers | 3 | 05-15-2003 01:22 PM |
|
|
Submit Tools | LinkBack | Thread Tools | Search this Thread | Display Modes |
|
#1
|
|||
|
|||
|
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 |
| Forum Sponsor | ||
|
|
|
#2
|
|||
|
|||
|
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
|
|||
|
|||
|
why do you open tmpfile inside the loop? Move the open statement above the loop, close below the loop.
|
|
#4
|
|||
|
|||
|
sed -e "s/ [ ]*/|/g" infile > outfile
there are 2 spaces between the first "/" and the "[". |
|
#5
|
|||
|
|||
|
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
|
|||
|
|||
|
Code:
open(FH_TMP,"<","$chk_file");
open(OUT,">>",tmpfile);
while (my $line = <FH_TMP>)
{
$line =~ s/ {2,}/|/g;
print OUT $line;
}
close(FH_TMP);
close(OUT);
Code:
@ARGV = ($chk_file);
$^I = '.bak';
while (<>){
s/ {2,}/|/g;
print;
}
|
|
#7
|
|||
|
|||
|
aaaaaaaaaaaaaah, homework
|
|||
| Google The UNIX and Linux Forums |