1748213 Members
3110 Online
108759 Solutions
New Discussion юеВ

Re: script insertion

 
SOLVED
Go to solution
iranzo
Frequent Advisor

script insertion

Hello,
On HPUX ksh :
On a file,i want to transform string
xxxxxxxxxxxx
with ( insert .)
xxxx.xxxx.xxxx
Thanks a lot.
Bonjour
11 REPLIES 11
John Carr_2
Honored Contributor

Re: script insertion

Hi

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

John.
harry d brown jr
Honored Contributor
Solution

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
Abdul Rahiman
Esteemed Contributor

Re: script insertion

use sed:
sed "s/xxxxxxxxxxxx/xxxx.xxxx.xxxx/" filename
No unix, no fun
Bharat Katkar
Honored Contributor

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
iranzo
Frequent Advisor

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
Mark Grant
Honored Contributor

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"
Mark Grant
Honored Contributor

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"
Cesare Salvioni
Trusted Contributor

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
H.Merijn Brand (procura
Honored Contributor

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