LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 05-07-2021, 12:43 AM   #1
parveen.gupta.23091987
LQ Newbie
 
Registered: May 2021
Posts: 8

Rep: Reputation: Disabled
Question Reading from EndPointsPriority.txt and finding that file name in other Directory


EndPointsPriority.txt contains>>
0001105
0005074
0079557


Reading from EndPointsPriority.txt and finding that file name in other Directory


This is working >>>>>>>>>>>>

echo "Start"
ePFileName='../resources/EndPointsPriority.txt'
counteP=1
while IFS= read -r eP; do
# reading each line
echo "Line No. $counteP : $eP"
#issue on below line
for file in $(find /apps_01/CBK/T5G2Batch/dataTqr/ -iname "*0001105*" 2>/dev/null)
do
# if file does not exist, don't try to send it
[ -f "$file" ]||continue
echo "File found $file"
done
counteP=$( expr $counteP + 1 )
echo "new count $counteP"
done < $ePFileName
echo "End"

This is not working >>>>>>>>>>>>

echo "Start"
ePFileName='../resources/EndPointsPriority.txt'
counteP=1
while IFS= read -r eP; do
# reading each line
echo "Line No. $counteP : $eP"
#issue on below line
for file in $(find /apps_01/CBK/T5G2Batch/dataTqr/ -iname "*$eP*" 2>/dev/null)
do
# if file does not exist, don't try to send it
[ -f "$file" ]||continue
echo "File found $file"
done
counteP=$( expr $counteP + 1 )
echo "new count $counteP"
done < $ePFileName
echo "End"
 
Old 05-07-2021, 02:16 AM   #2
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 22,002

Rep: Reputation: 7338Reputation: 7338Reputation: 7338Reputation: 7338Reputation: 7338Reputation: 7338Reputation: 7338Reputation: 7338Reputation: 7338Reputation: 7338Reputation: 7338
is there any question here? Do you need any support?
please use code tags to post your scripts.
also please use shellcheck to analyze your script.
 
Old 05-07-2021, 02:51 AM   #3
shruggy
Senior Member
 
Registered: Mar 2020
Posts: 3,677

Rep: Reputation: Disabled
As far as I can see the only difference between "working" and "not working" is
Code:
for file  in $(find /apps_01/CBK/T5G2Batch/dataTqr/ -iname "*0001105*"  2>/dev/null)
---
for file  in $(find /apps_01/CBK/T5G2Batch/dataTqr/ -iname "*$eP*"  2>/dev/null)
Try enclosing the variable in curly braces: ${eP}.
 
Old 05-07-2021, 04:14 AM   #4
parveen.gupta.23091987
LQ Newbie
 
Registered: May 2021
Posts: 8

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by shruggy View Post
As far as I can see the only difference between "working" and "not working" is
Code:
for file  in $(find /apps_01/CBK/T5G2Batch/dataTqr/ -iname "*0001105*"  2>/dev/null)
---
for file  in $(find /apps_01/CBK/T5G2Batch/dataTqr/ -iname "*$eP*"  2>/dev/null)
Try enclosing the variable in curly braces: ${eP}.

Tried but not working : for file in $(find /apps_01/CBK/T5G2Batch/dataTqr/ -iname "*${eP}*" 2>/dev/null)[/code]
 
Old 05-07-2021, 04:27 AM   #5
shruggy
Senior Member
 
Registered: Mar 2020
Posts: 3,677

Rep: Reputation: Disabled
Ensure the variable $eP contains the correct value at that point in script execution. I see you echoing it just before. Does it print what you expect?

You may also temporarily remove the 2>/dev/null redirection in the find command to see if there are any error messages.
 
Old 05-07-2021, 04:33 AM   #6
parveen.gupta.23091987
LQ Newbie
 
Registered: May 2021
Posts: 8

Original Poster
Rep: Reputation: Disabled
try with new code as per your link but still not working


#!/bin/bash
echo "Start"
ePFileName='../resources/EndPointsPriority.txt'
counteP=1
while IFS= read -r eP; do
# reading each line
echo "Line No. $counteP : $eP"
#issue on below line
while IFS= read -r -d '' file
do
# if file does not exist, don't try to send it
[ -f "$file" ]||continue
echo "File found $file"
done < <(find /apps_01/CBK/T5G2Batch/dataTqr/ -iname "*${eP}*" 2>/dev/null)
counteP=$( $counteP + 1 )
echo "new count $counteP"
done < $ePFileName
echo "End"
 
Old 05-07-2021, 04:40 AM   #7
parveen.gupta.23091987
LQ Newbie
 
Registered: May 2021
Posts: 8

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by shruggy View Post
Ensure the variable $eP contains the correct value at that point in script execution. I see you echoing it just before. Does it print what you expect?

