Re: script insertion (865 Views)
Reply
Frequent Advisor
iranzo
Posts: 85
Registered: ‎10-24-2001
Message 1 of 12 (865 Views)
Accepted Solution

script insertion

Hello,
On HPUX ksh :
On a file,i want to transform string
xxxxxxxxxxxx
with ( insert .)
xxxx.xxxx.xxxx
Thanks a lot.
Bonjour
Honored Contributor
John Carr_2
Posts: 1,154
Registered: ‎02-02-2002
Message 2 of 12 (865 Views)

Re: script insertion

Hi

cat filename | sed -e s/xxxxxxxxxxxx/xxxx.xxxx.xxxx/g > newfilename

John.
Honored Contributor
harry d brown jr
Posts: 8,418
Registered: ‎12-12-2000
Message 3 of 12 (865 Views)

Re: script insertion

# echo abcdefghijkl | sed "s/\(.\{4\}\)\(.\{4\}\)\(.\{4\}\)/\1.\2.\3/"
abcd.efgh.ijkl
#


live free or die
harry
Live Free or Die
Esteemed Contributor
Abdul Rahiman
Posts: 232
Registered: ‎03-23-2004
Message 4 of 12 (865 Views)

Re: script insertion

use sed:
sed "s/xxxxxxxxxxxx/xxxx.xxxx.xxxx/" filename
No unix, no fun
Honored Contributor
Bharat Katkar
Posts: 2,309
Registered: ‎05-29-2003
Message 5 of 12 (865 Views)

Re: script insertion

Hi there,
1. open the file using vi
2. press : to go to command line.
3. enter command s/xxxxxxxxxxxx/xxxx.xxxx.xxxx/g
4. :wq!
to save and exit.
that's all.

Regards,
You need to know a lot to actually know how little you know
Frequent Advisor
iranzo
Posts: 85
Registered: ‎10-24-2001
Message 6 of 12 (865 Views)

Re: script insertion

Thanks,
But there are lot of line
on my file , x is variable
i want to insert . every four caracters
here my file:
512df456rt89
789gfd18gf86
12fy56cd2296
789fy12hty89

Bonjour
Honored Contributor
Mark Grant
Posts: 2,712
Registered: ‎04-01-2003
Message 7 of 12 (865 Views)

Re: script insertion

I think this user wants to insert .'s in the string.

This is a nasty way of doing it but it's all I can come up with at the moment.

STRING="123456789abcdefg";

a=`echo $STRING | cut -c1-4`
b=`echo $STRING | cut -c5-8`
c=`echo $STRING | cut -c9-12`
d=`echo $STRING | cut -c13-16`

echo "$a.$b.$c.$d"
Never preceed any demonstration with anything more predictive than "watch this"
Honored Contributor
Mark Grant
Posts: 2,712
Registered: ‎04-01-2003
Message 8 of 12 (865 Views)

Re: script insertion

Sorry, didn't realise you had a file of these and I over-did how many you want.

Here's another nasty one (sorry Merijn) that does your whole file.

perl -ne '($a,$b,$c)=($_=~/^(....)(....)(....)/);print "$a.$b.$c\n"' datafile
Never preceed any demonstration with anything more predictive than "watch this"
Trusted Contributor
Cesare Salvioni
Posts: 110
Registered: ‎02-14-2002
Message 9 of 12 (865 Views)

Re: script insertion

The solution by harry is the best one to start with because it's general: that command will substitute every kind of string (x can be any char)
Describe better what u need to subst, for instance:

# sed "s/^\(.\{4\}\)\(.\{4\}\)\(.\{4\}\)/\1.\2.\3/" < file1 > file2

would change the beginning of every line of file1 in that way, writing the new file file2
while

# sed "s/\(.\{4\}\)\(.\{4\}\)\(.\{4\}\)$/\1.\2.\3/" < file1 > file2

would change the ending part of every line of file1

If you need to change in the middle you must specify better the subst you need, even with some line of example

hope this helps
Cesare
Honored Contributor
H.Merijn Brand (procura
Posts: 6,189
Registered: ‎10-13-1997
Message 10 of 12 (865 Views)

Re: script insertion

# export x=blah
# perl -pe 's/....(?:.)/$&$ENV{x}/g' your_file
512dfblah456rtblah89
789gfblahd18gfblah86
12fy5blah6cd22blah96
789fyblah12htyblah89
#

Maybe even clearer with

# env x=. perl -pe 's/....(?:.)/$&$ENV{x}/g' your_file
512df.456rt.89
789gf.d18gf.86
12fy5.6cd22.96
789fy.12hty.89
#

Enjoy, Have FUN!
Enjoy, Have FUN! H.Merijn
Valued Contributor
rmueller58
Posts: 852
Registered: ‎02-19-2001
Message 11 of 12 (865 Views)

Re: script insertion

perl -pe -i s/xxxxxxxxxxxxx/xxx.xxx.xxx.xxx/g filename
Frequent Advisor
Dan Martin_1
Posts: 64
Registered: ‎02-19-2003
Message 12 of 12 (865 Views)

Re: script insertion

Two alternatives. The first keeps things in the shell and is the most efficient:

while read line
do
var1=${line%????????}
var3=${line#????????}
var2=${line%$var3}
var2=${var2#$var1}
echo "${var1}.${var2}.${var3}"
done < filename

The second uses awk:

awk '{print substr($0,1,4} "." substr($0,5,4} "." substr($0,9)}' filname

Dan

The opinions expressed above are the personal opinions of the authors, not of HP. By using this site, you accept the Terms of Use and Rules of Participation.