0

At first, I am pretty new into bash scripting.

So I have 2 files, lets call them A and B.

In A are URLs and in B are several logs. What I want is, insert a $actual_count after every occurrence of $element

There is only 1 $actual_count for 1 $element

IFS=',' read -r -a array < $1
for element in "${array[@]}"
do
        actual_count=`cat $2 | grep $element |grep css | wc -l`
        sed -i ':'"$element"': a '"$actual_count"'' daily_output
done

this code returns me after a found $element

all $actual_count of this loop squared and multiplied with all elements of the array.

Like (all $actual_count generated in this loop)²*sizeof array. (Thats a lot).

I don't know where I messed up that bad.

I hope you can help me.

Sorry for my bad english xD

Kind regards fune

Edit: if I use a fixed search pattern, then it works fine.

Edit 2: thx for your answer!

Soo this the file for the array:

this/is/the/first.html,this/is/the/sec.html,this/is/the/third.html,this/is/the/forth.html,this/is/the/fifth.html,an/so/on.html

the file for actual_count is a apache-access logfile.

I want my result like this:

this/is/the/first.html
2
this/is/the/sec.html
3
this/is/the/third.html
0
this/is/the/forth.html
1
...
2
  • The $1 file, it has exactly 1 line with comma-separated elements? You'll want to show us what's in A and B, and show us what the desired output should look like. May 6, 2019 at 17:56
  • Thx for your answer glenn! I posted it on as answer to my question, due the format
    – fune
    May 7, 2019 at 9:12

1 Answer 1

0

Some remarks, not only concerning your original question, but hopefully helpful:

  1. cat $2 | grep $element is a useless pipe. You can directy do grep $element $2
  2. whether grep $element |grep css makes sense is up to whether $element and css appear in a fixed order in the line. In this case it would be better to combine them to something like "$element.*css" or the other way round
  3. grep css | wc -l can be replaced by grep -c css (grep -c returns the number of matching lines)
  4. Look at your quoting: ':'"$element"': a '"$actual_count"'' consists of five elements with altering quoting (with the last one empty!). You could put everything together in double quotes to achieve the same: ":$element: a $actual_count"
  5. What do you expect the sed script to do? Let's look at the script after expansion: :this/is/the/first.html: a 2 is supposed to do what? It's just a label (jump mark) and does nothing. But actually I see no need for sed here.

To get your given output the inner part of the loop just need to output the $element and in the following line the number of matching lines:

    echo $element
    echo $(grep -c "$element.*css" $2)

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .