replace characters in a file


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting replace characters in a file
# 1  
Old 10-22-2008
replace characters in a file

Hi,

I have a file in which i want to replace the charaters from position 3-5 with a particular string for the first line.

For ex

The file contains

abcdefghij
jkdsflsfkdk
908090900

i want to replace the characters 3-5 for the first line as 678

so, the file should look like

ab678fghij
jkdsflsfkdk
908090900

i am editing it in vi and doing the change. But we have so many file to do the same thing, and i am trying to automate it. can someone help.
# 2  
Old 10-22-2008
One way:

Code:
awk -v var="678" 'NR==1{print substr($0,1,2) var substr($0,6);next}1' file > newfile

Regards
# 3  
Old 10-22-2008
wher are we specifying the character numbers as 3-5
# 4  
Old 10-22-2008
Code:
sed '1s/\(..\)\(...\)\(.*\)/\1678\3/' file

# 5  
Old 10-22-2008
Quote:
Originally Posted by dnat
wher are we specifying the character numbers as 3-5
Nowhere, the command prints the first 2 characters of the first line, then the variable and at last position 6 until the end of the line.

Regards
# 6  
Old 10-23-2008
thank you..but can you tell me how i can mention the characters 3-5. i have just given examples here and my main data is something different.
# 7  
Old 10-23-2008
In a nutshell.

i want to cut the characters 3-5 from the file, change it and insert the changed value in the position 3-5.
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

How to replace characters in a file?

Hi Gurus, I need replace some charactors in a file. in following example. I need replace from 4th charactor to 6th charactor with x in each line. abcdefghijklmn 123456789011 excepted result: abcxxxghijklmn 123xxx789011 Thanks in advance. (6 Replies)
Discussion started by: ken6503
6 Replies

3. Shell Programming and Scripting

Replace first 3 characters in a unix file in all lines

Replace first 3 characters in a unix file (say replace "A&B" with "C&D") in all lines of the file. Need a sed or awk script to do this. Kindly help! -Kumar (4 Replies)
Discussion started by: vasan2815
4 Replies

4. Shell Programming and Scripting

Using shell to replace characters in a text file

Can I just say, this is such a frustrating and yet enormously rewarding field of study. I'm in the middle of configuring GeekTool (Uh oh, stupid n00b) and I really only have one question. I'm using Automator to grab a RSS feed, having GeekTool continually run that application every 10 minutes,... (7 Replies)
Discussion started by: SomeTechGuy
7 Replies

5. Shell Programming and Scripting

Replace 10 characters in file

Hi, I need to replace 10 characters string (21-30) in a file with another string. I tried using cut command, i am able get these 10 charaters, but do not know how to replace them inside the file. for example file content(these are alphanumeric characters):... (3 Replies)
Discussion started by: Johny001
3 Replies

6. Shell Programming and Scripting

Replace characters then read the file without changing it

Hi All At the moment the following code works but ideally i do not want to have to change the original $1 tr "\r" "\n" < "$1" > "$1.fix" printf "\n" >> "$1.fix" mv "$1.fix" "$1" FILE=$1 coffee_out="splitmovie" coffee_fill="-splitAt" coffee_end="-self-contained -o output.mov $2"... (1 Reply)
Discussion started by: babajuma
1 Replies

7. UNIX for Dummies Questions & Answers

Find and replace special characters in a file

HI All I need a shell script ehich removes all special characters from file and converts the file to UTF-* format Specail characters to be removed must be configurable. strIllegal = @"?/><,:;""'{|\\+=-)(*&^%$#@!~`"; Please help me in getting this script as my scripting skilla are... (2 Replies)
Discussion started by: sujithchandra
2 Replies

8. Shell Programming and Scripting

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 (9 Replies)
Discussion started by: jakSun8
9 Replies

9. UNIX for Dummies Questions & Answers

Replace Special characters in a file

Hi, I have a data like this in a file, 402003279034002000100147626030003300010000000000002000029000000 ær^M^\MÍW^H I need to replace those special char to some other char like # or $ Is there any ways to do it... I tried commands tr,sed and many but it was not able to replace because... (1 Reply)
Discussion started by: solai
1 Replies

10. Shell Programming and Scripting

Replace characters in all file names in a particular directory

Hi, I have searched the forum on how to mass replace the file names. We are doing the migration and I am trying to accomplish a task where I have to replace all UNIX scripts in a particular directory that start with bdw to fdm... For example: bdw0110137.sh should be fdm0110137.sh Keep the... (4 Replies)
Discussion started by: madhunk
4 Replies
Login or Register to Ask a Question