![]() |
|
|
|
|
|||||||
| 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 |
| 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 |
|
|
Submit Tools | LinkBack | Thread Tools | Search this Thread | Display Modes |
|
#1
|
|||
|
|||
|
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 |
| Forum Sponsor | ||
|
|
|
#2
|
||||
|
||||
|
awk '{print substr($0,1,6)"0000000"substr($0,14,10)}' infile
|
|
#3
|
|||
|
|||
|
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
|
|||
|
|||
|
in bash
Code:
# while read line; do echo ${line//${line:7:7}/0000000}; done < file
Code:
# for line in `cat file`; do echo ${line//${line:7:7}/0000000}; done
|
|
#5
|
|||
|
|||
|
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
|
|
#6
|
|||
|
|||
|
Thanks guys. To get around "too long" issue, I just used nawk instead and it worked.
Great to see people helping others! JaK |
|
#7
|
|||||||
|
|||||||
|
Consider this:
Quote:
@bakunin sed definitely takes the cake (as long as no too long line issues): Quote:
Quote:
Quote:
Quote:
@ghostdog74 This wasn't very fast either: Quote:
Mine wasn't too bad - not as fast as the sed version: Quote:
HTH |
|||||||
| Google The UNIX and Linux Forums |