Replace variable string with text


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Replace variable string with text
# 1  
Old 08-07-2015
Replace variable string with text

Hi All, Hoping someone can help....

I am trying to work out how I can ammend a log file to remove variable strings in order to remove confidential information which I cant pass on.

As an example I have used phone numbers. A large log file contains multiple lines containing something like the below

Code:
ses:3317] start call ms <xxxxxxxxxxxx> id < 13102 > id2 <0a23> index 858

In the code above i have placed x's where the variable numbers will be that i need to change, these varible numbers i need to be able to anonymize/replace. Is there a way of doing this ? Note that the numbers will always start with a set prefix as the first 2/3 digits also.

Any help would be much appreciated.

Thanks
# 2  
Old 08-07-2015
I'm not sure I understand what you're trying to do. Do you have a file that contains a string a numbers on each input line that you want to change each digit in that string to an "x"? (Could there by hyphens, plus-signs, and/or parentheses in the string? If so, should they be changed too?)

Or do you have a file that contains a string of "x" characters that you want to change to a phone number?

Are all lines in the same format, or are there other lines in different formats that are not supposed to be modified?

Please show us sample input (source file and whatever defines the "set prefix") and the corresponding output you want to produce from those inputs.
# 3  
Old 08-07-2015
Hi Don - the file contains numbers which need to be changed to x's.

There are different formatted lines however say if the line contained a 10 digit number and started with 078 this should be modified to x's this will then allow me to amend the prefix accordingly.

The code in my post is the output I want to obtain, the only difference between the source and output is where the x's are there are numbers.

Thanks
# 4  
Old 08-07-2015
For the sample output you showed us (with the output being 10 "x"s surrounded by angle brackets), the following should work with any version of sed that conforms to the standards:
Code:
sed 's/<078[[:digit:]]\{7\}>/<xxxxxxxxxx>/g' file

This User Gave Thanks to Don Cragun For This Post:
# 5  
Old 08-08-2015
If we are dealing only with 10 digit numbers lead by "078", that will work. If not, we need to know more to identify the digits to be replaced, like "the first angle bracketed number in the resp. line", or "the number following the 'start call' string"
This User Gave Thanks to RudiC For This Post:
# 6  
Old 08-08-2015
Isolate the number pattern and place it inside the capturing group in red.
Code:
$ cat test.file
ses:3317] start call ms <0781234567> id < 13102 > id2 <0a23> index 858
ses:3318] start call ms <0781234568> id < 13102 > id2 <0a23> index 858
ses:3319] start call ms <0801234568> id < 13102 > id2 <0a23> index 858
ses:3320] start call ms <0781234569> id < 13102 > id2 <0a23> index 858

$ perl -pe 's/(?<=<)(078\d+)(?=>)/x x length($1)/e' test.file
ses:3317] start call ms <xxxxxxxxxx> id < 13102 > id2 <0a23> index 858
ses:3318] start call ms <xxxxxxxxxx> id < 13102 > id2 <0a23> index 858
ses:3319] start call ms <0801234568> id < 13102 > id2 <0a23> index 858
ses:3320] start call ms <xxxxxxxxxx> id < 13102 > id2 <0a23> index 858

The red pattern can be just the current example if it would satisfy the phone number without matching any other unwanted parts.
It could be more specific as:
078(?:\d){7}
If you want to match, strictly, 078 followed by seven more digits.
Or, just \d{10} for any 10 digits.
Either way, you have flexibility since x x length($1) will convert whatever is matched into a length of x, accordingly.
Based on your example, it appears that there's only one occurrence of the phone number per line, therefore no attempt was done to match more than once per line.

Last edited by Aia; 08-08-2015 at 05:00 PM..
This User Gave Thanks to Aia For This Post:
# 7  
Old 08-08-2015
Thanks

Thanks chaps, worked a treat !
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

How to replace string in variable?

