Check string end with curly braces


Login or Register to Reply

 
Thread Tools Search this Thread
# 8  
Old 3 Days Ago
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.

Cheers,
Don
# 9  
Old 3 Days Ago
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.
This User Gave Thanks to RudiC For This Post:
jim mcnamara (3 Days Ago)
# 10  
Old 2 Days Ago
Quote:
Originally Posted by RudiC
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.
# 11  
Old 2 Days Ago
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.
Code:
#!/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:
Code:
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> _

Login or Register to Reply

|
Thread Tools Search this Thread
Search this Thread:
Advanced Search

More UNIX and Linux Forum Topics You Might Find Helpful
When curly braces needed? andy391791 Shell Programming and Scripting 6 05-12-2016 12:49 PM
Curly braces in sed tostay2003 Shell Programming and Scripting 13 06-25-2015 09:12 AM
How to remove newline, tab, spaces in curly braces.. :( Pls Help? NY_777 Shell Programming and Scripting 6 10-01-2014 08:34 AM
** EMERGENCY ** Having trouble with curly braces.. :( Pls Help NY_777 Shell Programming and Scripting 1 10-01-2014 03:34 AM
How do I pull the value between curly braces? Scottie1954 UNIX for Dummies Questions & Answers 7 07-08-2013 06:34 AM
Search a string in a text file and add another string at the end of line suryanarayana Shell Programming and Scripting 5 05-27-2013 07:21 AM
Remove lines between the start string and end string including start and end string Python Dabheeruz Shell Programming and Scripting 1 02-15-2013 11:40 AM
tar --exclude with curly braces majest Shell Programming and Scripting 12 08-09-2012 12:06 PM
sed in windows does not parse curly braces edgarvm Shell Programming and Scripting 1 11-11-2011 10:39 AM
Need to check date at the end venkatesht UNIX for Dummies Questions & Answers 1 05-17-2011 12:00 AM
find -regex not recognizing curly braces stevensw Shell Programming and Scripting 4 05-16-2011 12:15 AM
Curly braces assigned to variables njks68 Shell Programming and Scripting 1 06-11-2010 06:42 AM
Appending string, variable to file at the start and string at end Vaddadi Shell Programming and Scripting 4 03-24-2010 01:31 PM
Use of curly braces with variables abhinavsinha Shell Programming and Scripting 2 03-05-2008 06:57 AM
grep to find content in between curly braces, "{" and "}," keshav_rk Shell Programming and Scripting 4 08-09-2007 10:14 PM