Bash script reg-exp , replace , open and write


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Bash script reg-exp , replace , open and write
# 8  
Old 06-22-2012
thank you for your help , just I want to say I am not blindly running your code I created a backup before doing anything Smilie
# 9  
Old 06-22-2012
But my code expects .pm files, not .m files! You can't run it on .m files and expect it to work. It can't edit 'in place' -- very few noninteractive things besides 'ed' do -- and making something which did would be far more difficult.
# 10  
Old 06-25-2012
Yes agree, but like you I have a lot of file *.m I don't want to rename to *.pm all files if, I did back for all project I would like simply to run it in *.m files .
what I should change to do this .?
# 11  
Old 06-25-2012
All right, this should generate .pm files from .m files instead of vice versa:

Code:
$ cat ocstr2.awk

# If the code contains @"LBL_....", replace it with C_LBL_... and
# remember the labels for later.
match($0, /@"LBL_[0-9]*\"/) {
        S=$0; P=""

        do        # Loop until there's nothing left in the right half
        {
                A[L=substr(S, RSTART+2, RLENGTH-3)]++; # Remember label
                P=P substr(S, 1, RSTART-1) "C_" L;
                S=substr(S, RSTART+RLENGTH);
        }
        while(match(S, /@"LBL_[0-9]*\"/)); # Keep hunting for more labels

        $0=P S; # Update the line to be printed
}

# If we are given file.pm, print into file.m
{       O=FILENAME;
        sub(/[.]m$/, ".pm", O);
        print > O;      }

END {
        for(L in A) printf("NSString *const C_%s = @\"%s\";\n", L, L);
}

This User Gave Thanks to Corona688 For This Post:
# 12  
Old 07-25-2012
too many open files

HI I am trying to run the following script for 317 files (*.m) and I get this error :
awk: xfile.m makes too many open files
what's the solution pls ?

Code:
$ cat ocstr2.awk

# If the code contains @"LBL_....", replace it with C_LBL_... and
# remember the labels for later.
match($0, /@"LBL_[0-9]*\"/) {
        S=$0; P=""

        do        # Loop until there's nothing left in the right half
        {
                A[L=substr(S, RSTART+2, RLENGTH-3)]++; # Remember label
                P=P substr(S, 1, RSTART-1) "C_" L;
                S=substr(S, RSTART+RLENGTH);
        }
        while(match(S, /@"LBL_[0-9]*\"/)); # Keep hunting for more labels

        $0=P S; # Update the line to be printed
}

# If we are given file.pm, print into file.m
{       O=FILENAME;
        sub(/[.]m$/, ".pm", O);
        print > O;      }

END {
        for(L in A) printf("NSString *const C_%s = @\"%s\";\n", L, L);
}

Script Author Corona688
# 13  
Old 07-25-2012
It would have helped to know some basics, as my crystal ball of precognition is in repair right now: OS, version, user credentials, ....

You might want to have a look at ulimit with the user under which you want to run the script - maybe the account isn't allowed to open so many files. You will have to correct this with the root user.

Could you please explain what the script does/should do? Maybe it is possible to straighten the algorithm a bit.

I hope this helps.

bakunin
# 14  
Old 07-25-2012
the script create file list and search into all files *.m in specific directory for a reg-exp
Code:
Code:
@"LBL_[[0-9]]+"

exp: @"LBL_75847" , and write those matchs to constant.h if there are not written (no duplication into the file constants) like this

Code:
Code:
NSString *const C_LBL_[[0-9]] = thematch; // exp NSString *const C_LBL_78784 = @"LBL_78784";

Code:
Code:
NSString *const C_LBL_[[0-9]] = thematch; // exp NSString *const C_LBL_78787 = @"LBL_78787";

moreover this script should also replace all matched
Code:
Code:
@"LBL_[[0-9]]+"

into *.m files with the constant var which is
Code:
Code:
 C_LBL_[[0-9]]+

REF

Thanks in advance

---------- Post updated at 08:00 PM ---------- Previous update was at 07:55 PM ----------

if I run ulimit command I got
Code:
$ ulimit -Hn
unlimited
$ ulimit -Sn
256


Last edited by zaxxon; 07-25-2012 at 03:06 PM.. Reason: 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

How to use regular exp to replace the character in the file?

Hi Unix Gurus, yesterday I asked a question and got answer, it works fine. I have one more thing need to help in the code awk '{print substr($0,1,3)"xxx"substr($0,7)}' file If I have 50 charactor's need to be replaced, is there any easy way to use reg exp or I have to input 50 XXXXx......... (12 Replies)
Discussion started by: ken6503
12 Replies

2. Shell Programming and Scripting

Bash script - coloring reg. expressions in text

Hi all, is there anyone good at bash who will help me? I need to use syntax ${string/pattern/replacement} The problematic part where I am stuck is: #!bin/bash text="A cat is on a mat." exp="cat" newexp="SOMECODEcatSOMECODE" newtext=${${text}/${exp}/${newexp}} == > ERROR "wrong... (4 Replies)
Discussion started by: JohnnyM77
4 Replies

3. Emergency UNIX and Linux Support

Need Help On REG EXP in unix

Respected All, I have a very big xml in that i want to search only below 3 lines. <target name ="UpgradePrimaryBox" depends ="configureBox1"> <echo> Finished Upgrading Primary Box </echo> </target> grep -i "<target.*UpgradePrimaryBox" this gives me the first line. then i need to match... (7 Replies)
Discussion started by: ameyrk
7 Replies

4. Shell Programming and Scripting

perl reg-exp

$var1="LEN"; $VAR2="CODLENTT"; now, var2 contains var1(LEN).How do i check this in perl.... whether one string is a part of another..? if (<logic>) { my operation; } what'd be the logic.. (4 Replies)
Discussion started by: vijay_0209
4 Replies

5. Shell Programming and Scripting

Perl reg exp

Hi, I am using the following piece of code for extracting some data from in between some tags ... $text =~ /<TAG1>(.*)<\/TAG1>.*<TAG2>(.*)<\/TAG2>.*<TAG3>(.*)<\/TAG4>.*<TAG5>(.*)<\/TAG5>/; $tag1=$1; print "\n$tag1"; But I am getting an error like Use of uninitialized value in... (1 Reply)
Discussion started by: King Nothing
1 Replies

6. UNIX for Dummies Questions & Answers

extract a part of a path like (input: /etc/exp/home/bin ====> output: exp)

Hi, I need to make some extraction . with the following input to get the right output. input: /etc/exp/home/bin ====> output: exp and input: aex1234 ===> output: ex Thanks for your help, (4 Replies)
Discussion started by: yeclota
4 Replies

7. Shell Programming and Scripting

reg exp for sed

$ cat file.txt asd <AA>dev <LL>def <RR>sha This works for me: $ sed -r 's/^ .*<LL>def/\t<LL>my/' file.txt asd <AA>dev <LL>my <RR>sha But, this does not work for me: $ sed -r 's/^\s+<LL>def/\t<LL>my/' file.txt asd ... (1 Reply)
Discussion started by: demoprog
1 Replies

8. Shell Programming and Scripting

reg exp question

Hi, Should be a difference between ']]*' and ']+' ? I use them in bash with sed and grep. Thanks (1 Reply)
Discussion started by: ynir
1 Replies

9. Shell Programming and Scripting

usage...sed/awk/reg-exp ..in shell scripting

in shell scripting there is extensive usage of i> regular expression ii>sed iii>awk can anyone tell me the suitable contexts ...i mean which one is suitable for what kind of operation. like the reg-exp and sed seems to be doing the same job..i.e pattern matching (1 Reply)
Discussion started by: mobydick
1 Replies

10. Shell Programming and Scripting

Using reg. exp. variable in AWK??

Any idea please: How to pass a reg. exp. variable to awk call in a shell??? Thank u #!/bin/sh reg_exp=name awk '/reg_exp/{ print; }' $1 (5 Replies)
Discussion started by: andy2000
5 Replies
Login or Register to Ask a Question