How would I replace the 9th character of every line in a file?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting How would I replace the 9th character of every line in a file?
# 1  
Old 08-31-2004
Question How would I replace the 9th character of every line in a file?

Is there a simple way to replace the 9th character of every line in a text file?

Right now it is a space and instead I want to stick in a 0, every line is the same.

Is there a quick way to do this in Unix? (Solaris)
LordJezo
# 2  
Old 08-31-2004
Try using sed...

sed 's/\(^........\) /\10/' file1 > file2

Or awk...

awk '{print substr($0,1,8) "0" substr($0,10)}' file1 > file2
# 3  
Old 08-31-2004
Worked perfect, thanks.

Can you explain how it works though?

awk '{print substr($0,1,8) "0" substr($0,10)}' file1 > file2

I understand the awk part, the "0" part, and the file1 > file2 part but the substr part.. I see that it works but not why it does.
LordJezo
# 4  
Old 08-31-2004
Code:
{print substr($0,1,8) "0" substr($0,10)}

Awk's concatenation is built into the language. Awk is concatenating the first part of the record (positions 1 through 8) then setting the 9th position to "0". Lastly, the substring command is used to concatenate the remaining portion of the record (from position 10 to the end of the file) onto the new record which is output in the print statement.
# 5  
Old 08-31-2004
Wow.

That's a pretty clever way of doing it. I doubt I would have thought about that angle, what I was thinking was a specific character replace.

Neat.

Thanks.
LordJezo
# 6  
Old 09-01-2004
Or even simpler

sed 's/./0/9' file1 > file2

Which'll replace the 9th occurrence of "any character" with a zero.

Cheers
ZB
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Replace first 3 occurances of a character in a file for each line

Hi all, I am very new to unix - ksh. I want to replace some characters in a file with some other, but only for first three occurances for each line. For eg: the first 3 occurances character ']' has to be replaced with ',' in each line. Please help. thanks Sreejith (1 Reply)
Discussion started by: rsreejithmenon
1 Replies

2. UNIX for Dummies Questions & Answers

Replace 8th and 9th characters in file permanently

Good evening, I have a file and wish to replace the 8th and 9th characters on the first line only no matter what they are with 44 and the file permanantly changed. e.g. file example.txt before change: 123456789123456 hi how are you blah blah file example.txt after change: ... (4 Replies)
Discussion started by: GarciasMuffin
4 Replies

3. Solaris

Line too long error Replace string with new line line character

I get a file which has all its content in a single row. The file contains xml data containing 3000 records, but all in a single row, making it difficult for Unix to Process the file. I decided to insert a new line character at all occurrences of a particular string in this file (say replacing... (4 Replies)
Discussion started by: ducati
4 Replies

4. AIX

removing 8th and 9th character of a FILENAME

Hi everyone, I have a file called pdf.txt which has the following list inside: 7110412_1_31122012.pdf 7286510_4_46667100.pdf 4002176_1_00018824.pdf ... I need a looping script to REMOVE the 8th and 9th character of the filenames listed, no matter what character that is. So... (4 Replies)
Discussion started by: chipahoys
4 Replies

5. Shell Programming and Scripting

replace character only in first line of a file

Hi, I need to replace a character in a specific position in only the first line of a file (actually many files). Change T to a P in position 103. I'm on solaris and my implementation of sed doesn't have the -r capability. Thx, Tim (8 Replies)
Discussion started by: web-guy01
8 Replies

6. Shell Programming and Scripting

read in a file character by character - replace any unknown ASCII characters with spa

Can someone help me to write a script / command to read in a file, character by character, replace any unknown ASCII characters with space. then write out the file to a new filename/ Thanks! (1 Reply)
Discussion started by: raghav525
1 Replies

7. Shell Programming and Scripting

how to replace character in the line

I have unix text file which has the following data aadjdfad;fa;fjjd;lakd;lkaslkd;k;k;lk;k;lk;l;lk;lkj;lj;lkj;k;lkj;lj;lkj;lkj;lkj;j sdkadk;adlf;lajf;akdjf;lkdjf;lkadjf;lkajsd;lfkj;lkj;lkj;lk;lk;lk;lk;k;lkj;k;lkm... (2 Replies)
Discussion started by: Raju Datla
2 Replies

8. Shell Programming and Scripting

replace > with new line character

<reward_data><date><datetime>071308000804</datetime></date> I want the above data to be displayed as <reward_data> <date> <datetime>071308000804</datetime> </date> How can i accomplish this. I tried the below tr "><" ">\n" < filename (4 Replies)
Discussion started by: borncrazy
4 Replies

9. Shell Programming and Scripting

Replace certain character with a new a new line.

Hello all... please help with the following. I am parsing the following type of file... W001; W003; W025;W044; W030; W022;W024;W099;W098; Would like to make it look like this... W001 W003 W025 W044 W030 W022 W024 W099 W098 (8 Replies)
Discussion started by: djsal
8 Replies

10. Shell Programming and Scripting

to replace a new line character

sample I/p: S12J LLL H77K PPP J25O LOP I73S lOP K99O PLO Required O/p: S12J LLL H77K PPP J25O LOP I73S lOP K99O PLO how to replace a new line character with space using sed command only Cheers, Chan (2 Replies)
Discussion started by: chan
2 Replies
Login or Register to Ask a Question