Help me out - I don't see a difference between the two scripts, nor in their output - it's identical. So - please show the entire picture. Any output redirection upfront?
My guess would be that the file has DOS style line terminators and there is an invisible <carriage-return> character following the <open-brace> character before the <newline> character that is keeping the line read from the file from matching.
Please show us the output from the command I asked you to run in post #3 in this thread. Without the information that output will provide, we can't explain what is causing the problem you are describing.
Sorry for my inconvenience post that make you not understanding. I will be careful to read the guideline before post a good quality question in the next posts.
However, I solved it.
The problem is CF LF on each line on notepad++. That is the reason why I cannot find line at end with {
Thank you Don Cragun and RudiC to moderate and reply me.
Hi cmdcmd,
We are glad that you have solved your problem.
Please tell us how you solved your problem so that other people reading your thread can learn from your experience.
Note also that if you show us how you solved your problem, tell us what operating system you're using, and tell us what shell you're using; someone might be able to offer another solution that would work even better in your environment.
I'm a bit stumped by your "solution". In your original, unedited first post you had two (identical at the core) scripts working on the same input file, of which you claimed one showed satisfactory results, the other showed no output. That behaviour would not be explained by a non-*nix input text file.
Hi RudiC,
I believe that with bash, the [[ string == pattern ]] comparison uses pathname pattern matching (rather than BRE or ERE matching), so having a trailing <CR> on the ends of the names stored in the file kept a pattern of *"{" that was looking for the last character to be a { didn't match when the last character was a <CR>. It would match when he set a variable to the correct value without the trailing <CR>.
There are lots of ways this issue could be fixed. I'm still hoping that cmdcmd will tell us what method he selected when he solved this problem.
This doesn't care whether there is a <CR>, 0x0D, or not and puts it back if need be but removing them is easy.
Obviously performance depends on the size of the file and this does NOT allow for tabs and spaces at the start of each line containing a "{".
OSX 10.14.3, default bash terminal.
#!/bin/bash
printf "abc def{\x0D\x0Acdefg\x0D\x0Athis is a string\x0D\x0Afghh_hjhad_text{\x09text after brace with tab\x0D\x0A\x0D\x0A{\x0D\x0A{ text at end only, (with spaces)!\x0D\x0Aabcd_123\x0D\x0Aend of file without brace\x0D\x0A" > /tmp/text
hexdump -C /tmp/text
echo "cat /tmp/text:"
echo ""
cat /tmp/text
echo "TEST FILE CREATION END, FILTER START!"
echo ""
# Working part...
while read -r line
do
for (( n=0; n<=${#line}; n++ ))
do
if [ "${line:$n:1}" = "{" ]
then
echo "$line"
fi
done
done < /tmp/text > /tmp/lines
# End of working part!
echo "FILTER DONE!"
echo ""
hexdump -C /tmp/lines
echo ""
cat /tmp/lines
Resluts:
Last login: Sat Mar 16 23:21:49 on ttys000
AMIGA:amiga~> cd Desktop/Code/Shell
AMIGA:amiga~/Desktop/Code/Shell> ./left_brace.sh
00000000 61 62 63 20 64 65 66 7b 0d 0a 63 64 65 66 67 0d |abc def{..cdefg.|
00000010 0a 74 68 69 73 20 69 73 20 61 20 73 74 72 69 6e |.this is a strin|
00000020 67 0d 0a 66 67 68 68 5f 68 6a 68 61 64 5f 74 65 |g..fghh_hjhad_te|
00000030 78 74 7b 09 74 65 78 74 20 61 66 74 65 72 20 62 |xt{.text after b|
00000040 72 61 63 65 20 77 69 74 68 20 74 61 62 0d 0a 0d |race with tab...|
00000050 0a 7b 0d 0a 7b 20 74 65 78 74 20 20 61 74 20 20 |.{..{ text at |
00000060 20 20 65 6e 64 20 20 20 20 20 6f 6e 6c 79 2c 20 | end only, |
00000070 28 77 69 74 68 20 73 70 61 63 65 73 29 21 0d 0a |(with spaces)!..|
00000080 61 62 63 64 5f 31 32 33 0d 0a 65 6e 64 20 6f 66 |abcd_123..end of|
00000090 20 66 69 6c 65 20 77 69 74 68 6f 75 74 20 62 72 | file without br|
000000a0 61 63 65 0d 0a |ace..|
000000a5
cat /tmp/text:
abc def{
cdefg
this is a string
fghh_hjhad_text{ text after brace with tab
{
{ text at end only, (with spaces)!
abcd_123
end of file without brace
TEST FILE CREATION END, FILTER START!
FILTER DONE!
00000000 61 62 63 20 64 65 66 7b 0d 0a 66 67 68 68 5f 68 |abc def{..fghh_h|
00000010 6a 68 61 64 5f 74 65 78 74 7b 09 74 65 78 74 20 |jhad_text{.text |
00000020 61 66 74 65 72 20 62 72 61 63 65 20 77 69 74 68 |after brace with|
00000030 20 74 61 62 0d 0a 7b 0d 0a 7b 20 74 65 78 74 20 | tab..{..{ text |
00000040 20 61 74 20 20 20 20 65 6e 64 20 20 20 20 20 6f | at end o|
00000050 6e 6c 79 2c 20 28 77 69 74 68 20 73 70 61 63 65 |nly, (with space|
00000060 73 29 21 0d 0a |s)!..|
00000065
abc def{
fghh_hjhad_text{ text after brace with tab
{
{ text at end only, (with spaces)!
AMIGA:amiga~/Desktop/Code/Shell> _