Character Find and replace in Unix or Perl


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Character Find and replace in Unix or Perl
# 1  
Old 03-20-2012
Character Find and replace in Unix or Perl

Hi,
I am stuck with an problem and want some help, what i want to do is

There is a directory name temp
which include file named t1.txt, t2,txt, t3.txt and so on.

These files contains data, but there are some bad character also that is % present in the files , I want to write the script which should open the files present in a directory one by one and if that character found delete it.

I should give that bad character during call of the script
eg ./scriptname .sh % OR ./scriptname.pl %

Thanks

Last edited by parthmittal2007; 03-20-2012 at 10:19 AM..
# 2  
Old 03-20-2012
Quick and dirty Perl solution, you should:
  1. enable strict and warnings
  2. check for regex special characters in $ARGV[0] and escape them
  3. use File::Temp rather than the lazy option below which would clobber a file called temp.dat in the current directory

Code:
perl -e '@filenames=glob("./*.txt");
for $filename (@filenames){
    open($file ,"<",$filename);
    open ($temp,">","temp.dat");
    while(<$file>){
        $_=~s/$ARGV[0]//g;
        print $temp $_;
    }
    close $file;
    close $temp;
    rename "temp.dat", $filename;
}' %

# 3  
Old 03-20-2012
This is a single line command any script if possible
This will a extra file which is not needed.

Thanks
# 4  
Old 03-20-2012
Code:
perl -npi.bak -e 'BEGIN{$sep=shift;}s/$sep//g;' % *.txt

But again a regex special character could make this behave in an unexpected fashion.
# 5  
Old 03-20-2012
But this too make an extra file like a1.txt.bak, This file do not wanted
# 6  
Old 03-20-2012
Remove '.bak' from that one-liner.
# 7  
Old 03-20-2012
The UNIX Guru is back

---------- Post updated at 10:54 AM ---------- Previous update was at 10:52 AM ----------

After removing also the .bak file is forming
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

Find and replace single character w/awk given conditions

I have a file that looks like this: 14985 DPN verb PPa to spend. 12886 DPNDJN bay tree. 15686 DQ verb to observe 15656 KC ... (7 Replies)
Discussion started by: jvoot
7 Replies

2. Shell Programming and Scripting

Find and replace, perl code

Hi, Can somebody tell whats wrong with "find and replace perl code". I wanted to find "\n".write(status,data" and replace with "\n",data" in multipls files. #!/usr/bin/perl my @files = <*.c>; foreach $fileName (@files) { print "$fileName\n"; my $searchStr0 =... (4 Replies)
Discussion started by: chettyravi
4 Replies

3. Shell Programming and Scripting

[Solved] Find and replace till nth occurence of a special character

Hi, I have a requirement to search for a pattern in each line in a file and remove the in between words till the 3rd occurrence of double quote ("). Ex: CREATE TABLE "SCHEMANAME"."AMS_LTV_STATUS" (Note: "SCHEMANAME" may changes for different schemas. Its not a fixed value) I need to... (2 Replies)
Discussion started by: satyaatcgi
2 Replies

4. Shell Programming and Scripting

Find character and Replace character for given position

Hi, i want find the character '-' in a file from position 284-298, if it occurs i need to replace it with 'O ' for the position in the file. How to do that using SED command. thanks in advance, Sara (9 Replies)
Discussion started by: Sara183
9 Replies

5. Shell Programming and Scripting

How replace -- character in perl

Hi All, I am having below issue could anybody help me. $x= SELECT * FROM EMP --xyz this change is done in q2; I want delete the charchters from -- to till the end. I want $x to be $x= SELECT * FROM EMP; Thanks, Vijay G (1 Reply)
Discussion started by: gvk25
1 Replies

6. Shell Programming and Scripting

how to replace a particular character in perl

Hi, I have a file with two string : aa_bb_cc_def gg_hh_jj_xyz now i want a command in perl script, which gives me the result as : aa_bb_cc.def gg_hh_jj.xyz Can anyone help me on this plz.. thanks in advane. (6 Replies)
Discussion started by: arup1980
6 Replies

7. Shell Programming and Scripting

perl find and replace

Hi, Please could someone advise, how I can resolve this issue with my find and replace command : perl -i -npe "s#RLM_LICENSE.*#RLM_LICENSE=$TEAM_TOP/licenses/abc.demo.lic#;" environment.properties $TEAM_TOP is an environment varible within my system. when i run this perl command from... (1 Reply)
Discussion started by: fettie
1 Replies

8. Shell Programming and Scripting

Find and replace a character

Hi Team, i have 1st cloumn of data containing, LAMSBA01-BA-COFF-YTD LAMSBA01-BA-COFF-ITD LAMSBA01-BA-AGGR-IND . LAMSBA01-BA-CURR-COFF-BAL i need to replace the "-" to "_" (underscore) using AWK . please help me on this. Thanks, Baski (4 Replies)
Discussion started by: baskivs
4 Replies

9. UNIX for Dummies Questions & Answers

Find and replace character in a string

Hi all, My problem is the following: I've a script that must list all files in a directory and write this information in a text file. I've tried to get the list through ls command and then write it using msgecho msgecho "`ls $PATH_APS_JOB_ORA`" This works good but the created string... (7 Replies)
Discussion started by: callimaco0082
7 Replies

10. Shell Programming and Scripting

Parentheses in perl find/replace

I'm trying to use the following command to do a batch find and replace in all commonly named files through a file hierarchy find . -name 'file' |xargs perl -pi -e 's/find/replace/g' which works fine except for a substitution involving parenthesis. As a specific example I'm trying to sub... (3 Replies)
Discussion started by: Jeffish
3 Replies
Login or Register to Ask a Question