replace character only in first line of a file


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting replace character only in first line of a file
# 1  
Old 12-21-2009
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
# 2  
Old 12-21-2009
one way:
Code:
# assumes pos 103 is always a T -> P change
for fname in $( find . -name 'filetofix*')
do
     awk '{if(FNR==1) 
               {printf("%s%s%s", substr($0,1,102), "P", substr($0,104) )} 
             else 
               {print} ' $fname > ${fname}.fixed
done

# 3  
Old 12-21-2009
Code:
$ cat > t
testing...
test of second line.
$ sed -e '1s/\(.\{4\}\).\(.*\)/\1P\2/' t
testPng...
test of second line.

If you wanted the 103rd character to be replaced, then use 102 instead of 4 in the sed command.
Code:
sed -e '1s/\(.\{102\}\).\(.*\)/\1P\2/' t

And if you have the -i option, then you can use it which will edit the file itself.
Code:
sed -ie '1s/\(.\{102\}\).\(.*\)/\1P\2/' t

# 4  
Old 12-21-2009
Thanks! I couldn't get the awk to work, but the sed command did the trick.

Tim
# 5  
Old 12-21-2009
Try this awk:

Code:
awk 'BEGIN{FS=OFS=""} {if (NR==1&&$103="T") $103="P" }1' input.txt

# 6  
Old 12-21-2009
If you have the -r option:
Code:
sed -r '1s/^(.{102})T/\1P/' infile

otherwise
Code:
sed '1s/^\(.\{102\}\)T/\1P/' infile

The ^ caret is important otherwise you may end up matching some T in a different column > 102

Last edited by Scrutinizer; 12-21-2009 at 05:26 PM..
# 7  
Old 12-22-2009
this will work
Code:
awk 'BEGIN{FS=OFS=""} {if (NR==1&&$103="T") $103="P" }1' file.txt


Last edited by radoulov; 12-22-2009 at 04:07 AM.. Reason: Please use code tags.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Replace x-y character in line

I have a file which has n number of lines, i want to replace 3rd line position 3-5 to some text. 111111111111111111 222222222222222222 333333333333333333 444444444444444444 expected output 111111111111111111 222222222222222222 33abc3333333333333 444444444444444444 it is... (7 Replies)
Discussion started by: greenworld123
7 Replies

2. 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

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. Shell Programming and Scripting

Replace last character of every second line

Hi all. I wonder if this possible.... any help advice is very much appreciated.. I n a shell script I create a latex file that looks like this \documentclass{article} \usepackage{graphics} \begin{document} \begin{figure} \begin{center} \begin{tabular}{cc} ... (10 Replies)
Discussion started by: malandisa
10 Replies

5. 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

6. 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

7. 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

8. 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

9. Shell Programming and Scripting

Replace a character in last line

Hello Sed Experts, I have got a file which contain entries as below pmNoOfSwDownHsCong, pmUlUpswitchAttemptHigh, pmUlUpswitchAttemptLow, pmUlUpswitchSuccessHigh, pmUlUpswitchSuccessLow, pmUpswitchFachHsAttempt, ... (6 Replies)
Discussion started by: Mohammed
6 Replies

10. Shell Programming and Scripting

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) (5 Replies)
Discussion started by: LordJezo
5 Replies
Login or Register to Ask a Question