LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   Problem with reading multiple lines from multiple files (https://www.linuxquestions.org/questions/linux-newbie-8/problem-with-reading-multiple-lines-from-multiple-files-4175526172/)

VijayB 11-22-2014 04:47 AM

Problem with reading multiple lines from multiple files
 
Hi All,

I am having a problem while reading multiple lines from multiple files. I have two files in the directory sat test. File names are test1.txt & test2.txt. Contents of the files are:

test1.txt:

[~/test]$ cat test1.txt
XYZ uniqueID ABC
DEF Price 800

test2.txt:

[~/test]$ cat test2.txt
xyz uniqueID 900
def Price abc


i want my code to grep multiple particular word from these 2 or multiple files of same type & process accordingly. But the code seems to not be working for multiple lines.
Code is:

Code:

#!/bin/bash
#Reading Multiple files

for file in ~/test/*; do
    k=1
    while read -r line; do
        Site="$(echo $file | sed 's/.txt$//'| cut --d="/" -f 5)"
        echo -e "\e[1;33mFile Checked = $Site \
        \e[0m\n"
        uniq="$(grep uniqueID | cut -f3 -d" ")"
        echo -e "Uniq IDs are :\n$uniq\n"
        AUG="$(grep Price  | cut -f3 -d" ")"
        echo -e "AUG is :\n$AUG\n"
        ((k++))
    done < "$file"
done

Output that i am getting is :

[~]$ ./test21.sh
Site Checked = test1

Uniq Hw IDs are :


AUG is :


Site Checked = test2

Uniq Hw IDs are :


AUG is :



Can somebody please suggest how to move forward with this.

Thanks in Advance!

BR\\Vijay

smallpond 11-22-2014 06:33 AM

Your grep commands don't have any input. Get rid of the while loop an just pass $file to each grep.

grail 11-22-2014 08:31 AM

I am with the previous post. Your while loop is pointless as you do nothing with the read line anyway.


All times are GMT -5. The time now is 01:39 PM.