0

I am trying a find a strings of a file matching with another file and collecting them into an array. As a first step, I tried to find a string matching of two files. I used below code, but strangely I am getting this message grep: 3: No such file or directory,grep: 5: No such file or directory. I could not figure out the rootcause for this. Can some one help me finding the rootcause? Shell script used:

#!/bin/bash
while
read LINE
do
if grep -q $LINE /tmp/UA_Automation/glhard; then
echo "$LINE"
echo "matched"
else
echo "$LINE"
echo "not_matching"
fi
done < /tmp/UA_Automation/UA_daily_listings_unique_count.csv|awk '{ print $1 }'

glhard & UA_daily_listings_unique_count.csv are name of the file. Contents of these two files respectively are:

glauto
gltyre
gldisc
glclothes    


glwheel 2
glgun 3
glauto 4
gldisc 4
glpants 6

Can someone help me figure out this?

Thomas Ayoub
  • 29,063
  • 15
  • 95
  • 142
user3624000
  • 291
  • 1
  • 5
  • 17
  • for condition checking purpose. Is this wrong? sorry i don't know bash well. – Avinash Raj Oct 30 '14 at 15:16
  • It looks like you will be able to do what you want within a single awk one-liner. If you explain what it is you're trying to do in the long-run we will be able to help you more. – Tom Fenech Oct 30 '14 at 15:17
  • @TomFenech: Any idea what is the rootcause? – user3624000 Oct 30 '14 at 15:17
  • The file with all the patterns has numbers on each line, but the file you're searching doesn't have these numbers. So nothing will match them. Do you want to ignore the numbers? – Barmar Oct 30 '14 at 15:19
  • @Avinash `if` checks the return code of the following expression. The return code of `grep -q` will indicate whether there was a match or not. `[[` should not be used in this case. – Tom Fenech Oct 30 '14 at 15:20
  • @Tom: I want to collect contents of file:UA_daily* which are matching with contents of file:glhard. Contents of both files are different, but may contain common strings. When the contents are matching, as a next step, I would be storing them like `{c[$1]++} END` as there may be number of matches. – user3624000 Oct 30 '14 at 15:22
  • @Barmar: I used `awk '{ print $1 }'` just to ignore the numbers when it goes into `while` loop – user3624000 Oct 30 '14 at 15:24
  • 1
    `awk` is getting the output of the while loop, it's not being used for the input. – Barmar Oct 30 '14 at 15:25
  • @user3624000 You should edit your question to include these details – Tom Fenech Oct 30 '14 at 15:28

2 Answers2

1

The value of LINE contains whitespace; you need to quote it so that the entire value is used as the pattern, not just the first word after word-splitting.

if grep -q "$LINE" /tmp/UA_Automation/glhard; then

That said, I'd recommend looking at diff or comm for such line-by-line comparisons of files, rather than running grep for each line.

chepner
  • 497,756
  • 71
  • 530
  • 681
1

You're piping the output of the loop to awk. You want to go the other way:

awk '{print $1}' /tmp/UA_Automation/UA_daily_listings_unique_count.csv | while read LINE
do
    if grep -q $LINE /tmp/UA_Automation/glhard; then
        echo "$LINE"
        echo "matched"
    else
        echo "$LINE"
        echo "not_matching"
    fi
done

Although you could also make use of read splitting the line:

while read LINE NUMBER
do 
    ...
done < /tmp/UA_Automation/UA_daily_listings_unique_count.csv
Barmar
  • 741,623
  • 53
  • 500
  • 612
  • Thanks. I will try your suggestion. – user3624000 Oct 30 '14 at 15:36
  • That worked. Thank you. As a next step, I have collected matching strings into an array. I declared global array(outside of while loop) so that I can use them wherever I want globally. But outside of while loop, its not giving the contents I expected. I used like this `array=() awk '{ print $1}' /tmp/UA_Automation/UA_daily_crawled_listings_unique_withglinfo_count.csv|while read LINE do if grep -q $LINE /tmp/UA_Automation/glhardlines; then echo "$LINE matching" array+=("$LINE") else echo "$LINE not_matching" fi done echo ${#array[@]}` – user3624000 Oct 30 '14 at 17:46
  • Any corrections you would like to suggest me? The statement `echo ${#array[@]}` gives 0 as output. – user3624000 Oct 30 '14 at 17:50
  • I can't read that easily, there's no formatting in comments. You should append it to the question if you're still having trouble. – Barmar Oct 31 '14 at 00:02
  • `+=` doesn't add an element to an array in `bash`, so I don't know what you expect `array+=("$LINE")` to do. You need to increment a variable `$i` and use `array[$i]=$LINE` – Barmar Oct 31 '14 at 00:04
  • Declaring array globally doesn't work? Statement `echo ${#array[@]}` gives me 0 as output. I was expecting output to be "2". Any ideas?? – user3624000 Nov 01 '14 at 11:46
  • Commands in a pipeline are run in a subshell, so any variable assignments they make are not visible after the `while` loop ends. – Barmar Nov 01 '14 at 16:05
  • See http://stackoverflow.com/questions/16854280/modifying-variable-inside-while-loop-is-not-remembered – Barmar Nov 01 '14 at 16:07