find and replace - multiple combinations


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting find and replace - multiple combinations
# 1  
Old 11-14-2016
find and replace - multiple combinations

Hello all,

I'm trying to replace all possible combinations of specific character occurrences within a string with another character, that is:

For the following string:
Code:
ABCAACD

I want to replace all the possible combinations of the character 'A' with the character 'G', so the output should be:
Code:
GBCAACD
GBCGACD
GBCGGCD
ABCGACD
ABCGGCD
ABCAGCD

Does anyone know if there is a linux command that can do it?

Thanks a lot for any help!

Last edited by rbatte1; 11-14-2016 at 12:19 PM.. Reason: Added CODE tags
# 2  
Old 11-14-2016
How about
Code:
awk '
        {for (i=1; i<=NF; i++)   if ($i == "A") AC[++C] = i
         for (i=1; i<=C; i++)   {SAV = $0
                                 for (j=i; j<=C; j++)   {$AC[j] = "G"
                                                         print
                                                        }
                                 $0 = SAV
                                }
        }
' FS= OFS= file
GBCAACD
GBCGACD
GBCGGCD
ABCGACD
ABCGGCD
ABCAGCD

# 3  
Old 11-14-2016
Hi,
Or with sed:
Code:
$ echo ABCAACD | sed  'h;:bcl;s/A/_/;H;tbcl;g;s/\(.*\)\n.*\n.*/\1/' | sed -n ':deb;s/A/G/p;tdeb' | sed 's/_/A/g'
GBCAACD
GBCGACD
GBCGGCD
ABCGACD
ABCGGCD
ABCAGCD

Regards.
# 4  
Old 11-14-2016
Quote:
Originally Posted by hagit
Hello all,

I'm trying to replace all possible combinations of specific character occurrences within a string with another character, that is:

For the following string:
Code:
ABCAACD

I want to replace all the possible combinations of the character 'A' with the character 'G', so the output should be:
Code:
GBCAACD
GBCGACD
GBCGGCD
ABCGACD
ABCGGCD
ABCAGCD

Does anyone know if there is a linux command that can do it?

Thanks a lot for any help!
If you want all combinations, why isn't GBCAGCD included in the desired output?

You haven't said what operating system you're using, and some of the suggestions you have received so far will do what was requested on Linux systems, but not on most BSD and UNIX systems.

If you do want all combinations, you might try something more like:
Code:
printf '%s\n' ABCAACD A AA AAAA | awk -FA '
{	printf("Input:\t%s\n", $0)
	for(i = 1; i < 2 ^ (NF - 1); i++) {
		x = i
		for(j = 1; j < NF; j++) {
			printf("%s%s", $j, (x % 2) ? "G" : "A")
			x = int(x / 2)
		}
		printf("%s%s", $NF, ORS)
	}
}'

which produces the output:
Code:
Input:	ABCAACD
GBCAACD
ABCGACD
GBCGACD
ABCAGCD
GBCAGCD
ABCGGCD
GBCGGCD
Input:	A
G
Input:	AA
GA
AG
GG
Input:	AAAA
GAAA
AGAA
GGAA
AAGA
GAGA
AGGA
GGGA
AAAG
GAAG
AGAG
GGAG
AAGG
GAGG
AGGG
GGGG

If you want to try this on a Solaris/SunOS system, change awk to /usr/xpg4/bin/awk or nawk.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Find and replace from multiple files

Hello everybody, I need your help. I have a php site that was expoited, the hacker has injected into many php files a phishing code that was discovered and removed in order to have again a clean code. Now we need to remove from many php files that malware. I need to create a script that find and... (2 Replies)
Discussion started by: ninocap
2 Replies

2. Shell Programming and Scripting

Find and replace in multiple files

Hi, I have php files in main dir and sub dir's as well. I need to find "new mysqli('localhost', 'System', 'xxxxxx', 'System', '3306');" and replace as "new mysqli('localhost', 'unx_sys', 'yyyy', 'unx_sys', '3306');" I tried like: sed 's/new mysqli\(*\)\;$/new... (1 Reply)
Discussion started by: ashokvpp
1 Replies

3. Shell Programming and Scripting

Script to find & replace a multiple lines string across multiple php files and subdirectories

Hey guys. I know pratically 0 about Linux, so could anyone please give me instructions on how to accomplish this ? The distro is RedHat 4.1.2 and i need to find and replace a multiple lines string in several php files across subdirectories. So lets say im at root/dir1/dir2/ , when i execute... (12 Replies)
Discussion started by: spfc_dmt
12 Replies

4. Shell Programming and Scripting

Find and Replace in multiple fields using awk

Hi, Say I have a record "1|22| | |". In which the third and fourth fields are <space> alone. I have to replace the <Space> with <null>. Input: "1|22| | |" --> "1|22|<space> |<space> |" Expected output: "1|22|||" --> "1|22|<null> |<null>|" I tried: echo "1|22| | |" | awk -F... (4 Replies)
Discussion started by: machomaddy
4 Replies

5. Shell Programming and Scripting

Find and replace multiple lines

I have a section of text in file A, see below # falkdjf lkjadf lkjadf lkajdf lkajdf lkajdf lkjadf lkjadf 234.234.2.234 lkjlkjlk 234.234.3.234 # Only the first line with "# falkdjf lkjadf lkjadf" is unique in the file. The new section that I want to overwrite the old section above is in... (1 Reply)
Discussion started by: jyang72211
1 Replies

6. Shell Programming and Scripting

SED multiple find and replace

Hi, searched through the forums and not really found what I am looking for. I am a bit of novice when it comes to anything above basic scripting and not even that when it comes to the sed command. I have been reading the tutorials online but still struggling to get what I need :wall: ... (10 Replies)
Discussion started by: colinwilson1303
10 Replies

7. UNIX for Dummies Questions & Answers

how to find and replace strings in multiple files

Hi All, Iam new to unix, I need to find string and replace it in the file name. Like text_123_0.txt,text_123_1.txt,text_123_2.txt. I need to search 123 and replace it with 234 . Is there any unix command to replace them in single command since i have 5 directories. So i need to go each and every... (0 Replies)
Discussion started by: etldeveloper
0 Replies

8. Shell Programming and Scripting

sed find and replace multiple lines

I am new to linux and would like to modify the contents of a file preferably using a one line. The situation is as follows <start> some lines "I am the string" "replace string" more lines here <end> In the above example,On encountering "I am the string", the "replace string "should be... (6 Replies)
Discussion started by: supersimha
6 Replies

9. UNIX for Dummies Questions & Answers

Find and replace a string in multiple files

I used the following script cd pathname for y in `ls *`; do sed "s/ABCD/DCBA/g" $y > temp; mv temp $y; done and it worked fine for finding and replacing strings with names etc. in all files of the given path. I'm trying to replace a string which consists of path (location of file) ... (2 Replies)
Discussion started by: pharos467
2 Replies

10. Shell Programming and Scripting

Find and replace files in multiple folders

Hi there, I would like to write a script to automate the copy and renaming of files in multiple dir. I have a generic file named s253e.prb and would like to copy this to multiple dir and rename it. Example: Dir is AL-M1 and the prb file name is AL-M1.prb. I would like to be able to... (6 Replies)
Discussion started by: lodey
6 Replies
Login or Register to Ask a Question