Simpler Navigation for Servers and Operating Systems
Completed: a much simpler Servers and Operating Systems section of the Community. We combined many of the older boards, so you won't have to click through so many levels to get at the information you need. Check the consolidated boards here as many sub-forums are now single boards.
cancel
Showing results for 
Search instead for 
Did you mean: 

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
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"
Highlighted
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
rmueller58
Valued Contributor

Re: script insertion

perl -pe -i s/xxxxxxxxxxxxx/xxx.xxx.xxx.xxx/g filename
Dan Martin_1
Frequent Advisor

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