How to replace characters 7 through 14 of every line in a file


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting How to replace characters 7 through 14 of every line in a file
# 1  
Old 12-12-2007
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  
Old 12-12-2007
awk '{print substr($0,1,6)"0000000"substr($0,14,10)}' infile
# 3  
Old 12-12-2007
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  
Old 12-12-2007
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

# 5  
Old 12-12-2007
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
# 6  
Old 12-12-2007
Thanks guys. To get around "too long" issue, I just used nawk instead and it worked.

Great to see people helping others!
JaK
# 7  
Old 12-12-2007
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
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

Need to replace new line characters in a huge file

Hi , I would like to replace new line characters(\n) in a huge file of about 2 million records . I tried this one (:%s/\n//g) but it's hanging there and no result. Does this command do not work if the file is big. Please let me know if you have any other options Regards Raj (1 Reply)
Discussion started by: rajeevm
1 Replies

2. Shell Programming and Scripting

sed replace range of characters in each line

Hi, I'm trying to replace a range of characters by their position in each line by spaces. I need to replace characters 95 to 145 by spaces in each line. i tried below but it doesn't work sed -r "s/^(.{94})(.{51})/\ /" inputfile.txt > outputfile.txt can someone please help me... (3 Replies)
Discussion started by: Kevin Tivoli
3 Replies

3. UNIX for Dummies Questions & Answers

Bash: using SED, trying to replace some characters except first or last line

Hi, I require to replace 2 items: 1. replace start of all lines in a file with ' except the first line 2. replace end of all lines in a file with '||chr( except last line I am able to do the entire file using sed -e s/^/\'/g -e s/$/\'\|\|chr\(/g "$file" > newfile.txt but am not yet... (3 Replies)
Discussion started by: Chella15
3 Replies

4. Shell Programming and Scripting

Bash: using SED, trying to replace some characters except first or last line

Hi, I require to replace 2 items: 1. replace start of all lines in a file with ' except the first line 2. replace end of all lines in a file with '||chr( except last line I am able to do the entire file using sed -e s/^/\'/g -e s/$/\'\|\|chr\(/g "$file" > newfile.txt but am not yet able... (0 Replies)
Discussion started by: Chella15
0 Replies

5. Shell Programming and Scripting

sed replace characters in next line with input from a file

Hi, I have a set of strings in filea. I want to search string xyz in fileb and replace next line in file b with the content from filea. #cat filea abc def ghi #cat fileb asdkjdslka sajljskdjoi xyzjjjjkko aaaaaaaa bbbbbbbb cccccccc xyzsdsajd dddddddd eeeeeeee (2 Replies)
Discussion started by: anilvk
2 Replies

6. Shell Programming and Scripting

search pattern and replace x-y characters in nth line after every match

Hi, I am looking for any script which can do the following. have to read a pattern from fileA and copy it to fileB. fileA: ... ... Header ... ... ..p1 ... ... fileB: .... .... Header (3 Replies)
Discussion started by: anilvk
3 Replies

7. Shell Programming and Scripting

Replace new line with <br /> & escape special characters

Hi, I wish to replace a new line with br (html) but it doesn't seem to work message=$(echo ${FORM_message} | tr '\r' '<br \/>' ) what it gives me seems to be ... b...? I am also having problem escaping hash sign in cut command: list=$(echo "$line" | cut -d'\#;\#' -f1) ; my intention is... (2 Replies)
Discussion started by: ted_chou12
2 Replies

8. Shell Programming and Scripting

to replace unwanted new line characters

Hi how to replace un wanted new line characters. my file contains data like. ramu,sony,"raju \n ravi \n ramya" \n ravi,sarah,"sowmya \n sorry s\ sangam" \n i want replace new line characters in between double coats with single space. for example $ cat input_file ramu,sony,"raju... (3 Replies)
Discussion started by: Raghava
3 Replies

9. Shell Programming and Scripting

to replace un wanted new line characters in double quoats

Hi my file data is like below ramu,sony,"raju \n ravi \n ramya" \n ravi,sarah,"sowmya \n sorry \n sangam" \n i want replace new line characters in between double coats with single space. for example cat input_file ramu,sony,"raju ravi ramya" ravi,sarah,"sowmya sorry sangam" ... (5 Replies)
Discussion started by: Raghava
5 Replies

10. UNIX for Dummies Questions & Answers

help to replace extra new line characters

Hi my file data is like below ramu,sony,"raju \n ravi \n ramya" \n ravi,sarah,"sowmya \n sorry s\ sangam" \n i want replace new line characters in between double coats with sinhle space. for example cat input_file ramu,sony,"raju ravi ramya" ravi,sarah,"sowmya sorry sangam" ... (3 Replies)
Discussion started by: Raghava
3 Replies
Login or Register to Ask a Question