Operating System - HP-UX
1832692 Members
3059 Online
110043 Solutions
New Discussion

replace certain chars in every line of a text file

 
SOLVED
Go to solution

replace certain chars in every line of a text file

Hello, I need some help
I need to replace chars 7-10 in each line of a file with something else.. I looked and SED and TR but can't find the proper syntax.

Please help
thanx,
Steve
13 REPLIES 13
A. Clay Stephenson
Acclaimed Contributor

Re: replace certain chars in every line of a text file

Here's a simple method in awk but Perl would do this easily as well:

First create an awk script, my.awk.

{
if (length($0) >= 7)
{
s1 = substr($0,1,6)
s2 = "NEW!"
s3 = substr($0,11)
printf("%s%s%s\n",s1,s2,s3)
}
else printf("%s\n",$0)
}

Now:

awk -f my.awk oldfile > newfile.
If it ain't broke, I can fix that.
Martin Johnson
Honored Contributor

Re: replace certain chars in every line of a text file

In vi:

:= #Gives total lines in file (n)

:1,ns/str1/str2/g #replaces str1 with str2 in the entire file

HTH
Marty
Paul Sperry
Honored Contributor

Re: replace certain chars in every line of a text file

in vi

:%s/old/new/g
Carlos Fernandez Riera
Honored Contributor

Re: replace certain chars in every line of a text file

I cant help you, but try this one:

http://forums.itrc.hp.com/cm/QuestionAnswer/1,,0x001d63f96280d711abdc0090277a778c,00.html
I've saved it.

Maybe someday i will learn martian.
unsupported
V.Tamilvanan
Honored Contributor

Re: replace certain chars in every line of a text file

Hi,

#sed 's/Old/New/g' inputfile >outputfile

HTH
Martin Robinson
Frequent Advisor
Solution

Re: replace certain chars in every line of a text file

Since you specifically want to replace definite column positions, s/old/new/ won't do what you want. This will:

cat file | sed 's/^\(.......\).../\1XXX/'

This means substitute, matching a pattern that starts at the beginning of the line ^, match any 7 characters and keep them, because they are enclosed in \( and \). Then match the next three characters. Replace this with whatever the first 7 characters were using \1 then add the new stuff you want, XXX. There is also no special processing required for lines shorter than 10 characters, as the pattern will simply fail to match.
H.Merijn Brand (procura
Honored Contributor

Re: replace certain chars in every line of a text file

# perl -pi -e'substr($_,6,3)="Boo!"' your_file

Enjoy, have FUN! H.Merijn
Enjoy, Have FUN! H.Merijn
Karl Mumford
New Member

Re: replace certain chars in every line of a text file

cut -b 1-6 infile | sed 's/$/4NEW/' >f1
cut -b 11- infile >f2
paste f1 f2 >outfile
V. V. Ravi Kumar_1
Respected Contributor

Re: replace certain chars in every line of a text file

Hi,
You can achieve this as follows

for i in `cat `
do
echo $i > (tmpfile1)
str=`cut -c 7-10 tmpfile1`
sed 's/'$str'/xxxx/' tmpfile1 >> /tmp/
done

where "xxxx" is your substitute string

Regards
Never Say No

Re: replace certain chars in every line of a text file

Wow,

didn't expect so many replies..
thanx a bunch


Steve

Re: replace certain chars in every line of a text file

also, wanted to thank Martin Robinson.. his advice worked for what I needed.

using sed 's/Old/New/g' wouldn't work in my case because I didn't have anything to match to.. the only constant in my case was that I needed to replace from char 7 to char 10.

I didn't try using suggested AWK scripts, because I'm not very familiar with awk and didn't know what it was doing ( and I don't want to run scripts that I don't know what they do )

anyhow.. I want to thank everybody for their suggestions.. its great to know that I can turn to these forums if I ever need any more help.

thanx,
Steve
Brian Kinney
Frequent Advisor

Re: replace certain chars in every line of a text file

Steve -
now you should go back and assign points to these answers. It's another way to say thanks to those who've answered your questions.
"Any sufficiently advanced technology can be indistinguishable from magic" Arthur C. Clarke. My corollary - "Any advanced technology can be crushed with a sufficently large enough rock."
John Hall
Frequent Advisor

Re: replace certain chars in every line of a text file

The following uses "ex" to accomplish your task. This line should all be typed on the same line as a single command. It looks a little cryptic but, hey it's Unix, and it works:

#-----------------------
echo "1,\$s/\(......\)..../\1abcd/\n:wq!" | /usr/bin/ex xyz
#-----------------------

where "abcd" is the new text that you want to replace the old text, and "xyz" is the file to be modified.