Hi
I have a file profile.txt with the below input:
{"atgUserId":"736f14c4-eda2-4531-9d40-9de4d6d1fb0f","firstName":"donna","lastName":"biehler","email":"schoolathome42@live.com","receiveEmail":"y
es"},
{"atgUserId":"c3716baf-9bf8-42da-8a44-a13fff68d20f","firstName":"Gilberto Ramon","lastName":"Trevino Fuerte","email":"gtrevino@dfasc.com","recei
veEmail":"yes"}
I am writing a shell script in for to print the lines one by one from profile.txt including braces via:
for value in $(awk -F"{|}" '{printf "{"$2"}"}' profile.txt) {; do echo $value; done
But it is giving me a broken output as;
{"atgUserId":"736f14c4-eda2-4531-9d40-9de4d6d1fb0f","firstName":"donna","lastName":"biehler","email":"schoolathome42@live.com","receiveEmail":"yes"}{"atgUserId":"c3716baf-9bf8-42da-8a44-a13fff68d20f","firstName":"Gilberto
Ramon","lastName":"Trevino
Fuerte","email":"gtrevino@dfasc.com","receiveEmail":"yes"}
{
Note that the Gilberto and Ramon have separated? How to resolve this?
what's the desired out give your sample input?
Desired output should be:
{"atgUserId":"736f14c4-eda2-4531-9d40-9de4d6d1fb0f","firstName":"donna","lastName":"biehler","email":"schoolathome42@live.com","receiveEmail":"yes"}{"atgUserId":"c3716baf-9bf8-42da-8a44-a13fff68d20f","firstName":"Gilberto Ramon","lastName":"Trevino Fuerte","email":"gtrevino@dfasc.com","receiveEmail":"yes"}
Instead of the below one:
{"atgUserId":"736f14c4-eda2-4531-9d40-9de4d6d1fb0f","firstName":"donna","lastName":"biehler","email":"schoolathome42@live.com","receiveEmail":"yes"}{"atgUserId":"c3716baf-9bf8-42da-8a44-a13fff68d20f","firstName":"Gilberto
Ramon","lastName":"Trevino
Fuerte","email":"gtrevino@dfasc.com","receiveEmail":"yes"}
Whenever there is a space between a name it breaks the line
So you just want to remove all newlines?
tr -d '\n' < infile > outfile
I don't quite see how the name gets broken into 2 parts given your sample input, but try this:
awk '{printf substr($0,1, length-1)}END{printf ORS}' myFile
---------- Post updated at 03:48 PM ---------- Previous update was at 03:47 PM ----------
and remove the trailing ,
(I think)
Thanks to both of you...
I got the answer as to why it was splitting.
By default, a Bash for loop splits on all whitespace. You can override that by setting the IFS variable:
IFS=$'\n'
for i in `cat r.txt`; do echo "$i"; done
unset IFS
Source:
osx - in bash, "for;do echo;done" splits lines at spaces - Stack Overflow
IFS stands for Input Internal Field Separator - it's a character that separates fields.
IFS=$'\n' # make newlines the only separator
Your question did not involve a bash for loop. Had you showed what you were actually doing, we might have guessed.
You shouldn't be doing that anyway. That's a useless use of cat, a dangerous use of backticks, and waste of memory (slurping an entire file into memory to deal with it). You didn't find the shell script construct actually intended for this job:
while read ENTIRELINE
do
echo "line read: $LINE"
done < inputfile