0

I currently have a text file called student.txt and there are some records in the text file. The format of the records are StudentID|Name|ContactNumber|EmailAddress and the records are as follows:

21PMR07222|Steven|011-1234567|steven@yahoo.com
21PMR07123|John|012-3456789|john@yahoo.com
21PMR07221|Amy|017-4567890|amy@gmail.com

User is allowed to search the students details using studentID. I have the following codes to display the student details if the studentID was found, else it displays error message.

output=$(awk -F'|' -v id="${studentID^^}" '$1 == id { found=1; printf "Name: \033[1m%s\033[0m Contact Number: \033[1m%s\033[0m Email Address: \033[1m%s\033[0m", $2, $3, $4 } END { if (found==0) { printf "The student ID entered does not exist! Please enter again." }}' student.txt)

And currently the output is shown as below (the details are displayed in bold):

Name: **Steven** Contact Number: **011-1234567** Email Address: **steven@yahoo.com**

May I know how can I let the output shown as below? (I do not want the details to display in one whole line, I want the details to be displayed in separate lines and the details are displayed in bold)

Name: **Steven**
Contact Number: **011-1234567** 
Email Address: **steven@yahoo.com**
  • 2
    Simply add `'\n'` wherever you want the newline, e.g. `"Name: \033[1m%s\033[0m \n...` (note ANSI escapes are not portable, they depend on VT terminal support. Unless you have to have the color and/or bold, recommend you ditch it.) – David C. Rankin Mar 29 '22 at 05:01
  • I have tried putting `\n` but it still the same – Steven Thoo Mar 29 '22 at 05:04
  • 2
    `awk -F'|' -v id="${studentID^^}" '$1 == id { found=1; printf "Name: \033[1m%s\033[0m\nContact Number: \033[1m%s\033[0m\nEmail Address: \033[1m%s\033[0m\n", $2, $3, $4 } END {if (!found) print "The student ID entered does not exist! Please enter again."t exist! Please enter again."}' file` should work I believe. – anubhava Mar 29 '22 at 05:05
  • 1
    This works fine `printf "Name: \033[1m%s\033[0m\nContact Number: \033[1m%s\033[0m\nEmail Address: \033[1m%s\033[0m\n", $2, $3, $4` – David C. Rankin Mar 29 '22 at 05:06
  • 1
    That's odd. I ran the following complete test `studentID=21PMR07123; echo "21PMR07123|John|012-3456789|john@yahoo.com" | awk -F'|' -v id="${studentID^^}" '$1 == id { found=1; printf "Name: \033[1m%s\033[0m\nContact Number: \033[1m%s\033[0m\nEmail Address: \033[1m%s\033[0m\n", $2, $3, $4 } END { if (found==0) { printf "The student ID entered does not exist! Please enter again." }}'` Worked fine. I suspect your terminal doesn't support the ANSI escapes or some other weirdness. You can copy/paste the test above to check. – David C. Rankin Mar 29 '22 at 05:08
  • Linux (Ubuntu 20) – Steven Thoo Mar 29 '22 at 06:05
  • It's a virtual machine – Steven Thoo Mar 29 '22 at 06:07
  • Okay, now we are getting somewhere. I'm surprised it accepted the ANSI escapes for bold, but that is a good sign. If it is outputting, e.g. `**Steven**` instead of **Steven**, then it is NOT interpreting the escapes for bold -- and that is likely screwing up the newline interpretation. Try just `printf "Name: %s\nContact Number: %s\nEmail Address:%s\n", $2, $3, $4` and report back. You can also try `print` alone, e.g. `print "Name: " $2 ORS "Contact Number: " $3 ORS "Email Address: " $4` – David C. Rankin Mar 29 '22 at 06:10
  • I have tried both, it seems like it cannot create newline – Steven Thoo Mar 29 '22 at 06:16
  • I think I have figured it out. When I want to display `output`, I accidentally put `echo $output` instead of `echo "$output"` (missing double quotes) – Steven Thoo Mar 29 '22 at 06:31
  • 2
    Shit! You have a [BashFAQ #50](http://mywiki.wooledge.org/BashFAQ/050) problem where word-splitting on `echo ...` output is occurring. I'm sorry I missed the fact you were using a *command substitution* `output=$(...)`. Yes, that's the problem. You can double-quote `"$output"` to prevent word splitting. Lesson -- don't store multi-line output in a single variable -- without considering word-splitting that will occur when that variable is used as input to another process. – David C. Rankin Mar 29 '22 at 06:34

0 Answers0