0

I'm sorry for my poor English, first. I want to read a file (tel.txt) that contains many tel numbers (a number per line) and use that line to grep command to search about the specific number in the source file (another file)!

I wrote this code :

dir="/home/mujan/Desktop/data/ADSL_CDR_Text_Parts_A"
file="$dir/tel.txt"
datafile="$dir/ADSL_CDR_Like_Tct4_From_960501_to_97501_Part0.txt"

while IFS= read -r line
do
    current="$line"
    echo `grep -F $current "$datafile" >> output.txt`
done < $file

the tel file sample :

44001547
44001478
55421487

but that code returns nothing! when I declare 'current' variable with literals it works correctly! what happened?!

4
  • cat file.txt | grep <number> Commented Feb 13, 2019 at 13:01
  • Make sure you quote "$current" Commented Feb 13, 2019 at 13:11
  • It is also quite possible that your input file contains DOS carriage returns. If so, this is a duplicate of stackoverflow.com/questions/39527571/… Commented Feb 13, 2019 at 13:13
  • Paste into shellcheck.net for line by line advice. Then use tripleee's solution. All you need is grep (and maybe love. ;) Commented Feb 13, 2019 at 14:19

3 Answers 3

6

Your grep command is redirected to write its output to a file, so you don't see it on the terminal.

Anyway, you should probably be using the much simpler and faster

grep -Ff "$file" "$datafile"

Add | tee -a output.txt if you want to save the output to a file and see it at the same time.

echo `command` is a buggy and inefficient way to write command. (echo "`command`" would merely be inefficient.) There is no reason to capture standard output into a string just so that you can echo that string to standard output.

Sign up to request clarification or add additional context in comments.

2 Comments

Might also want to use grep's -x option (if it's available) to make these "full-line" expressions.
@ghoti Good addition, though it's not clear from the question whether the OP simply forgot to mention this, or if they need something else.
0

Why don't you search for the line var directly? I've done some tests, this script works on my linux (CentOS 7.x) with bash shell:

#!/bin/bash
file="/home/mujan/Desktop/data/ADSL_CDR_Text_Parts_A/tel.txt"

while IFS= read -r line
do
    echo `grep "$line" /home/mujan/Desktop/data/ADSL_CDR_Text_Parts_A/ADSL_CDR_Like_Tct4_From_960501_to_97501_Part0.tx >> output.txt`
done < $file

Give it a try... It shows nothing on the screen since you're redirecting the output to the file output.txt so the matching results are saved there.

2 Comments

This preserves many of the antipatterns and inefficiencies in the original code. The useless use of echo is particularly sad.
I know, thank you for pointing it out. I was trying to keep the script in the initial form... But I'll agree, your approach is better
0

You should use file descriptors when reading with while loop.instead use for loop to avoid false re-directions

dir="/home/mujan/Desktop/data/ADSL_CDR_Text_Parts_A"
file="$dir/tel.txt"
datafile="$dir/ADSL_CDR_Like_Tct4_From_960501_to_97501_Part0.txt"

for line in `cat $file`
do
    current="$line"
    echo `grep -F $current "$datafile" >> output.txt`
done

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.