Sponsored Content
Top Forums Shell Programming and Scripting sed or awk command to replace a string pattern with another string based on position of this string Post 302656059 by vivek d r on Thursday 14th of June 2012 07:50:28 AM
Old 06-14-2012
totally awesome... :-) a million thanks guruprasad...
if you have time can you please explain how this command works?

---------- Post updated at 05:20 PM ---------- Previous update was at 05:16 PM ----------

is the command 'number of column' specific..? cause just now i tried with a big file and it dint work :-(

the above wont work for

Code:
INSERT INTO `Table44` (`id`, `action`, `date`, `description`, `modifiedId`, `object`, `performedById`, `schemaDifference_blob_reserved`, `schemaDifference_reserved`, `sourceIPAddress`, `zoneId`, `lastModified`) VALUES 
(1,'Change','2011-05-05 00:00:00','User Account Updated',3,'Account',1,NULL,NULL,'',1,'2012-06-09 13:56:24');

 

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Find the position of a string and replace with another string

Hi, I have a file named "Test_2008_01_21" The file contains a string "manual" that occurs many times in the file How can i find the positions of the string "manual" in the file Ex: if the string " manual " occurs three times in the file. i want to replace the second occurance of string... (6 Replies)
Discussion started by: bab123
6 Replies

2. Shell Programming and Scripting

Search for a string and replace the searched string in the same position

Hi All, My requisite is to search for the string "0108"(which is the year and has come in the wrong year format) in a particular column say 4th column in a tab delimited file and then replace it with 2008(the correct year format) in the same position where 0108 was found..The issue is the last... (15 Replies)
Discussion started by: ganesh_248
15 Replies

3. UNIX for Dummies Questions & Answers

Search for a string and replace the searched string in the same position in samefile

Hi All, My requisite is to search for the string "0108"(which is the year and has come in the wrong year format) in a particular column say 4th column in a tab delimited file and then replace it with 2008(the correct year format) in the same position where 0108 was found in the same file..The... (27 Replies)
Discussion started by: ganesh_248
27 Replies

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

5. UNIX for Dummies Questions & Answers

Search a string in the file and then replace another string after that position

Hi I am looking for a particular string in a file.If the string exists, then I want to replace another string with some other text.Once replaced, search for the same text after that character position in the file. :wall: E.g: Actual File content: Hello Name: Nitin Raj Welcome to Unix... (4 Replies)
Discussion started by: dashing201
4 Replies

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

7. Shell Programming and Scripting

Using sed to replace a string in a specific position

I asked this before, but my problem got more complicated. Heres what I am trying to do: I'm trying to replace a string at a certain location with another string. Heres the file I'm trying to change: \E I want to replace the escape code at the 3rd line, 2nd column with this escape code... (3 Replies)
Discussion started by: tinman47
3 Replies

8. Shell Programming and Scripting

Search for a string at a particular position and replace with blank based on position

Hi, I have a file with multiple lines(fixed width dat file). I want to search for '02' in the positions 45-46 and if available, in that lines, I need to replace value in position 359 with blank. As I am new to unix, I am not able to figure out how to do this. Can you please help me to achieve... (9 Replies)
Discussion started by: Pradhikshan
9 Replies

9. Shell Programming and Scripting

Replace string in XML file with awk/sed with string from another

Sorry for the long/weird title but I'm stuck on a problem I have. I have this XML file: </member> <member> <name>TransactionID</name> <value><string>123456789123456</string></value> </member> <member> <name>Number</name> ... (9 Replies)
Discussion started by: cozzin
9 Replies

10. Shell Programming and Scripting

Replace string of a file with a string of another file for matches using grep,sed,awk

I have a file comp.pkglist which mention package version and release . In 'version change' and 'release change' line there are two versions 'old' and 'new' Version Change: --> Release Change: --> cat comp.pkglist Package list: nss-util-devel-3.28.4-1.el6_9.x86_64 Version Change: 3.28.4 -->... (1 Reply)
Discussion started by: Paras Pandey
1 Replies
DBIx::Class::Helper::Row::OnColumnChange(3pm)		User Contributed Perl Documentation	     DBIx::Class::Helper::Row::OnColumnChange(3pm)

NAME
DBIx::Class::Helper::Row::OnColumnChange - Do things when the values of a column change VERSION
version 2.013002 SYNOPSIS
package MyApp::Schema::Result::Account; use parent 'DBIx::Class::Core'; __PACKAGE__->load_components(qw(Helper::Row::OnColumnChange)); __PACKAGE__->table('Account'); __PACKAGE__->add_columns( id => { data_type => 'integer', is_auto_increment => 1, }, amount => { data_type => 'float', keep_storage_value => 1, }, ); __PACKAGE__->before_column_change( amount => { method => 'bank_transfer', txn_wrap => 1, } ); sub bank_transfer { my ($self, $old_value, $new_value) = @_; my $delta = abs($old_value - $new_value); if ($old_value < $new_value) { Bank->subtract($delta) } else { Bank->add($delta) } } 1; or with DBIx::Class::Candy: package MyApp::Schema::Result::Account; use DBIx::Class::Candy -components => ['Helper::Row::OnColumnChange']; table 'Account'; column id => { data_type => 'integer', is_auto_increment => 1, }; column amount => { data_type => 'float', keep_storage_value => 1, }; before_column_change amount => { method => 'bank_transfer', txn_wrap => 1, }; sub bank_transfer { my ($self, $old_value, $new_value) = @_; my $delta = abs($old_value - $new_value); if ($old_value < $new_value) { Bank->subtract($delta) } else { Bank->add($delta) } } 1; DESCRIPTION
This module codifies a pattern that I've used in a number of projects, namely that of doing something when a column changes it's value in the database. It leverages DBIx::Class::Helper::Row::StorageValues for passing in the $old_value, which do not have to use. If you leave the "keep_storage_value" out of the column definition it will just pass "undef" in as the $old_value. Also note the "txn_wrap" option. This allows you to specify that you want the call to "update" and the call to the method you requested to be wrapped in a transaction. If you end up calling more than one method due to multple column change methods and more than one specify "txn_wrap" it will still only wrap once. I've gone to great lengths to ensure that order is preserved, so "before" and "around" changes are called in order of definition and "after" changes are called in reverse order. To be clear, the change methods only get called if the value will be changed after "update" runs. It correctly looks at the current value of the column as well as the arguments passed to "update". METHODS
before_column_change __PACKAGE__->before_column_change( col_name => { method => 'method', # <-- anything that can be called as a method txn_wrap => 1, # <-- true if you want it to be wrapped in a txn } ); Note: the arguments passed to "method" will be "$self, $old_value, $new_value". after_column_change __PACKAGE__->after_column_change( col_name => { method => 'method', # <-- anything that can be called as a method txn_wrap => 1, # <-- true if you want it to be wrapped in a txn } ); Note: the arguments passed to "method" will be "$self, $old_value, $new_value". around_column_change __PACKAGE__->around_column_change( col_name => { method => 'method', # <-- anything that can be called as a method txn_wrap => 1, # <-- true if you want it to be wrapped in a txn } ); Note: the arguments passed to "method" will be "$self, $next, $old_value, $new_value". Around is subtly different than the other two callbacks. You must call $next in your method or it will not work at all. A silly example of how this is done could be: sub around_change_name { my ($self, $next, $old, $new) = @_; my $govt_records = $self->govt_records; $next->(); $govt_records->update({ name => $new }); } Note: the above code implies a weird database schema. I haven't actually seen a time when I've needed around yet, but it seems like there is a use-case. Also Note: you don't get to change the args to $next. If you think you should be able to, you probably don't understand what this component is for. That or you know something I don't (equally likely.) CANDY EXPORTS
If used in conjunction with DBIx::Class::Candy this component will export: before_column_change around_column_change after_column_change AUTHOR
Arthur Axel "fREW" Schmidt <frioux+cpan@gmail.com> COPYRIGHT AND LICENSE
This software is copyright (c) 2012 by Arthur Axel "fREW" Schmidt. This is free software; you can redistribute it and/or modify it under the same terms as the Perl 5 programming language system itself. perl v5.14.2 2012-06-18 DBIx::Class::Helper::Row::OnColumnChange(3pm)
All times are GMT -4. The time now is 07:41 AM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy