0

I have 2 files with different data. How can I put data in those files and print it? I was trying for loop, but it is only good for 1 variable.

FileA
https://xyx.com/test-posts/
https://www.abc.com/temp-article/

FileB
xyx.com
abc.com

I want to achieve something like below

Sample

<a href="https://xyx.com/test-posts/">xyx.com</a>
<a href="https://www.abc.com/temp-article/">abc.com</a>

Thanks in advance!

  • Please read How to Ask -- you need to provide a lot more details about what you're doing: what are your input files and what is your desired output? – glenn jackman Mar 9 at 12:45
  • Please edit your question and show us i) a few lines of each of your two input files and ii) the output you want to get from those example lines. We can't help you manipulate data you don't show. – terdon Mar 9 at 13:17
  • 2
    What does file B have to do with anything? It seems to me that the output could be generated using only file A? – Jeff Schaller Mar 9 at 14:15
  • @JeffSchaller Thanks for checking this. Please let me know how we can achieve that with only A file? – Pawan Mar 9 at 14:40
  • It looked to me like the target domain was simply repeated in the link text. – Jeff Schaller Mar 9 at 15:05
2

How about

$ paste FileA FileB | awk '{print "<a href=\"" $1 "\">" $2 "</a>"}'
<a href="https://xyx.com/test-posts/">xyx.com</a>
<a href="https://www.abc.com/temp-article/">abc.com</a>

Note: you would need to do something more sophisticated (e.g. selecting a different delimiter for both paste and awk) if your file entries may contain whitespace - I assume that is not the case here since they are URLs

  • Great! Working like charm. – Pawan Mar 9 at 14:55
1
$ while read -r url && read -r domain <&3; do printf '<a href="%s">%s</a>\n' "$url" "$domain"; done <FileA 3<FileB
<a href="https://xyx.com/test-posts/">xyx.com</a>
<a href="https://www.abc.com/temp-article/">abc.com</a>

Or, as you would write it in a script:

while read -r url && read -r domain <&3; do
    printf '<a href="%s">%s</a>\n' "$url" "$domain"
done <FileA 3<FileB

This is a while loop that loops until either of two read calls fails to read a complete line. The first read reads the URL from FileA while the second read reads the domain from FileB (via file-descriptor 3).

The output is handled by a printf call that inserts the read data into a formatting string.

0

I was able to accomplish this with the following script:

#!/bin/sh

filea=/path/to/filea
fileb=/path/to/fileb

lines=$(grep -c . "$filea")

for ((i=1; i<=lines; i++)); do
    url=$(sed -n "${i}p" "$filea")
    name=$(sed -n "${i}p" "$fileb")
    printf "<a href=\"${url}\">${name}</a>\n"
done

lines will be set to the total number of lines in filea

We then loop through from 1 to n-lines and extract that line from each of filea and fileb using sed. Then print the required href statement using that extracted data.

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.