LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (http://www.linuxquestions.org/questions/programming-9/)
-   -   Shell script: Find "\n\t..." to replace a string in a file (http://www.linuxquestions.org/questions/programming-9/shell-script-find-%5Cn%5Ct-to-replace-a-string-in-a-file-551757/)

michael24h7d 05-06-2007 03:29 PM

Shell script: Find "\n\t..." to replace a string in a file
 
Can someone give me a simple bash script to find a string which includes some special characters eg "\n\t...hello world\r" in a file, and replace it with a different string?

Thank in advance.

jaykup 05-06-2007 05:30 PM

Crude, but may work.
Code:

echo -e "\n\tHello World\r" | tr -s "[\r\n\t]Hello World" "j"
This will replace the new line, carriage return, tab and string "Hello World" with "j"

You can replace echo with cat file and pipe that into tr

michael24h7d 05-06-2007 08:01 PM

thanks jaykup. I will try it soon. Thanks

cfaj 05-06-2007 09:19 PM

Quote:

Originally Posted by jaykup
Crude, but may work.
Code:

echo -e "\n\tHello World\r" | tr -s "[\r\n\t]Hello World" "j"
This will replace the new line, carriage return, tab and string "Hello World" with "j"


That's not what that command does; it replaces all consecutive occurrences of any character in the first string with a single 'j'. tr doesn't know anything about strings, only characters.

To get a clearer idea of what it does, try these commands:
Code:

printf "\n\tHello World\r" | tr "[\r\n\t]Hello World" "j"
echo
printf "\n\tHello XX World\r" | tr "[\r\n\t]Hello World" "j"
echo
printf "\n\tHello XX World\r" | tr -s "[\r\n\t]Hello World" "j"
echo


michael24h7d 05-07-2007 03:26 AM

Dear guys, I've just to tried your solution but i guessed it didnt match my need. Let me explain my problem clearly
I have a source file with a total of 15 lines(line 4,5,6,11,12,13 are empty lines)as below:
Source file
Code:

1: HHR _PCBCheck, ,
2: COMMENT Name ....... : PCBCheck
3: COMMENT Version . .. : V468A
4:
5:
6:
7: -- Used for initialize()
8:
9: COMMENT                      This variable shall be used to access Class level functions and method.
10: -------------------------------------------------------------------------------
11:       
12:
13:       
14:        ELEMENT
15: END FILE

I am looking to find a shell script to convert the source file to a destination file such as:
Destination file
Code:

1: HHR _PCBCheckImpl, ,
2: COMMENT Name ....... : PCBCheck
3: COMMENT Version . .. : V468A
4:
5: CPara              *ROL_CParaImpl;
6:
7: -- Used for initialize()
8:
9: COMMENT                      This variable shall be used to access Class level functions and method.
10: -------------------------------------------------------------------------------
11: COMMENT [REG-AS54] :
12: COMMENT                      When a class instance inherits a specific interface, it shall implement a method named "get<Name>Instance".
13: COMMENT                      This method shall return a pointer to the <InterfaceName> instance
14:        ELEMENT
15: END FILE

The destination file should have a same line number with source file(15 lines).
I also define a condition file to easy for finding and replacement.
Condition file:
Code:

[FIND 1]
HHR _PCBCheck, ,
[REPLACE 1]
HHR _PCBCheckImpl, ,
[FIND 2]

 
 
-- Used for initialize()
[REPLACE 2]
 
CPara              *ROL_CParaImpl;
 
-- Used for initialize()
[FIND 3]
       
 
       
        ELEMENT
[REPLACE 3]
COMMENT [REG-AS54] :
COMMENT                      When a class instance inherits a specific interface, it shall implement a method named "get<Name>Instance".
COMMENT                      This method shall return a pointer to the <InterfaceName> instance
        ELEMENT

I suppose a shell script will do as below flow chart:
step0: i=1
step1: read block text A (including empty line) from section [FIND i] in Condition file
step2: find A from Souce file
step3: if found, replace the text A with text in section [REPALCE i] (including empty line) in Condition file then output to Destination file
step4: i=i+1
step4: back step1 while i<=n

I am really want a shell script (bash or sh shell) like that and hope you take your valuable time to help me.

Thanks and thanks again.

bigearsbilly 05-08-2007 06:50 AM

are the line numbers in the code?

michael24h7d 05-08-2007 12:57 PM

Quote:

Originally Posted by bigearsbilly
are the line numbers in the code?

No, Sir. The line numbers are not in the code

bigearsbilly 05-09-2007 03:46 AM

i think you need to properly state the problem not the solution.
anyway, as I ain't busy I did some perl for you...

Code:

#! /usr/local/bin/perl -wn


# the first substitute
s/HHR _PCBCheck/HHR _PCBCheckImpl/;

# insert at line 5  ('$.' is the line number)
s/.*/CPara  *ROL_CParaImpl/ if ($. == 5);

if (11..14) {
    print <DATA>;
    next; # skip the default print
}

print; # default print

# put a template into DATA
__DATA__
COMMENT [REG-AS54] :
COMMENT                      When a class instance inherits a specific interface, it shall
implement a method named "get<Name>Instance".
COMMENT                      This method shall return a pointer to the <InterfaceName> inst
ance
ELEMENT


michael24h7d 05-11-2007 04:07 AM

Thank you so much Bigearsbilly. Finally, I solved my problem.


All times are GMT -5. The time now is 06:33 PM.