Code: Select all
A='1'\x0a'2'
Code: Select all
echo '1'\x0a'2'
I use to use a real LineFeed in the string, but it doesn`t work anymore.
Code: Select all
A='1'\x0a'2'
Code: Select all
echo '1'\x0a'2'
Code: Select all
A=`echo -e '1\n2'`
Code: Select all
A="1
2
3
"
echo "$A"
#or
echo "${A}"
Code: Select all
echo '1, 2, 3' |sed 's/, /
/'
echo "1,2,3,4" | sed 's/,/\x0a/g'sunburnt wrote:I use to use this, but it doesn`t work anymore:.Code: Select all
echo '1, 2, 3' |sed 's/, / /'
It does recognize it. Echo -e interprets it as a hex.sunburnt wrote:jpeps; Yes, it`s interesting that sed and echo -e recognizes the hex
representation of a character, but the Bash command line doesn`t.
It seems like this would have been corrected a long time ago.
Code: Select all
A="1,2,3,4"
echo "${A//,/
}"
Code: Select all
A=`somecommand`
echo "${A//,/
}"
only problem is 7 extra lines, one for the linefeed and five to explain that it contains invisible code:technosaurus wrote:why didn't you say so - here is the portable way to do that
Is "echo -e" also to be regarded as separate from Bash?sunburnt wrote:I tend to think of cp, ls, sed, grep, awk, etc. as separate from Bash.
Code: Select all
# A="1,2,3,4"
# echo -e "${A//,/\x0a}"
Code: Select all
# string='a,b,c,d,e'
# echo -e "${string//,/\n}"
a
b
c
d
e
# printf "${string//,/\n}\n"
a
b
c
d
e
Code: Select all
# IFS=, read -ra array <<<"$string"
# for i in "${!array[@]}";do printf "\${array[$i]}=\"${array[$i]}\"\n";done
${array[0]}="a"
${array[1]}="b"
${array[2]}="c"
${array[3]}="d"
${array[4]}="e"
# new_string=$(IFS=$'\n';printf "${array[*]}")
# printf "$new_string\n"
a
b
c
d
e
Code: Select all
# another_string=$(printf "%s" "${array[@]/%/\n}")
# printf "$another_string"
a
b
c
d
e
Code: Select all
# yet_another_string=$(IFS=;printf "${array[*]/%/\n}")
# printf "$yet_another_string"
a
b
c
d
e# printf "$yet_another_string"|od -acx
0000000 a nl b nl c nl d nl e nul
a \n b \n c \n d \n e \0
0a61 0a62 0a63 0a64 0065
0000011
Code: Select all
# yet_another_string=$(IFS=;printf "%s" "${array[*]/%/\n}")
# printf "$yet_another_string"|od -acx
0000000 a nl b nl c nl d nl e nl
a \n b \n c \n d \n e \n
0a61 0a62 0a63 0a64 0a65
0000012
Code: Select all
string="Vertical"
echo -n "string" | sed 's/./&@/g '| tr '@' '\n'
Code: Select all
string="Vertical"
echo -n "string" | sed 's/./&@/g '| tr '@' '\012'
Code: Select all
string="Vertical"
echo -n "string" | sed 's/./&@/g; s/@$//' | tr '@' '\012'
string="Verticaltabs"
echo -n "string" | sed 's/./&@/g '| tr '@' '\013'
string="bigtabs"
echo -n "string" | sed 's/./&@/g '| tr '@' '\011'
Code: Select all
# yas=$(set a b c d e;printf "%s\n" "$@")
# printf "$yas\n"
a
b
c
d
e
Code: Select all
# sed 's/,/\n/g' <<<"a,b,c,d,e"
a
b
c
d
e
Code: Select all
#!/bin/ash
tail_help(){
echo "tail -[cnqv] files"
exit
}
[ $1 ] || tail_help
while ([ $# > 0 ]) do
case $1 in
-h|--help)tail_help;;
--version)echo "tail v0.0";exit;;
-c)BYTES=${2};shift;shift;;
--bytes=*)BYTES=${1%=*};shift;;
-n)LINES=${2};shift;shift;;
--lines=*)LINES=${1%=*};shift;;
-q|--quiet|--silent)QUIET=1;shift;;
-v|--verbose)VERBOSE=1;shift;;
#todo -f and -s
*)[ -f ${1} ] && break || tail_help;;
esac
done
#for -f while goes here
for FILE in $@; do
HEADER="==> "$FILE" <=="
[ ! $QUIET ] && [ $# -gt 1 ] || [ $VERBOSE ] && echo $HEADER
# while read LINE || [ $LINE ] && echo $LINE && LINE=""; do echo $LINE; done <"${FILE}"
A=`while read -r LINE; do echo $LINE; done <"${FILE}";echo $LINE`
[ ${#A} -lt 1 ] && echo && continue
if [ $BYTES ];then
#for -f compare BYTES to new $#A - old $#A and print the lesser
i=$((${#A}-$BYTES))
echo "${A:$i:$BYTES}"
else
OLDIFS=$IFS
IFS="
"
set ${A}
#for -f get size here and compare to last
LINES=${LINES:-10}
shift $(($#-$LINES))
for LINE in $@; do
echo "${LINE}"
done
IFS=$OLDIFS
fi
done
hey potong nice thanks and without commas needed to separatePotong
p.s. big_bass: if your using gnu sed
Code:
# sed 's/,/\n/g' <<<"a,b,c,d,e"
a
b
c
d
e
works too!
Code: Select all
sed 's/./&\n/g' <<<"string"