0

Why does this only show the entry from data.csv one time?

for i in `cat ~/Downloads/data.csv`
do
  echo $i $i
done

I have tried $i$i and the += operator and none of them seem to do what I would have expected. The goal is to create links from data.csv, which is a list of urls. So the output would ideally be

<a href='$i'>$i</a>

I am using OSX.

Scott C Wilson
  • 19,102
  • 10
  • 61
  • 83
  • 2
    See [Bash FAQ 001](https://mywiki.wooledge.org/BashFAQ/001) for the correct way to iterate over the contents of a file. – chepner Sep 03 '18 at 12:14
  • 5
    Your CSV file probably has DOS line endings, so the carriage return in the variable value causes the second one to overwrite the first. – chepner Sep 03 '18 at 12:15
  • 1
    I'm sure you know that already, but it's maybe also a good hint for others: Caution on the output of `'$i'`. The single quotes have to be escaped or double quotes should be used to resolve variable correctly. – colidyre Sep 03 '18 at 12:17
  • @chepner Would you please make your comment into an answer? That was indeed the root cause of my trouble. Thanks to other posters though for improving my lazy `for` loop use. – Scott C Wilson Sep 03 '18 at 12:36

1 Answers1

1

don't use bash for file content processing, awk is better suited for this.

$ awk '{print "<a href=\"" $0 "\">" $0 "</a>"}' file
karakfa
  • 66,216
  • 7
  • 41
  • 56