Recursivley search files and replace the characters


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Recursivley search files and replace the characters
# 8  
Old 11-24-2014
how to work this out if situation remains same only difference is now cnf file is a XML one instead flat file name=value

Code:
1st_FILE.cnf
<FILE_NAME> [TST_FILE]</FILE_NAME>
<FILE_VALUE> [FILE_NAME]</FILE_VALUE>

o/p should be
Code:
1st_FILE.cnf.new
<FILE_NAME>1st_FILE.txt</FILE_NAME>
<FILE_VALUE>1st_FILE.txt</FILE_VALUE>

Only tag name are going to same across different cnf files only variable names are different.
<FILE_NAME> and <FILE_VALUE>

Last edited by manas_ranjan; 11-24-2014 at 09:58 AM..
# 9  
Old 11-24-2014
Did you try to run that script on your new file? If I do, this is the result:
Code:
<FILE_NAME> 1st_FILE.txt</FILE_NAME>
<FILE_VALUE> 1st_FILE.txt</FILE_VALUE>

# 10  
Old 11-25-2014
I tried, for me the o/p looks like

Code:
<FILE_NAME>[TST_FILE</FILE_NAME>
<FILE_VALUE>[FILE_NAME</FILE_VALUE>

--> removed only extreme right ], not even replacing it

---------- Post updated at 04:36 AM ---------- Previous update was at 04:17 AM ----------

no worries, I was doing some little crappy mistake. Now I corrected it and working fine. thanks for your heads up.

---------- Post updated at 08:31 AM ---------- Previous update was at 04:36 AM ----------

now if variable.txt changed then how to reflect the change in .cnf file.

Code:
variable.txt
TST_FILE=1st_FILE.txt
FILE_NAME={1st_FILE.txt==}
2ND_TST_FILE=2nd_FILE.txt
REASON=test

awk is not working as o/p now looks like
Code:
1st_FILE.cnf.new
<FILE_NAME>1st_FILE.txt</FILE_NAME>
<FILE_VALUE>{1st_FILE.txt</FILE_VALUE>

whereas it should look like
Code:
1st_FILE.cnf.new
<FILE_NAME>1st_FILE.txt</FILE_NAME>
<FILE_VALUE>{1st_FILE.txt==}</FILE_VALUE>

this is due to fact that FS is "="
# 11  
Old 11-25-2014
Yes it is - any ideas from your side?
# 12  
Old 11-25-2014
honestly I don't have any clue how to consider only one string as filed separator(=) when there are multiple same characters resides in the value.

so how can I instruct awk to only consider first character(=) as field separator later same characters can be considered as normal characters.

like
Code:
FILE_NAME={1st_FILE.txt==}

I tried
Code:
awk -F"{|}" '{print $2}'
awk -F"{|}|=" '{print $2}'

nothing doing well.....so stuck....
# 13  
Old 11-25-2014
Well, I don't think there's a general solution for all possible scenarios. In this special case, try
Code:
awk 'NR==FNR {X=$0; gsub($1 FS,"",X);R[$1]=X;  next} {for (r in R) gsub ("[[]"r"[]]", R[r]); print}' FS="=" file1 file2
<FILE_NAME> 1st_FILE.txt</FILE_NAME>
<FILE_VALUE> {1st_FILE.txt==}</FILE_VALUE>

This User Gave Thanks to RudiC For This Post:
# 14  
Old 11-26-2014
thanks Rudi......it gives me more close to the result.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

Search special characters in a file and replace with meaningful text messages like Hello

Search special characters in a file and replace with meaningful text messages like Hello (2 Replies)
Discussion started by: raka_rjit
2 Replies

2. Shell Programming and Scripting

Search and Replace Extended Ascii Characters

We are getting extended Ascii characters in the input file and my requirement is to search and replace them with a space. I am using the following command LANG=C sed -e 's// /g' It is doing a good job, but in some cases it is replacing the extended characters with two spaces. So my input... (12 Replies)
Discussion started by: ysvsr1
12 Replies

3. Shell Programming and Scripting

search pattern and replace x-y characters in nth line after every match

Hi, I am looking for any script which can do the following. have to read a pattern from fileA and copy it to fileB. fileA: ... ... Header ... ... ..p1 ... ... fileB: .... .... Header (3 Replies)
Discussion started by: anilvk
3 Replies

4. Shell Programming and Scripting

Search and replace particular characters in fixed length-file

Masters, I have fixed length input file like FHEAD0000000001XXXX20090901 0000009000Y1000XXX2 THEAD000000000220090901 ITM0000109393813 430143504352N22SP 000000000000RN000000010000EA P0000000000000014390020090901 TTAIL0000000003000000 FTAIL00000000040000000002 Note... (4 Replies)
Discussion started by: bittoo
4 Replies

5. Shell Programming and Scripting

Replace special characters in multiple files - perl

I have 100 files, where i want to search a set of strings and make the replacement by other strings In the first case I want to include a parameter in the name of a file LOG_DCT = $ LOG_DIR/DCT_GERAL_"$DATAINI".log replace to : LOG_DCT = $ LOG_DIR / DCT_GERAL_ $ 1_ "$ DATAINI". log I did... (1 Reply)
Discussion started by: RMSoares
1 Replies

6. Shell Programming and Scripting

Search and Replace between two files

Hi all- I've got 2 files: One is the final results and one is a result set from a query. In the final results files I have placeholder strings in there that need to be replaced by the corresponding strings from the query file. So File#1 (FINAL RESULTS) LINEID CLIENT ID REP ... (1 Reply)
Discussion started by: Cailet
1 Replies

7. Shell Programming and Scripting

search and replace characters in one string

I have lines like: Dog Cat House Mouse Dog Cat House Mouse Dog Cat House Mouse Dog Cat House Mouse I'd like to replace characters only in $3. H -> Z s -> W e -> x Resulting in something like (where $1, $2, and $4 are not changed): Dog Cat ZouWx Mouse Dog Cat ZouWx Mouse... (3 Replies)
Discussion started by: dcfargo
3 Replies

8. Shell Programming and Scripting

Replace Characters for bunch of Files.

Hi, I am new to unix and looking out for some help in reading a file contents and replacing the characters, the requirement is I having a folder and having nearly 300 txt files, all the file contents contains some words we need to iterate all each and every files and need to find and replace it... (1 Reply)
Discussion started by: subrahmaniank
1 Replies

9. Shell Programming and Scripting

Search For Control M characters in files

Hi , I have special character control M in many of my files as below ersNet-Telnet-3.03/Makefile.PL100644 21166 144 612 7113770214 135 77 0ustar jayusers## -*- Perl -*-^M ^M use ExtUtils::MakeMaker qw(WriteMakefile);^M ^M WriteMakefile(NAME => "Net::Telnet",^M ... (4 Replies)
Discussion started by: Mohammed
4 Replies

10. Shell Programming and Scripting

Best way to search files for non-printable characters?

I need to check ftp'd incoming files for characters that are not alphanumeric,<tab>, <cr>, or <lf> characters. Each file would have 10-20,000 line with up to 3,000 characters per line. Should I use awk, sed, or grep and what would the command look like to do such a search? Thanks much to anyone... (2 Replies)
Discussion started by: jvander
2 Replies
Login or Register to Ask a Question