How to get a value from a file and serach that value filename in a dir?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting How to get a value from a file and serach that value filename in a dir?
# 1  
Old 06-06-2017
How to get a value from a file and serach that value filename in a dir?

buddies,

my requirement would be as follows,

I have a file called test.txt and content of it would be

HTML Code:
yahoo
gmail
hotmail
and i want to search a file name called "yahoo.html" (first line of test.txt) and then "gmail.html" and then "hotmail.html" in a /home/test dir.

Any idea to achive this ?
# 2  
Old 06-06-2017
Hello natraj005,

Could you please try following and let me know if this helps you(I haven't tested though).
Code:
while read line
do
    find /home/test -type f -iname $line".html"
done < "test.txt"

Thanks,
R. Singh
# 3  
Old 06-06-2017
RavinderSingh13,

to be more specfic i have to capture each line from test.txt and assign it to a variable called MAIL and then i can assign that to another variable

like below

HTML Code:
PERSONAL_MAIL=/home/test/www.$MAIL.com
so PERSONAL_MAIL will have all three files names like below

HTML Code:
/home/test/www.yahoo.com
/home/test/www.gmail.com
/home/test/www.hotmail.com
and then i will serach a patern called "username" in all the three files, like below

HTML Code:
MAIL_INFO=$(grep "^username" $PERSONAL_MAIL
Hope am not confusing you Smilie
# 4  
Old 06-06-2017
Code:
dir=/home/test

while read MAIL
do
   PERSONAL_MAIL=$dir/www.$MAIL.com
   echo $PERSONAL_MAIL
done < test.txt

# 5  
Old 06-06-2017
Thanks rdrtx1 for your quick reply!

what you gave is checking only in the first file i.e) only checking in /home/test/www.yahoo.com and coming out, and not checking in the remaining two files i.e) /home/test/www.gmail.com and /home/test/www.hotmail.com.

So, it has to search pattern "username" in all the file one by one till the test.txt gets empty or reach the bottom line of it.
# 6  
Old 06-06-2017
Difficult to believe. When running rdrtx1's proposal, I get exactly the desired output. Does your input resemble the given sample data?
# 7  
Old 06-06-2017
Am trying below script and it is not working

Code:
dir=/home/test

while read MAIL
do
   PERSONAL_MAIL=$dir/www.$MAIL.com
   ACCOUNT=$(grep -c mail $PERSONAL_MAIL)
   if [ -z "$PERSONAL_MAIL" ]
       then
            if [ -z "$ACCOUNT" ]
               echo "$PERSONAL_MAIL is avaiable "
       else
               echo "$PERSONAL_MAIL not avaiable "
       fi
    fi
   
done < test.txt

above script is not searching for all the line in test.txt and content of the file is as like below

yahoo
gmail
hotmail

let me know if i still confusing you....


Moderator's Comments:
Mod Comment Please use CODE tags correctly as required by forum rules!

Last edited by RudiC; 06-07-2017 at 12:40 AM.. Reason: Changed PHP to CODE tags.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Print filename/dir name while executing aclput using find command

Running below command , but unable to print the filename , is there way to print filename/dirname using -print option find . -type f -exec aclput -i fileacl.template {} \; (5 Replies)
Discussion started by: lalitpct
5 Replies

2. Windows & DOS: Issues & Discussions

Check dir for overly path+filename and list in txt

Well since Windows always laments over some of my files having a too long "path+filename" and it gets in the way of copying complete directory structures I would love to have a DOS Script that helps me with finding those. I already tried DCSoft Long Filename Finder but that is neither DOS based... (3 Replies)
Discussion started by: pasc
3 Replies

3. Shell Programming and Scripting

serach diff filename in diff location using shell scripting

Hi, I am new to shell scripting. please help me to find out the solution. I need a script where we need to read the text file(consists of all file names) and get the file names one by one and append the date suffix for each file name as 'yyyymmdd' . Then search each file if exists... (1 Reply)
Discussion started by: Lucky123
1 Replies

4. UNIX for Dummies Questions & Answers

How to list all files in dir and sub-dir's recursively along with file size?

I am very new to unix as well as shell scripting. I have to write a script for the following requirement. In have to list all the files in directory and its sub directories along with file path and size of the file Please help me in this regard and many thanks in advance. (3 Replies)
Discussion started by: nmakkena
3 Replies

5. Shell Programming and Scripting

want to move files in a dir into different directories based on the filename

I want to move the files in a dir to different dirs based on their file names. Ex: i have 4 different files with name - CTS_NONE_10476031_MRL_PFT20081215a.txt CTS_NONE_10633009_MRL_PFT20091020a.txt CTS_NONE_10345673_MRL_PFT20081215a.txt CTS_NONE_10872456_MRL_PFT20091020a.txt and the 1st... (4 Replies)
Discussion started by: Sriranga
4 Replies

6. UNIX for Dummies Questions & Answers

want to move files in a dir into different directories based on the filename

I want to move the files in a dir to different dirs based on their file names. Ex: i have 4 different files with name - CTS_NONE_10476031_MRL_PFT20081215a.txt CTS_NONE_10633009_MRL_PFT20091020a.txt CTS_NONE_10345673_MRL_PFT20081215a.txt CTS_NONE_10872456_MRL_PFT20091020a.txt and the 1st... (2 Replies)
Discussion started by: Sriranga
2 Replies

7. UNIX for Dummies Questions & Answers

How to get dir name tacked onto filename with ls command

hello i'm trying to figure out how to tack the directory name onto the file name when i do the ls -ltrR command please? --------------------------------------------------------------------------------- I do the following command ls -ltrR > ls.out and get the following output:... (5 Replies)
Discussion started by: bobk544
5 Replies

8. Shell Programming and Scripting

serach file using awk

Hi, I was able to print lines when search for specific string using awk but i want to print when search for two different strings using awk instead of doing two times (Print lines only contain "Insert Records and Insert into" not between lines) Ex: awk '/Insert Records./' a4.log It... (3 Replies)
Discussion started by: mohan705
3 Replies

9. Shell Programming and Scripting

Print filename and resolution in wrkng dir

Greetings, I am trying to write script, (preferably in sh) that will use a proprietary program to print the resolution and name of files in the current working directory in a vertical format. I think this script will require 3 commands with variables and a pipe to the prop program, but am not... (6 Replies)
Discussion started by: rocinante
6 Replies

10. UNIX for Advanced & Expert Users

text serach in unknown file

Can any one tell me if there is an easy way to find a text in an unknown file name in a dierectory. I'm trying to search for the text "where is john going" from a file whose name I do not even know. There are about 1000 files in that directory and one of the files has this text in it. Can... (11 Replies)
Discussion started by: ted
11 Replies
Login or Register to Ask a Question