Sponsored Content
Top Forums UNIX for Dummies Questions & Answers Find and replace a string in multiple files Post 302143942 by pharos467 on Monday 5th of November 2007 01:02:58 PM
Old 11-05-2007
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)

say instead of ABCD i have to replace c:/mydocuments/pictures to
d:/mypics/personal , as metacharacters wont be searched in unix this script is failing to replace the string which has a path in it.

now my script is


cd pathname
for y in `ls *`;
do sed "s/'c:/mydocuments/pictures'/'d:/mypics/personal'/g" $y > temp; mv temp $y;
done





i tired giving the path in single quotes and double quotes, but i see error

sed: command garbled: s/'c:/mydocuments/pictures'/'d:/mypics/personal'/g



And all the contents of the files in the path are erased.
Smilie
Is there any other way to work this out.

Thanks
 

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

shell script to find and replace 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) ... (11 Replies)
Discussion started by: pharos467
11 Replies

2. Shell Programming and Scripting

awk find and replace in multiple files

Hi I use the following code to replace ‘.' with ‘N' in my files and keep both versions. awk '{ gsub(/\./,"N"); print }' file_0001.txt > path/to/new/dir/file_0001.txt I need help on how to apply the code to 100 files instead of doing them one file at a time. The files are labeled... (7 Replies)
Discussion started by: jdhahbi
7 Replies

3. Shell Programming and Scripting

Find & Replace string in multiple files & folders using perl

find . -type f -name "*.sql" -print|xargs perl -i -pe 's/pattern/replaced/g' this is simple logic to find and replace in multiple files & folders Hope this helps. Thanks Zaheer (0 Replies)
Discussion started by: Zaheer.mic
0 Replies

4. Shell Programming and Scripting

String search and replace in multiple files.

Hello. I have five config files in /etc that I want to edit in one click for testing. I would like to make a script like this : #!/bin/bash # a_file="/etc/file_1" src_str="src_string_1" rpl_str="rpl_string_1" calling_sed_or_awk_or_whatelse $a_file search_for_all $src_str replace_with... (4 Replies)
Discussion started by: jcdole
4 Replies

5. UNIX for Dummies Questions & Answers

replace string in multiple files

Hi, I'm new to Unix. My understanding of Unix and its command is very limited. I have about 1000 text files that have a word in it that I need to replace with a different word. e.g. a.txt has 1 line of txt: monday, tuesday, wednesday b.txt has 1 line of txt: monday, tuesday,... (5 Replies)
Discussion started by: millsy5
5 Replies

6. 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

7. Shell Programming and Scripting

Find multiple strings and replace single string

Hi, following Perl code i used for finding multiple strings and replace with single string. code: #!/usr/bin/perl my @files = <*.txt>; foreach $fileName (@files) { print "$fileName\n"; my $searchStr = ',rdata\)' | ',,rdata\)' | ', ,rdata\)'; my $replaceStr =... (2 Replies)
Discussion started by: chettyravi
2 Replies

8. 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

9. 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

10. UNIX for Beginners Questions & Answers

Find all .sh files in file system and need to replace the string inside .sh files

Hi All, I need to write a script to find all "*.sh" files in /home file system and if any string find "*.sh" files with the name vijay@gmail.com need to replace with vijay.bhaskar@gmail.com. I just understood about the find the command to search .sh files. Please help me on this. find / -name... (3 Replies)
Discussion started by: bhas85
3 Replies
BBE(1)																	    BBE(1)

