Operating System - HP-UX
1826004 Members
3313 Online
109690 Solutions
New Discussion

need to insert a / in a word multiple times - script

 
SOLVED
Go to solution
Ratzie
Super Advisor

need to insert a / in a word multiple times - script

I have a file which contains multiple lines.
Each line is (in all essence) one word.

I need to insert a / after the:
2nd character
6th
12th
15th
and 19th character
10 REPLIES 10
Charles McCary
Valued Contributor

Re: need to insert a / in a word multiple times - script

NOt the most efficent answer but will work:

cat FILE | while read LINE
do
F1=`echo $LINE | cut -c1-2`
F2=`echo $LINE | cut -c3-6`
...

echo "${F1}/${F2}......." > NEWFILE

done

Rodney Hills
Honored Contributor
Solution

Re: need to insert a / in a word multiple times - script

Here is a short perl command line-
perl -p -e 'print join("/",unpack("a2a4a6a3a4a999",$_))' outfile

HTH

-- Rod Hills
There be dragons...
Rodney Hills
Honored Contributor

Re: need to insert a / in a word multiple times - script

Oops, should have been-
perl -n -e 'print join("/",unpack("a2a4a6a3a4a999",$_))' outfile

PS-
To write the data back to the original file, do-
perl -i -p -e 'print join("/",unpack("a2a4a6a3a4a999",$_))' yourfile

Of course "yourfile" can be replaced with a wildcard name to do multiple files.

Rod Hills
There be dragons...
Charles McCary
Valued Contributor

Re: need to insert a / in a word multiple times - script

You could also use awk (solution below assumes that the field separator is a space, if there are no spaces then all data is $1:

cat $INFILE | awk '

BEGIN { FS = " " }

{ F1=substr($1,1,2)
F2=substr($1,3,6)
F3=substr($1,7,12)
...
F4=
F5
}

{ printf F1 }
{ printf "/" }
{ printf F2 }
...
{ printf F5 }
{ printf "\n" }

}'
Arturo Galbiati
Esteemed Contributor

Re: need to insert a / in a word multiple times - script

Hi,
you can use:
echo "$(cut -c 1-2 f)/$(cut -c 3-6 f)/$(cut -c 7-12 f)/$(cut -c 13-15 f)/$(cut -c 16-19)/"

HTH,
Art
Muthukumar_5
Honored Contributor

Re: need to insert a / in a word multiple times - script

You can use sed / perl to do this easily,

# echo "123456789abcdefghij12" | wc -c
22
# echo "123456789abcdefghij12" | sed 's%^\(.\{2\}\)\(.\{4\}\)\(.\{6\}\)\(.\{3\}\)\(.\{4\}\)\(.*\)%\1/\2/\3/\4/\5/\6%g'
12/3456/789abc/def/ghij/12

# perl -pi -e '' filename
will update automatically.

hth.
Easy to suggest when don't know about the problem!
Muthukumar_5
Honored Contributor

Re: need to insert a / in a word multiple times - script

Example with perl:

# cat > file
hibyehibyehobyehibyeokok
okokokokokokokokokokokok

# perl -pe 's%^(.{2})(.{4})(.{6})(.{3})(.{4})(.*)%\1/\2/\3/\4/\5/\6%g' file
hi/byeh/ibyeho/bye/hiby/eokok
ok/okok/okokok/oko/koko/kokok

# perl -pi -e 's%^(.{2})(.{4})(.{6})(.{3})(.{4})(.*)%\1/\2/\3/\4/\5/\6%g' file
# cat file
hi/byeh/ibyeho/bye/hiby/eokok
ok/okok/okokok/oko/koko/kokok
#

-Muthu
Easy to suggest when don't know about the problem!
Adam Harvey_1
New Member

Re: need to insert a / in a word multiple times - script

If you have /usr/dt/bin/dtksh, you can use some its advanced environment variable processing features...

Create a file containing the following:

#!/usr/dt/bin/dtksh
for X in $(<${1:-/dev/null});do
echo "${X:0:2}/${X:2:4}/${X:6:6}/${X:12:3}/${X:15:4}/${X:19}"
done

Call it 'myscript.sh' for example.

Run it using 'myscript.sh '.

Where 'filename' is the name of the file you want to process.

Adam Harvey

PS: The weirdness involving /dev/null prevents the script hanging if you do not supply a file-name.
Kent Ostby
Honored Contributor

Re: need to insert a / in a word multiple times - script


awk '{printf("%2s/%4s/%6s/%3s/%4s/%s \n",substr($1,1,2),substr($1,3,4),substr($1,7,6),substr($1,13,3),substr($1,16,4),substr($1,20))}' < useme
"Well, actually, she is a rocket scientist" -- Steve Martin in "Roxanne"
Ratzie
Super Advisor

Re: need to insert a / in a word multiple times - script

Thanks for all the help