Hi,
I want to split the length of a line based on the numbe of positions.
eg: ABCBEFGH IJKLMN asdfas
I want to split every 4 characters into a new line
Can i use cut or any easy way of using a single or 2 lines.
Thanks
Vijay
Hi,
I want to split the length of a line based on the numbe of positions.
eg: ABCBEFGH IJKLMN asdfas
I want to split every 4 characters into a new line
Can i use cut or any easy way of using a single or 2 lines.
Thanks
Vijay
> echo "ABCDEFGHIJKLMNOPQRSTUVWXYZ" | sed -e "s/..../&~/g" | tr "~" "\n"
ABCD
EFGH
IJKL
MNOP
QRST
UVWX
YZ
> echo "ABCDEFGHIJKLMNOPQRSTUVWXYZ ABC" | sed -e "s/..../&~/g" | tr "~" "\n"
ABCD
EFGH
IJKL
MNOP
QRST
UVWX
YZ A
BC
If you have a recent version of bash try:
string="ABCDEFGHIJKLM"
while [ ${#string} -gt 0 ]
do
echo "${string:0:4}" # 4 characters
string="${string:4}"
done
"recent" = any version since bash-2.0 of 1996.
In any POSIX shell:
string="ABCDEFGHIJKLM"
while [ ${#string} -ge 4 ]
do
temp=${string#????}
printf "%s\n" "${string%"$temp"}"
string=$temp
done
[ -n "$string" ] && printf "%s\n" "$string"
Thanks all.