1833738 Members
2498 Online
110063 Solutions
New Discussion

Script question

 
SOLVED
Go to solution
George Chechakunnil
Frequent Advisor

Script question

Hello admins

I have a bunch of files where i need to replace the 100th Charecter in the fist line of each file by new charecter

Any help will be great
I am like a small boy picking up pebbles in god's vast shore of knowledge --- Sir Issac Newton
6 REPLIES 6
Wouter Jagers
Honored Contributor

Re: Script question

Hiya,

cat file.txt | sed -e 's/\(.\{99\}\).\(.*\)/\1Q\2/'

Where the 'Q' is your 'new' character.

Hope that helps,
Cheers
Wout
an engineer's aim in a discussion is not to persuade, but to clarify.
Wouter Jagers
Honored Contributor

Re: Script question

Sorry. For just the 1st line:

cat file.txt | sed -e '1 s/\(.\{99\}\).\(.*\)/\1Q\2/'

So, to fix all txt files in a directory:

for i in `ls *.txt`
do
cat $i | sed -e '1 s/\(.\{99\}\).\(.*\)/\1Q\2/' > $i
done

Test it first, though ;-)
an engineer's aim in a discussion is not to persuade, but to clarify.
Peter Godron
Honored Contributor

Re: Script question

George,
sed '1 s/./a/100' a.lis > b.lis

Wouter Jagers
Honored Contributor

Re: Script question

Of course ! /100 for the 100th occurence.. I could have thought of that.. Points to Peter for an even simpler solution :-)
an engineer's aim in a discussion is not to persuade, but to clarify.
vthiru
Advisor

Re: Script question

pls let me know the command to insert a charater or a word at 100th position, if the 100th column is blank space..
Wouter Jagers
Honored Contributor
Solution

Re: Script question

Try this combination of the above:

sed -e '/^.\{99\}[ ]/ s/./ your text /100' yourfile.txt

Translation: on lines which start with 99 characters and then have a space, replace the 100th character with " your text ".

Cheers
an engineer's aim in a discussion is not to persuade, but to clarify.