pancho
pancho

Reputation: 186

Problems using grep and sed in bash

I'm extracting domains, subdomains and ips from a text file using:

grep -oE '[[:alnum:]]+[.][[:alnum:]_.-]+' "extra-domains.txt" | sed 's/www.//' | sort -u > outputfile.txt

And I'm using this bash to run it quicker as: extract-domains.sh text-with-domains.txt

#!/bin/bash
FILE="$1"
while read LINE; do
  grep -oE '[[:alnum:]]+[.][[:alnum:]_.-]+' "$LINE" | sed 's/www.//' | sort -u > outputfile.txt
done < ${FILE} 

but I keep getting multiple errors with "No such file or directory" when running the bash.

Can anyone give me a hand? Thanks.

Upvotes: 0

Views: 212

Answers (1)

Camion
Camion

Reputation: 1384

The way you wrote it, grep takes "$LINE" as a filename. Is that what it is supposed to do ?

edit : There is no point in making a while loop and reading your file line by line. It will be much slower. You should probably write you script like this:

#!/bin/bash
grep -oE '[[:alnum:]]+[.][[:alnum:]_.-]+' "$1" | 
    sed 's/www.//' | 
    sort -u

and call it :

extract-domains.sh "extra-domains.txt" > outputfile.txt

Upvotes: 3

Related Questions