That is very odd. By doing echo $RESULTS without double quotes around $RESULTS, it should have flattened all whitespace for you. echo "$RESULTS" would show it on several lines but echo $RESULTS should not. Have you changed the value of IFS anywhere in your script?
Not necessarily. This is true for bash, but the OP did not mention which shell s/he is using. I just tried it with zsh, and this one does not flatten, but preserves the newlines.
@ andy391791: Did you run that scriptlet on the sample file in post#1? It will print exactly the desired fields. And, if extended by ORS=";" , it will print the desired single line. The residual flaw is the trailing semicolon...
Not so easy in /bin/sh, but quite straightforward in awk (horses for courses):
#!/usr/bin/awk -f
BEGIN {
FS=":"
}
{
join()
next
}
END {
printf "\n"
}
function join() {
gsub("\"","",$1)
printf "%s%s", semi, $1
semi = ";"
}
That will put it all on one line; if you want lines joined in batches of three, that is a little more work. The first step to solving a problem is specifying it precisely!
Assuming that there is only one quoted string before the colon (as in the sample input), it is also easy with any POSIX-conforming shell:
#!/bin/ksh
while IFS='"' read junk data junk
do printf '%s%s' "$semi" "$data"
semi=';'
done < "file.txt"
echo
This was written using a Korn shell and tested with ksh and bash with both of them producing the requested results:
Word1;Word2;This is Word3
Running it with zsh also produced the same results even though zsh obviously does not conform to the standard's quoting requirements for a shell (as mentioned in post #6 in this thread).