Hello, I have simple while and for loops in a shell script and I would like to replace some characters in COL2 when I run it. I am on ubuntu 14.04 while read COL1 COL2 COL3 COL4 do name=$COL2 #cat $name | sed -i "s|_| |g" $name for i in $COL3 $COL4 do some codes ...... run $i b="$name"... (11 Replies)
Discussion started by: baris35
11 Replies

2. Shell Programming and Scripting

Replace substring from a string variable

Hi, Wish to remove "DR-" from the string variable (var). var="DR-SERVER1" var=`echo $var | sed -e 's/DR-//g'` echo "$var" Expected Output: However, I get the below error: Can you please suggest. (4 Replies)
Discussion started by: mohtashims
4 Replies

3. Shell Programming and Scripting

replace (sed?) a string in file with multiple lines (string) from variable

Can someone tell me how I can do this? e.g: a=$(echo -e wert trewt ertert ertert ertert erttert erterte rterter tertertert ert) How do i replace the STRING with $a? I try this: sed -i 's/STRING/'"$a"'/g' filename.ext but this don' t work (2 Replies)
Discussion started by: jforce
2 Replies

4. Shell Programming and Scripting

Replace string in file with a variable value

Hi Fellows, I am new to shell, please help we me out in this.. i have file which some lines like this.. $$param1='12-jan-2011' $$param2='14-jan-2011' $$param3='30-jan-2011' . . .....so on.. I want to change $$param3 to '31-dec-2011'. i have variable which is storing(30-jan-2011 this... (1 Reply)
Discussion started by: victor369
1 Replies

5. Shell Programming and Scripting

replace text in file using variable

Hi, I'm making a script that automaticaly set file size and path in xml file. I tried with : sed -i 's/BOOTPATH/TEST/g' file.xml it works fine but if I use a viriable : sed -i 's/BOOTPATH/$bootpathf/g' file.xml with this one, no change are made. I don't understand why. If a make a ... (13 Replies)
Discussion started by: Toug
13 Replies

6. AIX

Replace string with asterisk(*) in variable

I was trying to replace a string ( for eg - @@asterisk@@ to * ) in variable using cat $INFILE | while read LINE do stmt1=`echo $LINE | sed 's/@@asterisk@@/\*/g'` stmt=$stmt' '$stmt1 stmt2=`echo $LINE` STATEMENT=$STATEMENT' '$stmt2 done echo 'Statement with sed -- > '... (5 Replies)
Discussion started by: Vaddadi
5 Replies

7. Shell Programming and Scripting

Using sed to replace a string in file with a string in a variable that contains spaces

Hi, i call my shell like: my_shell "my project name" my script: #!/bin/bash -vx projectname=$1 sed s/'PROJECT_NAME ='/'PROJECT_NAME = '$projectname/ <test_config_doxy >temp cp temp test_config_doxy the following error occurres: sed s/'PROJECT_NAME ... (2 Replies)
Discussion started by: vivelafete
2 Replies

8. Shell Programming and Scripting

Replace string in a file w/ a variable value

I am trying to replace the default home page for several mac user accounts, I wrote a script that will hunt the files down and replace them with a pre-configured set. The problem I am having is that the download destination path for the browser is hard coded into a .plist (text config file) file... (5 Replies)
Discussion started by: tret
5 Replies

9. UNIX for Dummies Questions & Answers

sed to replace text with a variable

I'm trying to replace text in a file with text from a variable I have the following in my script, but its not working: #!/bin/ksh echo "Enter the path to load scripts" read x echo "updating the templates" sed "s/CHANGE_ME_TO_LOAD_PATH/"$x"/g" LoadFiles.sh > LoadFiles2.sh I thought... (1 Reply)
Discussion started by: orahi001
1 Replies

10. Shell Programming and Scripting

find and replace text with a variable?

Is there a way that I can make the following work with using variables? perl -pi -e 's#blah#hrm#ig' replacetext but like this var=blah perl -pi -e 's#$var#hrm#ig' replacetext (3 Replies)
Discussion started by: doublejz
3 Replies
Login or Register to Ask a Question