The UNIX and Linux Forums  
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.

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
how to cut first 3 characters of each line in a file kittusri9 Shell Programming and Scripting 7 01-29-2009 12:53 AM
Limit of no of characters PER LINE in a unix file mohapatra Shell Programming and Scripting 5 10-10-2006 04:18 PM
Replace Special characters in a file solai UNIX for Dummies Questions & Answers 1 07-13-2006 10:36 AM
Replace characters in all file names in a particular directory madhunk Shell Programming and Scripting 4 02-16-2006 07:10 PM
Filling in characters to line a file up hcclnoodles Shell Programming and Scripting 1 07-27-2004 10:11 PM

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

Join Date: Dec 2007
Location: Gotham City
Posts: 59
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
  #2 (permalink)  
Old 12-12-2007
rikxik's Avatar
rikxik rikxik is offline
Registered User
  
 

Join Date: Dec 2007
Posts: 250
awk '{print substr($0,1,6)"0000000"substr($0,14,10)}' infile
  #3 (permalink)  
Old 12-12-2007
jakSun8 jakSun8 is offline
Registered User
  
 

Join Date: Dec 2007
Location: Gotham City
Posts: 59
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
  #4 (permalink)  
Old 12-12-2007
bakunin bakunin is offline Forum Staff  
Bughunter Extraordinaire
  
 

Join Date: May 2005
Location: In the leftmost byte of /dev/kmem
Posts: 1,628
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
  #5 (permalink)  
Old 12-12-2007
jakSun8 jakSun8 is offline
Registered User
  
 

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

Great to see people helping others!
JaK
  #6 (permalink)  
Old 12-12-2007
rikxik's Avatar
rikxik rikxik is offline
Registered User
  
 

Join Date: Dec 2007
Posts: 250
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
  #7 (permalink)  
Old 12-12-2007
ghostdog74 ghostdog74 is offline Forum Advisor  
Registered User
  
 

Join Date: Sep 2006
Posts: 2,509
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
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 PM.


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