You may also temporarily remove the 2>/dev/null redirection in the find command to see if there are any error messages.


>>>>>>

echo "Start"
ePFileName='../resources/EndPointsPriority.txt'
counteP=1
while IFS= read -r eP; do
# reading each line
echo "Line No. $counteP : ${eP}"
#issue on below line
for file in $(find /apps_01/CBK/T5G2Batch/dataTqr/ -iname "*${eP}*")
do
# if file does not exist, don't try to send it
[ -f "$file" ]||continue
echo "File found $file"
done
counteP=$( expr $counteP + 1 )
echo "new count $counteP"
done < $ePFileName
echo "End"

>>>Output
Start
Line No. 1 : 0001105
new count 2
Line No. 2 : 0005074
new count 3
Line No. 3 : 0079557
new count 4
End


One more thing i observer



echo "Start"
ePFileName='../resources/EndPointsPriority.txt'
counteP=1
while IFS= read -r eP; do
# reading each line
echo "Line No. $counteP : ${eP} test"
#issue on below line
for file in $(find /apps_01/CBK/T5G2Batch/dataTqr/ -iname "*${eP}*")
do
# if file does not exist, don't try to send it
[ -f "$file" ]||continue
echo "File found $file"
done
counteP=$( expr $counteP + 1 )
echo "new count $counteP"
done < $ePFileName
echo "End"
output changes to ( this is not correct)

Start
testNo. 1 : 0001105
new count 2
testNo. 2 : 0005074
new count 3
testNo. 3 : 0079557
new count 4
End
 
Old 05-07-2021, 04:45 AM   #8
shruggy
Senior Member
 
Registered: Mar 2020
Posts: 3,677

Rep: Reputation: Disabled
Quote:
Originally Posted by parveen.gupta.23091987 View Post
as per your link
What link?


When echoing the variable enclose it into brackets like this
Code:
echo "Line No. $counteP : [$eP]"
This way you'll catch any stray whitespace characters.


Also, remove IFS= before read:
Code:
while read -r eP; do
This way any whitespace characters will be trimmed from the beginning and end of the input line.

Perchance, can the input file be encoded with DOS newlines (CRLF)?
Code:
cat -A EndPointsPriority.txt

Last edited by shruggy; 05-07-2021 at 04:50 AM.
 
Old 05-07-2021, 05:00 AM   #9
parveen.gupta.23091987
LQ Newbie
 
Registered: May 2021
Posts: 8

Original Poster
Rep: Reputation: Disabled
Fixed thank you so much

I created EndPointsPriority.txt using touch in linux then retyped all the values

not sure what was the issue ?? any thing to handle in code ?


previously I created EndPointsPriority.txt in windows and copied it to linux

Last edited by parveen.gupta.23091987; 05-07-2021 at 05:02 AM.
 
Old 05-07-2021, 05:07 AM   #10
shruggy
Senior Member
 
Registered: Mar 2020
Posts: 3,677

Rep: Reputation: Disabled
Quote:
Originally Posted by parveen.gupta.23091987 View Post
previously I created EndPointsPriority.txt in windows and copied it to linux
^This. Windows and Linux have different newline conventions.

Quote:
Originally Posted by parveen.gupta.23091987 View Post
any thing to handle in code?
In your code, you could handle it like this:
Code:
while IFS=$IFS$'\r' read -r eP; do

Last edited by shruggy; 05-07-2021 at 05:10 AM.
 
Old 05-07-2021, 05:17 AM   #11
parveen.gupta.23091987
LQ Newbie
 
Registered: May 2021
Posts: 8

Original Poster
Rep: Reputation: Disabled
thank you so much !!!

Really learning something new

I'm a java sr Dev working in Shell for first time.

Thank you again it worked.
 
Old 05-07-2021, 05:26 AM   #12
parveen.gupta.23091987
LQ Newbie
 
Registered: May 2021
Posts: 8

Original Poster
Rep: Reputation: Disabled
Thank you Shruggy
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] read txt file into an array and make a second txt file zimbot Linux - General 12 09-05-2015 01:39 PM
cut first 10 lines of file master.txt and paste in ab1.txt and so on yogeshkumkar Programming 4 08-31-2011 07:23 AM
Copy the contents of a txt file to other txt files (with similar names) by cp command Aquarius_Girl Linux - Newbie 7 07-03-2010 12:54 AM
cat onelinefile.txt >> newfile.txt; cat twofile.txt >> newfile.txt keep newline? tmcguinness Programming 4 02-12-2009 06:38 AM
How can read from file.txt C++ where can save this file(file.txt) to start reading sam_22 Programming 1 01-11-2007 05:11 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 08:10 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration