Sponsored Content
Top Forums Shell Programming and Scripting Replace special characters in multiple files - perl Post 302343383 by RMSoares on Wednesday 12th of August 2009 10:01:39 AM
Old 08-12-2009
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

replace to : LOG_DCT = $ LOG_DIR / DCT_GERAL_ $ 1_ "$ DATAINI". log

I did tests with the instruction "perl -pi -e 's/DCT_GERAL/DCT_GERAL_ $ 1 /' filename”, but the result is not what we expected: LOG_DCT = $ LOG_DIR / DCT_GERAL__" $ DATAINI”.log replaces the $ 1 by null.

In the second case, I want to move the instruction of new line to the end of a printf

Replace to: printf "$IDCADEIA - $IDJOB - $HORAACTUAL - XPTO(YYYYMMDD) \n#">> $ LOGFILE

I tried using the command "perl -pi -e 's/printf "\n#/printf "/' filename” and "perl -pi -e' s/) ">> $ LOGFILE /) \n#"> > $ LOGFILE/' filename”, but since the string has special characters ( ), \ ", #) it gives an error in the replacement.

Accepted suggestions of how to make these changes to files without having to have them manually: Smilie


10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

Replace Special characters in a file

Hi, I have a data like this in a file, 402003279034002000100147626030003300010000000000002000029000000 ær^M^\MÍW^H I need to replace those special char to some other char like # or $ Is there any ways to do it... I tried commands tr,sed and many but it was not able to replace because... (1 Reply)
Discussion started by: solai
1 Replies

2. Shell Programming and Scripting

Single/Multiple Line with Special characters - Find & Replace in Unix Script

Hi, I am creating a script to do a find and replace single/multiple lines in a file with any number of lines. I have written a logic in a script that reads a reference file say "findrep" and populates two variables $FIND and $REPLACE print $FIND gives Hi How r $u Rahul() Note:... (0 Replies)
Discussion started by: r_sarnayak
0 Replies

3. Shell Programming and Scripting

Using sed to replace special characters

Hi everyone I have file1 contains: '7832' ' 8765 6543 I want a sed command that will format as: '7832' , '8765' , '6543' I tried sed -e s/\'//g -e 's/^*//;s/*$//' file1 > file2 sed -e :a -e '$!N; s/\n/ /; ta' file2 which gives: 7832 8765 6543 I need some help to continue with... (5 Replies)
Discussion started by: nimo
5 Replies

4. Solaris

How to replace special characters in vi?

Hi , I want to replace the special characters in the file. For eg: cat abc 1234/4455/acb 234/k/lll/ 234`fs`fd I want to replace / and ` with the letter a and the output should like below. How to achieve this. 1234a4455aacb 234akallla 234afsafd (2 Replies)
Discussion started by: rogerben
2 Replies

5. Shell Programming and Scripting

Replace special characters

I have a line ending with special character and 0 The special character is the field separator for this line in VI mode the file will look like below, but while cat the special character wont display i know the hexa code for the special character ^_ is \x1f and ascii code is \0037, ... (0 Replies)
Discussion started by: ratheeshjulk
0 Replies

6. Shell Programming and Scripting

Replace special characters with Escape characters?

i need to replace the any special characters with escape characters like below. test!=123-> test\!\=123 !@#$%^&*()-= to be replaced by \!\@\#\$\%\^\&\*\(\)\-\= (8 Replies)
Discussion started by: laknar
8 Replies

7. Shell Programming and Scripting

How to replace special characters?

Hi Unix Guru, I have an requirement for replace some specail characters in a file, my file came from mainframe. please see below example: when open it with vi 17896660|89059215|04/24/1998 00:00:00.000000| abc 123-453-1312^M<85>^M<85>|124557 if I run cat -v I got following:... (25 Replies)
Discussion started by: ken002
25 Replies

8. Shell Programming and Scripting

Help to replace the string with special characters

{"name":"alR_pl-ENVIRONMENT_192_168_211_123_sDK_PROVISION_7","description":"aLR_pl-ENVIRONMENT_192_168_211_123_sDK_PROVISION_7","json_class":"Chef::Role","default_attributes":{},"override_attributes":{"yoapp":{"jboss":"5.1.0","port":"2243","warname":"soap","datacenter":"alR","ip":"","... (3 Replies)
Discussion started by: nikhil jain
3 Replies

9. UNIX for Advanced & Expert Users

How to replace special characters?

Hi Team, I have data like this. |*|.5|*|0.2|*|A.B|*| Would like to add zero (0) before the decimal point where there is no zero as |*|0.5|*|0.2|*|A.B|*| How to replace |*|. with |*|0. I tried below command which didn't work echo '|*|.5|*|0.2|*|A.B|*' | sed... (4 Replies)
Discussion started by: Ravi.K
4 Replies

10. UNIX for Beginners Questions & Answers

Replace Pattern with another that has Special Characters

Hello Team, Any help would be much appreciated for the below scenario: I have a sed command below where I am trying to replace the contents of 'old_pkey' variable with 'new_pkey' variable in a Soap request file (delete_request.txt). This works fine for regular string values, but this new_pkey... (8 Replies)
Discussion started by: ChicagoBlues
8 Replies
exstr(1)							   User Commands							  exstr(1)

exstr - extract strings from source files SYNOPSIS
exstr filename... exstr -e filename... exstr -r [-d] filename... DESCRIPTION
The exstr utility is used to extract strings from C-language source files and replace them by calls to the message retrieval function (see gettxt(3C)). This utility will extract all character strings surrounded by double quotes, not just strings used as arguments to the printf command or the printf routine. In the first form, exstr finds all strings in the source files and writes them on the standard output. Each string is preceded by the source file name and a colon (:). The first step is to use exstr -e to extract a list of strings and save it in a file. Next, examine this list and determine which strings can be translated and subsequently retrieved by the message retrieval function. Then, modify this file by deleting lines that can't be translated and, for lines that can be translated, by adding the message file names and the message numbers as the fourth (msgfile) and fifth (msgnum) entries on a line. The message files named must have been created by mkmsgs(1) and exist in /usr/lib/locale/locale/LC_MES- SAGES . (The directory locale corresponds to the language in which the text strings are written; see setlocale(3C)). The message numbers used must correspond to the sequence numbers of strings in the message files. Now use this modified file as input to exstr -r to produce a new version of the original C-language source file in which the strings have been replaced by calls to the message retrieval function gettxt(). The msgfile and msgnum fields are used to construct the first argument to gettxt(). The second argument to gettxt() is printed if the message retrieval fails at run-time. This argument is the null string, unless the -d option is used. This utility cannot replace strings in all instances. For example, a static initialized character string cannot be replaced by a function call. A second example is that a string could be in a form of an escape sequence which could not be translated. In order not to break existing code, the files created by invoking exstr -e must be examined and lines containing strings not replaceable by function calls must be deleted. In some cases the code may require modifications so that strings can be extracted and replaced by calls to the message retrieval function. OPTIONS
The following options are supported: -e Extract a list of strings from the named C-language source files, with positional information. This list is produced on standard output in the following format: file:line:position:msgfile:msgnum:string file the name of a C-language source file line line number in the file position character position in the line msgfile null msgnum null string the extracted text string Normally you would redirect this output into a file. Then you would edit this file to add the values you want to use for msgfile and msgnum: msgfile the file that contains the text strings that will replace string. A file with this name must be created and installed in the appropriate place by the mkmsgs(1) utility. msgnum the sequence number of the string in msgfile. The next step is to use exstr -r to replace strings in file. -r Replace strings in a C-language source file with function calls to the message retrieval function gettxt(). -d This option is used together with the -r option. If the message retrieval fails when gettxt() is invoked at run-time, then the extracted string is printed. You would use the capability provided by exstr on an application program that needs to run in an inter- national environment and have messages print in more than one language. exstr replaces text strings with function calls that point at strings in a message data base. The data base used depends on the run-time value of the LC_MESSAGES environment variable (see environ(5)). EXAMPLES
Example 1 The following examples show uses of exstr Assume that the file example.c contains two strings: main() { printf("This is an example "); printf("Hello world! "); } The exstr utility, invoked with the argument example.c extracts strings from the named file and prints them on the standard output. example% exstr example.c produces the following output: example.c:This is an example example.c:Hello world! The exstr utility, invoked with the -e option and the argument example.c, and redirecting output to the file example.stringsout example% exstr -e example.c > example.stringsout produces the following output in the file example.stringsout example.c:3:8:::This is an example example.c:4:8:::Hello world! You must edit example.stringsout to add the values you want to use for the msgfile and msgnum fields before these strings can be replaced by calls to the retrieval function. If UX is the name of the message file, and the numbers 1 and 2 represent the sequence number of the strings in the file, here is what example.stringsout looks like after you add this information: example.c:3:8:UX:1:This is an example example.c:4:8:UX:2:Hello world! The exstr utility can now be invoked with the -r option to replace the strings in the source file by calls to the message retrieval func- tion gettxt(). example% exstr -r example.c <example.stringsout >intlexample.c produces the following output: extern char *gettxt(); main() { printf(gettxt("UX:1", "")); printf(gettxt("UX:2", "")); } The following example: example% exstr -rd example.c <example.stringsout >intlexample.c uses the extracted strings as a second argument to gettxt(): extern char *gettxt(); main() { printf(gettxt("UX:1", "This is an example ")); printf(gettxt("UX:2", "Hello world! ")); } FILES
/usr/lib/locale/locale/LC_MESSAGES/* files created by mkmsgs(1) ATTRIBUTES
See attributes(5) for descriptions of the following attributes: +-----------------------------+-----------------------------+ | ATTRIBUTE TYPE | ATTRIBUTE VALUE | +-----------------------------+-----------------------------+ |Availability |SUNWtoo | +-----------------------------+-----------------------------+ SEE ALSO
gettxt(1), mkmsgs(1), printf(1), srchtxt(1), gettxt(3C), printf(3C), setlocale(3C), attributes(5), environ(5) DIAGNOSTICS
The error messages produced by exstr are intended to be self-explanatory. They indicate errors in the command line or format errors encoun- tered within the input file. SunOS 5.11 5 Jul 1990 exstr(1)

Featured Tech Videos

All times are GMT -4. The time now is 04:41 AM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy