Check string end with curly braces


 
Thread Tools Search this Thread
Top Forums UNIX for Beginners Questions & Answers Check string end with curly braces
# 8  
Old 03-16-2019
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 03-16-2019
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:
# 10  
Old 03-16-2019
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 03-16-2019
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 Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

When curly braces needed?

Hello, i was trying to find get a command to list duplicated files so i tried ls dir1 dir2 | awk '{x++}' and it didnt work. After a bit of searching online i found that it works without the curly braces ls dir1 dir2 | awk 'x++' I thought the curly braces were needed in awk so... (6 Replies)
Discussion started by: andy391791
6 Replies

2. Shell Programming and Scripting

Curly braces in sed

Hi, I have below command in one of the script. Can you please let me know what does the curly braces do over here \{1,\}. The remaining part of the code atleast I am able to understand. sed -n 's/.*\-\()\{1,\}\)\-.*/\1/p' (13 Replies)
Discussion started by: tostay2003
13 Replies

3. Shell Programming and Scripting

How to remove newline, tab, spaces in curly braces.. :( Pls Help?

Hi Everyone, in the below "xyz (Exception e)" part... after the curly braces, there is a new line and immediately few tabs are present before closing curly brace. xyz (Exception e) { } note: there can be one or more newlines between the curly braces. My desired output should be ... (6 Replies)
Discussion started by: NY_777
6 Replies

4. Shell Programming and Scripting

** EMERGENCY ** Having trouble with curly braces.. :( Pls Help

Hi Everyone, in the below "xyz (Exception e)" part... after the curly braces, there is a new line and immediately few tabs are present before closing curly brace. xyz (Exception e) { } note: there can be one or... (1 Reply)
Discussion started by: NY_777
1 Replies

5. UNIX for Dummies Questions & Answers

How do I pull the value between curly braces?

Hi everyone, I've got a file that looks like this: uid{508}pid{22224}pname{/PPROGRAM/pprgramx -profile:LIVE -serv:as ... I want to pull the value of pid between the curly braces, or 22224 in this example. pid is always the second pair of curly braces, but the length of the number is... (7 Replies)
Discussion started by: Scottie1954
7 Replies

6. Shell Programming and Scripting

tar --exclude with curly braces

I'm having trouble understanding the exclude option in tar. From some web sites, it seems one is able to exclude several strings by enclosing them in curly brackets. However it seems to be "random" what gets excluded when using the curlies. I've been using the exclude-from=myfile option in a... (12 Replies)
Discussion started by: majest
12 Replies

7. Shell Programming and Scripting

sed in windows does not parse curly braces

Hi everyone: I'm stuck at this point, could you guys please give me some hints about what I am doing wrong in the following script, I'm using sed for windows: sed ^"$ {^ a^ STRINGTABLE DISCARDABLE^ BEGIN^ #define CLIENT_MODULE, "%CLIENT_MODULE%"^ #define CLIENT_ID, "%CLIENT_ID%"^... (1 Reply)
Discussion started by: edgarvm
1 Replies

8. Shell Programming and Scripting

find -regex not recognizing curly braces

Must be a bug or something. Whether I escape them or not, it will not work. No matter what I set the minimum and maximum to nothing gets caught. For instance: find / -regex "/.{0, 50}.*" -maxdepth 1 or find / -regex "/.\{0, 50\}.*" -maxdepth 1 should pretty much catch everything residing within... (4 Replies)
Discussion started by: stevensw
4 Replies

9. Shell Programming and Scripting

Curly braces assigned to variables

Hi, Im pretty new to Unix. I came across a script which was using PLSQL inside a script and there was an unusual thing mentioned. there was a variable assigned as P_CUR=${1} and one more as V_TAGFILE="$1" Couldnt find the difference. Also the variables were used in PLSQL... (1 Reply)
Discussion started by: njks68
1 Replies

10. Shell Programming and Scripting

Use of curly braces with variables

Hi, I am new to shell scripting.I have worked somewhat with Perl though. I am not able to find what the second line does and how does it do. <code> FP_RUNNING=`service filepool status` FP_RUNNING=${FP_RUNNING%% *} <\code> After the first line,the variable FP_RUNNING stores '1 FilePool... (2 Replies)
Discussion started by: abhinavsinha
2 Replies
Login or Register to Ask a Question