File pattern changing


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting File pattern changing
# 8  
Old 10-12-2012
Even I make the values into variables, the data after @ is skipped. Please help.
# 9  
Old 10-15-2012
Hello Friends,

The above PERL works only for Alphabets and not for Alphanumeric or with Special characters.

Since that will be a Password column, User can provide any Character sequence. Please suggest some other Solution.
# 10  
Old 10-15-2012
Try this:
Code:
perl -e '$exists=shift @ARGV;$from=shift @ARGV;$to=shift @ARGV;
$fromlen=length($from);$tolen=length($to);
$^I=".bak";
while(<>) {
 if(index($_,$exists) != -1) {
  while(($s=index($_,$from,$nxt)) != -1) {
   substr($_,$s,$fromlen,$to);
   $nxt=$s+$tolen;
  }
 }
 print;
}' 'sathya' '123' 'S@THYA01' infile

The first argument (sathya in this case) is the "string" which should be present in the line to attempt substitution.
The second argument (123 here) is the "string" you want to search for.
The third argument (S@THYA01 here) is the replacement "string".
The fourth argument (infile here) is the file to be changed.
Backup of the original file will be taken with .bak extension.

Last edited by elixir_sinari; 10-15-2012 at 05:24 AM..
This User Gave Thanks to elixir_sinari For This Post:
# 11  
Old 10-15-2012
Many thanks Knight. How can we include this in it
Code:
if /sathya/'

# 12  
Old 10-15-2012
find . -type f -print | xargs perl -p -i -e "s/pattern search string /replace string /g"

Code:
find . -type f -print | xargs perl -p -i -e "s/pattern search string/replace string /g"


Last edited by Franklin52; 10-15-2012 at 06:02 AM.. Reason: Please use code tags for data and code samples
# 13  
Old 10-15-2012
I've edited my earlier post. Check it now.
# 14  
Old 10-16-2012
Thanks Knight, I will check it.

@Saud Pasha: I should have to specify my file name in the find, isn't it ?

find . -name "filename" -type f | xargs perl -p -i -e "s/pattern search string
/replace string /g"

also not only that, I have to find out a pattern "sathya" and only after that I have to replace the value of its 3rd column.

---------- Post updated 10-16-12 at 02:00 AM ---------- Previous update was 10-15-12 at 03:41 AM ----------

Thanks Knight , it works Smilie

---------- Post updated at 02:42 AM ---------- Previous update was at 02:00 AM ----------

Why the below is not working ?

My file is
Code:
>> cat test.txt
sathya yuiop New
ramesh sdgh 456
santhosh sdghsdf 789

I just want to try how this works with the awk as said by itkamaraj.

Code:
#!/bin/sh

var1=sathya
pwd=New
npwd=S@thya01


echo $pwd $npwd | awk -v var=$var1 '/var.*$1/{gsub("$1","$2")}1' test.txt > temp.txt
cat temp.txt > test.txt
rm temp.txt

After executing the above, my file contents remains the same.

But if I execute the awk statement without variables, it works as expected.
Please advise me on what am i missing.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Big pattern file matching within another pattern file in awk or shell

Hi I need to do a patten match between files . I am new to shell scripting and have come up with this so far. It take 50 seconds to process files of 2mb size . I need to tune this code as file size will be around 50mb and need to save time. Main issue is that I need to search the pattern from... (2 Replies)
Discussion started by: nitin_daharwal
2 Replies

2. Shell Programming and Scripting

Searching the pattern and accordingly changing the contents using shell

HI, I have File1 which contains :- admins = anand,satheesha,user1,user2,user3,user4,user5,user10,vishal nonadmins = read-only @admins = rw @nonadmins = r One shell script, using that I want to change the File1 as per user input (let's say $1) which have value as 'John', so now I want to... (6 Replies)
Discussion started by: Vishal Mishra
6 Replies

3. Shell Programming and Scripting

Finding the pattern and replacing the pattern inside the file

i have little challenge, help me out.i have a file where i have a value declared and and i have to replace the value when called. for example i have the value for abc and ccc. now i have to substitute the value of value abc and ccc in the place of them. Input File: go to &abc=ddd; if... (16 Replies)
Discussion started by: saaisiva
16 Replies

4. Shell Programming and Scripting

How to use sed to search a particular pattern in a file backward after a pattern is matched.?

Hi, I have two files file1.txt and file2.txt. Please see the attachments. In file2.txt (which actually is a diff output between two versions of file1.txt.), I extract the pattern corresponding to 1172c1172. Now ,In file1.txt I have to search for this pattern 1172c1172 and if found, I have to... (9 Replies)
Discussion started by: saurabh kumar
9 Replies

5. Shell Programming and Scripting

Search for a pattern in a String file and count the occurance of each pattern

I am trying to search a file for a patterns ERR- in a file and return a count for each of the error reported Input file is a free flowing file without any format example of output ERR-00001=5 .... ERR-01010=10 ..... ERR-99999=10 (4 Replies)
Discussion started by: swayam123
4 Replies

6. Shell Programming and Scripting

Changing the first occurrence after matching a pattern

Hi, I got a file which looks like this: Value A Status: - Other: - Value B Status: - Other: - Value C Status: - Other: - I would like to change only the first line which includes the "Status:" string after matching the line containing "Value B", so the output file should look... (5 Replies)
Discussion started by: wenclu
5 Replies

7. Shell Programming and Scripting

AWK match $1 $2 pattern in file 1 to $1 $2 pattern in file2

Hi, I have 2 files that I have modified to basically match each other, however I want to determine what (if any) line in file 1 does not exist in file 2. I need to match column $1 and $2 as a single string in file1 to $1 and $2 in file2 as these two columns create a match. I'm stuck in an AWK... (9 Replies)
Discussion started by: right_coaster
9 Replies

8. Shell Programming and Scripting

Searching a pattern in file and deleting th ewhole line containing the pattern

Hi All, Please can someone assist in the script I have made that searches a pattern in a file and delete the whole line containing the pattern. #!bin/sh # The pattern that user want to add to the files echo "Enter the pattern of the redirect" read value # check if the user has... (1 Reply)
Discussion started by: Shazin
1 Replies

9. Shell Programming and Scripting

pattern changing

Hi, I have a file with contents in this format.... ... (3 Replies)
Discussion started by: kripssmart
3 Replies

10. Shell Programming and Scripting

Changing Pattern in a file

I have a config file with the pattern given below. /usr/games cocoa fudge # export to only these machines /usr -access=clients # export to my clients /usr/local # export to the world /usr2 -access=bison:deer:pup ... (0 Replies)
Discussion started by: pbtenneti
0 Replies
Login or Register to Ask a Question