NAME
bbe - binary block editor SYNOPSIS
bbe [options]... DESCRIPTION
bbe is a sed-like editor for binary files. It performs binary transformations on the blocks of input stream. OPTIONS
bbe accepts the following options: -b, --block=BLOCK Block definition. -e, --expression=COMMAND Add the COMMAND to the commands to be executed. -f, --file=script-file Add the contest of script-file to commands. -o, --output=name Write output to name instead of standard output. -s, --suppress Suppress normal output, print only block contents. -?, --help List all available options and their meanings. -V, --version Show version of program. BLOCK can be defined as: N:M Where N'th byte starts a M bytes long block (first byte is 0). :M Block length in input stream is M. /start/:M String start starts M bytes long block. /start/:/stop/ String start starts the block and block ends to string stop. /start/: String start starts the block and block will end at next occurence of start. Only the first start is included to the block. :/stop/ Block starts at the beginning of input stream (or at the end of previous block) and ends at the next occurrence of stop. String stop will be included to the block. Special value '$' of M means the end of stream. Default value for block is 0:$, meaning the whole input stream. Both start and stop strings are included to block. Nonprintable characters can be escaped as nn decimal xnn hexadecimal nnn octal Character '' can be escaped as '\'. Escape codes 'a','',' ',' ','v','f',' ' and ';' can also be used. Length (N and M) can be defined as decimal (n), hexadecimal (xn) or octal (0n) value. COMMAND SYNOPSIS
bbe has two type of commands: block and byte commands, both are allways related to current block. That means that the input stream outside of block remains untouched. Block commands D [n] Delete the n'th block. Without n, all found blocks are deleted from the output stream. I string Insert the string string before the block. A string Append the string string at the end of block. J n Skip n blocks before executing commands after this command. L n Leave all blocks unmodified starting from block number n. Affects only commands after this command. N Before printing a block, the file name in which the block starts is printed. F f Before printing a block, the input stream offset at the begining of the block is printed. f can be H, D or O for Hexadecimal, Deci- mal or Octal format of offset. B f Before printing a block, the block number is printed (first block == 1) f can be H, D or O for Hexadecimal, Decimal or Octal format of block number. > file Before printing a block, the contents of file file is printed. < file After printing a block, the contents of file file is printed. Byte commands n in byte commands is offset from the beginning of current block (starts from zero). r n string Replace bytes starting at position n with string string. i n string Insert string starting at position n. p format The contents of block is printed in format defined by format. format can have any of the formats H, D, O, A and B for Hexadecimal, Decimal, Octal, Asciii and Binary. s/search/replace/ Replace all occurrences of search with replace. y/source/dest/ Translate bytes in source to the corresponding bytes in dest. Source and dest must have equal length. d n m|* Delete m bytes starting from the offset n. If * is defined instead of m, then all bytes starting from n are deleted. c from to Convert bytes from format from to to. Currently supported formats are: BCD Binary coded decimal ASC Ascii j n Commands after the j-command are ignored for first n bytes of the block. l n Commands after the l-command are ignored from n'th byte of the block. w file Write bytes from the current block to file file. Commands before w-command have effect to what will be written. %B or %nB in file will be replaced by current block number. n in %nB is field length, leading zero in n causes the block number to be left padded with zeroes. & c Performs binary and with c. | c Performs binary or with c. ^ c Performs binary xor with c. ~ Performs binary negation. u n c All bytes from start of the block to offset n are replaced by c. f n c All bytes starting from offset n to end of the block are replaced by c. x Exchange the contents of nibbles (half an octet) of bytes. Nonvisible characters in strings can be escaped same way as in block definition strings. Character '/' in s and y commands can be any visi- ble character. Note that the D, A, I, F, B, c, s, i, y, p, <, > and d commands cause the length of input and output streams to be different. EXAMPLES
bbe -e "s/c:\temp\data1.txt/c:\temp\data2.txt/" file1 all occurences of "c: empdata1.txt" in file file1 are changed to "c: empdata2.txt" bbe -b 0420:16 -e "r 4 x12x4a" file1 Two bytes starting at fifth byte of a 16 byte long block starting at offset 0420 (octal) in file1 are changed to hexadecimal values 12 and 4a. bbe -b :16 -e "A x0a" file1 Newline is added after every block, block length is 16. SEE ALSO
sed(1). AUTHOR
Timo Savinen <tjsa@iki.fi> 2006-11-02 BBE(1)
All times are GMT -4. The time now is 08:21 PM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy