LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 05-06-2007, 02:29 PM   #1
michael24h7d
LQ Newbie
 
Registered: May 2007
Posts: 5

Rep: Reputation: 0
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.

Last edited by michael24h7d; 05-06-2007 at 02:31 PM.
 
Old 05-06-2007, 04:30 PM   #2
jaykup
Member
 
Registered: Jan 2006
Location: Mukwonago, WI
Distribution: Slackware 12
Posts: 77

Rep: Reputation: 16
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

Last edited by jaykup; 05-06-2007 at 04:32 PM.
 
Old 05-06-2007, 07:01 PM   #3
michael24h7d
LQ Newbie
 
Registered: May 2007
Posts: 5

Original Poster
Rep: Reputation: 0
thanks jaykup. I will try it soon. Thanks
 
Old 05-06-2007, 08:19 PM   #4
cfaj
Member
 
Registered: Dec 2003
Location: Toronto, Canada
Distribution: Mint, Mandriva
Posts: 221

Rep: Reputation: 31
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
 
Old 05-07-2007, 02:26 AM   #5
michael24h7d
LQ Newbie
 
Registered: May 2007
Posts: 5

Original Poster
Rep: Reputation: 0
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.
 
Old 05-08-2007, 05:50 AM   #6
bigearsbilly
Senior Member
 
Registered: Mar 2004
Location: england
Distribution: Mint, Armbian, NetBSD, Puppy, Raspbian
Posts: 3,515

Rep: Reputation: 239Reputation: 239Reputation: 239
are the line numbers in the code?
 
Old 05-08-2007, 11:57 AM   #7
michael24h7d
LQ Newbie
 
Registered: May 2007
Posts: 5

Original Poster
Rep: Reputation: 0
Quote:
Originally Posted by bigearsbilly
are the line numbers in the code?
No, Sir. The line numbers are not in the code
 
Old 05-09-2007, 02:46 AM   #8
bigearsbilly
Senior Member
 
Registered: Mar 2004
Location: england
Distribution: Mint, Armbian, NetBSD, Puppy, Raspbian
Posts: 3,515

Rep: Reputation: 239Reputation: 239Reputation: 239
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
 
Old 05-11-2007, 03:07 AM   #9
michael24h7d
LQ Newbie
 
Registered: May 2007
Posts: 5

Original Poster
Rep: Reputation: 0
Thank you so much Bigearsbilly. Finally, I solved my problem.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
giving search-string(s) from a file to linux "find" command Fond_of_Opensource Linux - Newbie 3 02-02-2009 06:14 PM
Shell Script: Find "Word" Run "Command" granatica Linux - Software 5 07-25-2007 07:42 AM
How to write a bash script to replace all "KH" to "K" in file ABC??? cqmyg5 Slackware 4 07-24-2007 09:00 AM
Can't install "glibmm" library. "configure" script can't find "sigc++-2.0&q kornerr Linux - General 4 05-10-2005 02:32 PM
1. shell script "find and replace" on text 2. java GUI application randomx Programming 4 03-05-2004 01:01 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 09:19 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration