3

Wow, I could not think of a good way to title this question. Basically I have a file called "attendance" with data like this:

11/06/2014 101.11.001.01 FirstName LastName
11/06/2014 101.11.001.01 FirstName LastName
11/06/2014 101.11.001.01 FirstName LastName
11/06/2014 101.11.001.01 FirstName LastName

Basically it's the date, IP address, First name, and Last name. The above is how it is formatted in the "attendance" file. I have created an html page with text boxes and a submit button where a user can either: A.) Enter a FN/LN and receive a list of dates that person logged in, OR B.) Type in a date and receive a list of users who logged in on that date.

I'm getting the results I want, but the result of the grep displayed in the browser is all on one line, like this:

11/06/2014 101.11.001.01 FirstName LastName 11/06/2014 101.11.001.01 FirstName LastName 11/06/2014 101.11.001.01 FirstName LastName 11/06/2014 101.11.001.01 FirstName LastName

Obviously this is sub-optimal. I need the grep results to appear on separate lines. Below is my .cgi file. "getvars" is just a script the professor made for converting variable types. Also, I'm cutting corners by only grepping last name, because nobody has the same last name:

#!/bin/bash

. ~/bin/getvars

if [ ! -z "$LN" ]; then
cat attendance | grep $LN

elif [ ! -z "$DATE" ]; then
cat attendance | grep $DATE

else echo No Records Found

fi

I've tried to be as concise as possible and I apologize is anything doesn't make sense. My only question is: How do I get the grep results on separate lines?

  • hm, when I test it in a shell it shows up on different lines as expected.. Even when I redirect it into a file. I'm guessing either you have something odd with your grep (unlikely I'd think) or the way you display the text is doing it.. – Seth Nov 7 '14 at 0:39
  • What is the name of the file you are saving the lines in? Does it have an html extension? – terdon Nov 7 '14 at 0:46
  • Seth - Yes, I did the same grep just in the shell and it looked fine, until its out to the browser – Shane Nov 7 '14 at 0:46
  • You need to add <br> between each line. Also need to change all occurrences of & < > to their HTML entity equivalents. – Mark Plotnick Nov 7 '14 at 0:48
  • I think Mark might be correct, when you pass grep's output into the webpage (I'm assuming it is HTML) the linebreaks aren't necessarily going to be preserved. – Seth Nov 7 '14 at 0:52
2

Presumably, your browser is interpreting whatever file you're pointing it to as HTML. In HTML a newline is not \n but the <br> tag, so you would need to add that. For example:

#!/usr/bin/env bash

. ~/bin/getvars

if [ ! -z "$LN" ]; then
  grep "$LN" attendance | sed 's/$/<br>/'
elif [ ! -z "$DATE" ]; then
  grep "$DATE" attendance | sed 's/$/<br>/'
else 
   echo No Records Found
fi

On my Debian, firefox interprets \n as line breaks if the file has no .html extension (I tried with no extension and .txt) so that might be a workaround but I have no idea how portable that would be.

A better alternative would be to make it a proper HTML page, use <pre> tags and replace all < and > with their HTML equivalents:

#!/usr/bin/env bash

. ~/bin/getvars

echo "<HTML><BODY><pre>"
if [ ! -z "$LN" ]; then
  grep "$LN" attendance | sed 's/>/\&gt;/g;s/</\&lt;/g;'
elif [ ! -z "$DATE" ]; then
  grep "$DATE" attendance | sed 's/>/\&gt;/g;s/</\&lt;/g;'
else 
   echo No Records Found
fi
echo "</pre></BODY></HTML>
  • Wow! thank you terdon. That worked. We talked about "sed" in class, but it's still mystifying to me. It seems to be pretty useful. – Shane Nov 7 '14 at 1:13
  • @Shane yes, sed is very powerful. Have a look through some of the sed Q&As. What I'm using here is the substitution operator, general syntax: s/foo/bar/ which will change foo to bar. The /g in the answer makes it replace all occurrences in the line. The & is a special character for sed (it means "whatever I just matched") so I need to escape it (\&). – terdon Nov 7 '14 at 1:20
  • you really do not need to even call grep in your case. sed is a pattern matching/replacing tool. It can do a lot of very cool things. check this link out grymoire.com/Unix/Sed.html be be on your way to greatness :) It also goes over another really powerful tool called awk. enjoy. – Rob Nov 7 '14 at 1:46
  • @terdon If sed used there is no need to use grep? Just propose: sed -n '/$var/{s/>/\&gt;/g;s/</\&lt;/g;p}'. Secondary if we do the same operations may be it will be better to use for var in "$LN" "$DATE"? – Costas Nov 7 '14 at 9:37
  • @Costas yes, I know I don't need grep (I don't know why you felt you had to repeat that, didn't you see Rob's comment?). I don't need sed either, I could have done the whole thing in perl or awk. Hell, I could have done it all in a bash while loop. However, I try to keep to the OP's code as much as possible because i) I don't know their level of understanding and want my answer to be understood and ii) this may be just a simplified example. Personally, I would have done this as you suggest but... Oh, and you would need double quotes on your sed for it to work. – terdon Nov 7 '14 at 10:50

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.