hello,
How can i split this..
10.25.10.2
two octet
a=2
b=5
Thank you...
hello,
How can i split this..
10.25.10.2
two octet
a=2
b=5
Thank you...
What have you tried?
This should be a pretty simple thing to do with cut or awk commands.
i maked this.
#!/usr/bin/env bash
read ip
oktet1=`echo $ip | cut -d \. -f 1`
oktet2=`echo $ip | cut -d \. -f 2`
oktet3=`echo $ip | cut -d \. -f 3`
oktet4=`echo $ip | cut -d \. -f 3`
echo $oktet1
echo $oktet2
echo $oktet3
echo $oktet4
but i want cut secon octet on ip (eg:192.168.1.1)
168
a=1
b=6
c=8
thank you for your reply.
You did not show the output of your commands.
Does you second command give you the second number you wish to split?
oktet2=`echo $ip | cut -d \. -f 2`
If so, then perhaps split that variable oktet2...
example:
read ip
192.168.1.1
oktet1=`echo $ip | cut -d \. -f 1`
oktet2=`echo $ip | cut -d \. -f 2`
oktet3=`echo $ip | cut -d \. -f 3`
oktet4=`echo $ip | cut -d \. -f 3`
echo $oktet1
echo $oktet2
echo $oktet3
echo $oktet4
192
168
1
1
I want to shred "168" Is it possible?
IFS="." read A B C D
No externals needed.
Rearrange A B C D however you please to get the order you want.
Thanks Corona688..
i make this
IFS="." read A B C D
echo $A
echo $B
in> 192.168.1.1
output>
192
168
I already do that. I also want to separate the number 168
example>
1
6
8
No you hadn't, you were using four inefficient externals to do that, but to split one string, use the substring operator:
echo ${B:0:1}
Here is another approach using awk:-
echo "192.168.1.1" | awk -F'.' '
{
for ( i = 1; i <= NF; i++ )
{
for ( j = 1; j <= length($i); j++ )
{
A_oct[i OFS j] = sprintf ( "%d", substr( $i, j, 1 ) )
A_max = j
}
}
}
END {
for ( i = 1; i <= NF; i++ )
{
print "Octet:", i
for ( j = 1; j <= A_max; j++ )
{
print A_oct[i OFS j]
}
}
}
'
Produces output:-
Octet: 1
1
9
2
Octet: 2
1
6
8
Octet: 3
1
Octet: 4
1
Wow .. great .. thank you very much...
How can I assign these final results to the variable_
Octet: 1
1=A
9=B
2=C
Octet: 2
1=D
6=E
8=F
echo "exampletext $A examples text $B"
C="${B:0:1}"
thanks. but dont worked...
`Bad substitution` error...
Then you're not really in BASH.
ksh has it too, but only modern ksh, not ksh88.
My OS kalilinux.. my script is working on shell
The (admittedly implied) question was: What shell do you use?
Try this that should run on the most basic Bourne shell:
IFS="." read OCT1 OCT2 OCT3 OCT4 <<EOF
$IP
EOF
while [ ${#OCT2} -gt 0 ]; do echo ${OCT2%${OCT2#?}}; OCT2=${OCT2#?}; done
1
6
8
thank you... its work...
this output:
1
6
8
Can I assign a variable? To use echo command in text?
What shell do you use?
If you don't know, could you find out?
An array would be most suited to be assigned to, but sh
doesn't have them. As your octet can be 1, 2, or 3 digits, it's not that easy to assign consistently, e.g. A
to contain the hundreds digit always. Maybe you start from the back and assign to C
?
Like
C=${OCT2#${OCT2%?}}
OCT2=${OCT2%?}
B=${OCT2#${OCT2%?}}
A=${OCT2%?}
This is right but you can pad by adding zeroes at the begin and then get the characters one at a time from right to left until you hit zero:
NUM=1
# NUM=12
# NUM=123
XNUM="00$NUM"
echo ${XNUM#${XNUM%?}} # rightmost char
...etc.
(caution: not tested with a real Bourne shell as i do not have such a thing)
(addendum: if you use ksh (any ksh) you can typeset -RZ3
the variable and don't need the padding)
I hope this helps.
bakunin
Yes, of course. I skipped that for readability. You can lead in the code snippet in post#18 with
OCT2=00$OCT2
OCT2=${OCT2#${OCT2%???}}
to have a three digit octet with leading zeroes if need be.