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 here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
how to cut first 3 characters of each line in a file kittusri9 Shell Programming and Scripting 4 04-25-2008 05:10 AM
Limit of no of characters PER LINE in a unix file mohapatra Shell Programming and Scripting 5 10-10-2006 01:18 PM
Replace Special characters in a file solai UNIX for Dummies Questions & Answers 1 07-13-2006 07:36 AM
Replace characters in all file names in a particular directory madhunk Shell Programming and Scripting 4 02-16-2006 04:10 PM
Filling in characters to line a file up hcclnoodles Shell Programming and Scripting 1 07-27-2004 07:11 PM

Reply
 
Submit Tools LinkBack Thread Tools Search this Thread Display Modes
  #1  
Old 12-12-2007
Registered User
 

Join Date: Dec 2007
Location: Gotham City
Posts: 55
How to replace characters 7 through 14 of every line in a file

Hi all,

I have a file with multiple lines. I want to replace characters 7 through 14 of every line with 0000000

Input:
12345678901234567890
23456789012345678901

Output
12345600000004567890
23456700000005678901

Please help.

JaK
Reply With Quote
Forum Sponsor
  #2  
Old 12-12-2007
rikxik's Avatar
Registered User
 

Join Date: Dec 2007
Posts: 105
awk '{print substr($0,1,6)"0000000"substr($0,14,10)}' infile
Reply With Quote
  #3  
Old 12-12-2007
Registered User
 

Join Date: Dec 2007
Location: Gotham City
Posts: 55
Thanks rikxik, it works but when i run the same command on record which has lenght of 6656 bytes .. it says "record too long" .. is there some limitation on awk? How do i get around it?

Please help
JaK
Reply With Quote
  #4  
Old 12-12-2007
Registered User
 

Join Date: Sep 2006
Posts: 1,580
in bash
Code:
# while read line; do echo ${line//${line:7:7}/0000000}; done < file
or
Code:
# for line in `cat file`; do echo ${line//${line:7:7}/0000000}; done
Reply With Quote
  #5  
Old 12-12-2007
Bughunter Extraordinaire
 

Join Date: May 2005
Location: In the leftmost byte of /dev/kmem
Posts: 1,262
sed 's/\(.\{7\}\).\{7\}/\10000000/' oldfile > newfile

A lot faster than calling substr() two times in awk.

If your lines are too long to handle you could cut them before making the exchange and then put them together again:

Code:
typeset line=""
typeset start=""
typeset end=""

cat oldfile | while read line ; do
     start="(print - "$line" | cut -c1-14)"
     end="$(print - "$line" | cut -c15-)"
     start="${start%%???????}0000000"
     print - "${start}${end}" >> newfile
done
bakunin
Reply With Quote
  #6  
Old 12-12-2007
Registered User
 

Join Date: Dec 2007
Location: Gotham City
Posts: 55
Thanks guys. To get around "too long" issue, I just used nawk instead and it worked.

Great to see people helping others!
JaK
Reply With Quote
  #7  
Old 12-12-2007
rikxik's Avatar
Registered User
 

Join Date: Dec 2007
Posts: 105
Consider this:

Quote:
$ head bigfile ; wc -l bigfile
12345678901234567890
23456789012345678901
12345678901234567890
23456789012345678901
12345678901234567890
23456789012345678901
12345678901234567890
23456789012345678901
12345678901234567890
23456789012345678901
20000 bigfile

@bakunin

sed definitely takes the cake (as long as no too long line issues):

Quote:
[sdass@db012a:PNB] /shared/home/sdass/tmp > time sed 's/\(.\{7\}\).\{7\}/\10000000/' bigfile > newfile.sed

real 0m0.08s
user 0m0.03s
sys 0m0.00s
But wrongly produces lines like this - nothing which cannot be fixed:
Quote:
12345670000000567890
23456780000000678901
Instead of this:
Quote:
12345600000004567890
23456700000005678901
However, speaking of speed, the bash script you posted is as fast as a Snail:

Quote:
$ cat baku
#!/usr/bin/ksh

typeset line=""
typeset start=""
typeset end=""

cat bigfile | while read line ; do
start="(print - "$line" | cut -c1-14)"
end="$(print - "$line" | cut -c15-)"
start="${start%%???????}0000000"
print - "${start}${end}" >> newfile
done
$ time baku

real 5m6.62s
user 0m14.95s
sys 1m9.19s
And it produced incorrect output too.

@ghostdog74
This wasn't very fast either:

Quote:
$ cat ghost
#!/usr/bin/bash

while read line; do echo ${line//${line:7:7}/0000000}; done < bigfile > newfile.bash
$ time ghost

real 0m1.31s
user 0m0.82s
sys 0m0.44s


Mine wasn't too bad - not as fast as the sed version:
Quote:
$ time awk '{print substr($0,1,6)"0000000"substr($0,14)}' bigfile > newfile.awk

real 0m0.17s
user 0m0.11s
sys 0m0.00s
Nothing against anyone - just fair comparison.

HTH
Reply With Quote
Google The UNIX and Linux Forums
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes




All times are GMT -7. The time now is 07:15 AM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited.
The UNIX and Linux Forums Content Copyright ©1993-2008. All Rights Reserved.Ad Management by RedTyger Visit The Complex Event Processing Blog

Content Relevant URLs by vBSEO 3.2.0