Sponsored Content
Operating Systems Linux Removing a character at specific position in a column Post 302957569 by RavinderSingh13 on Tuesday 13th of October 2015 02:34:41 AM
Old 10-13-2015
Hello Syeda,

Here is an example suppose you want to substitute the 2nd occurrence of _ in $2 then following may help you.
Input_file:
Code:
Ravinder gi_49482297_ref_YP_039521.1_ TESTing test123 sizth_column_ seventh eight_column_test
TEST121 gi_49482297_ref_YP_039521.1_ TESTing test123 sizth_column_ seventh eight_column_test
TEST1211 gi_49482315_ref_YP_039539.1_ TESTing test123 sizth_column_ seventh eight_column_test
TEST12134 gi_49482315_ref_YP_039539.1_ TESTing test123 sizth_column_ seventh eight_column_test

Following is the code for same.
Code:
awk -vvar=2 '{split($2, A,"_");{for(i=1;i<=length(A);i++){if((i-1)==var){k=""} else {k="_"};q=q?q k A[i]:A[i]};$2=q;;q=""}} 1'  Input_file

Output will be as follows.
Code:
Ravinder gi_49482297ref_YP_039521.1_ TESTing test123 sizth_column_ seventh eight_column_test
TEST121 gi_49482297ref_YP_039521.1_ TESTing test123 sizth_column_ seventh eight_column_test
TEST1211 gi_49482315ref_YP_039539.1_ TESTing test123 sizth_column_ seventh eight_column_test
TEST12134 gi_49482315ref_YP_039539.1_ TESTing test123 sizth_column_ seventh eight_column_test

Here I have given a variable named var=2 in my code as I wanted to change only second occurrence in $2 of _.
You could change it accordingly as per your requirement too. Hope this helps.

Thanks,
R. Singh
This User Gave Thanks to RavinderSingh13 For This Post:
 

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

How to add character in specific position of a string?

Hi All, I would like to use sed to add "-" between the following string: Value: 20060830 Result: 2006-08-30 Pls advice. Thx a lot Victor (5 Replies)
Discussion started by: victorlung
5 Replies

2. Shell Programming and Scripting

Print lines with specific character at nth position in a file

I need to print lines with character S at nth position in a file...can someone pl help me with appropriate awk command for this (2 Replies)
Discussion started by: manaswinig
2 Replies

3. Shell Programming and Scripting

Print lines with specific character at nth position in a file

I need to print lines with character S at nth position in a file...can someone pl help me with appropriate awk command for this (1 Reply)
Discussion started by: manaswinig
1 Replies

4. Shell Programming and Scripting

Insert character in a specific position of a file

Hi, I need to add Pipe (|) at 5th and 18th position of all records a file. How can I do this? I tried to add it at 5th position using the below code. It didnt work. Please help!!! awk '{substr($0,5,1) ~ /|/}{print}' $input_file > $temp_file (1 Reply)
Discussion started by: gpaulose
1 Replies

5. Shell Programming and Scripting

Using sed to replace specific character and specific position

I am trying to use sed to replace specific characters at a specific position in the file with a different value... can this be done? Example: File: A0199999123 A0199999124 A0199999125 Need to replace 99999 in positions 3-7 with 88888. Any help is appreciated. (5 Replies)
Discussion started by: programmer22
5 Replies

6. Shell Programming and Scripting

using awk removing newline and specific position

Hello Friends, Input File looks as follows: >FASTA Header1 line1 line2 line3 linen >FASTA Header2 Line1 Line2 linen >FASTA Header3 and so on ....... Output: Want something as: >FASTA Header1 line1line2line3linen >FASTA Header2 (5 Replies)
Discussion started by: Deep9000
5 Replies

7. Shell Programming and Scripting

Removing 0 from a specific position - if it exists

I have a file that I need to parse using a script. The dates in the file are displayed in the format: Mar 2, 2011 9:09:31 PM I have tried using the date command %e and %l but it pads an extra space for the day and hour if they are single digits. So this I used a normal date command: ... (6 Replies)
Discussion started by: crazyideas
6 Replies

8. Shell Programming and Scripting

Delete character on specific position

Hi, im still new in unix. i want to ask how to delete character on specific position in line, lets say i want to remove 5 character from position 1000, so characters from position 1000-1005 will be deleted. i found this sed command can delete 4 characters from position 10, but i dont know if... (7 Replies)
Discussion started by: bluesue
7 Replies

9. Post Here to Contact Site Administrators and Moderators

Search for a pattern and replace a space at specific position with a Character in File

In file, we have millions of records each of 1000 in length. And at specific position say 800 there is a space, we need to replace it with Character X if the ID in that row starts with 123. So far i have used the below which is replacing space at that position to X but its not checking for... (3 Replies)
Discussion started by: Jagmeet Singh
3 Replies

10. Shell Programming and Scripting

Count specific character of a file in each line and delete this character in a specific position

I will appreciate if you help me here in this script in Solaris Enviroment. Scenario: i have 2 files : 1) /tmp/TRANSACTIONS_DAILY_20180730.txt: 201807300000000004 201807300000000005 201807300000000006 201807300000000007 201807300000000008 2)... (10 Replies)
Discussion started by: teokon90
10 Replies
MYSQLDBCOPY(1)							  MySQL Utilities						    MYSQLDBCOPY(1)

NAME
mysqldbcopy - Copy databases from one MySQL server to another SYNOPSIS
mysqldbcopy [options] db_name[:new_db_name] DESCRIPTION
This utility copies a database on a source server to a database on a destination server. If the source and destination servers are differ- ent, the database names can be the same or different. If the source and destination servers are the same, the database names must be dif- ferent. The utility accepts one or more database pairs on the command line. To name a database pair, use db_name:new_db_name syntax to specify the source and destination names explicitly. If the source and destination database names are the same, db_name can be used as shorthand for db_name:db_name. By default, the operation copies all objects (tables, views, triggers, events, procedures, functions, and database-level grants) and data to the destination server. There are options to turn off copying any or all of the objects as well as not copying the data. To exclude specific objects by name, use the --exclude option with a name in db.*obj* format, or you can supply a search pattern. For exam- ple, --exclude=db1.trig1 excludes the single trigger and --exclude=trig_ excludes all objects from all databases having a name that begins with trig and has a following character. By default, the utility creates each table on the destination server using the same storage engine as the original table. To override this and specify the storage engine to use for all tables created on the destination server, use the --new-storage-engine option. If the desti- nation server supports the new engine, all tables use that engine. To specify the storage engine to use for tables for which the destination server does not support the original storage engine on the source server, use the --default-storage-engine option. The --new-storage-engine option takes precedence over --default-storage-engine if both are given. If the --new-storage-engine or --default-storage-engine option is given and the destination server does not support the specified storage engine, a warning is issued and the server's default storage engine setting is used instead. By default, the operation uses a consistent snapshot to read the source databases. To change the locking mode, use the --locking option with a locking type value. Use a value of no-locks to turn off locking altogether or lock-all to use only table locks. The default value is snapshot. Additionally, the utility uses WRITE locks to lock the destination tables during the copy. You can include replication statements for copying data among a master and slave or between slaves. The --rpl option permits you to select from the following replication statements to include in the export. master Include the CHANGE MASTER statement to start a new slave with the current server acting as the master. This executes the appropriate STOP and START slave statements. The STOP SLAVE statement is executed at the start of the copy and the CHANGE MASTER followed by the START SLAVE statements are executed after the copy. slave Include the CHANGE MASTER statement to start a new slave using the current server's master information. This executes the appropri- ate STOP and START slave statements. The STOP SLAVE statement is executed at the start of the copy and the CHANGE MASTER followed by the START SLAVE statements follow the copy. To include the replication user in the CHANGE MASTER statement, use the --rpl-user option to specify the user and password. If this option is omitted, the utility attempts to identify the replication user. In the event that there are multiple candidates or the user requires a password, the utility aborts with an error. OPTIONS
mysqldbcopy accepts the following command-line options: --help Display a help message and exit. --default-storage-engine=<def_engine> The engine to use for tables if the destination server does not support the original storage engine on the source server. --destination=<destination> Connection information for the destination server in <user>[:<passwd>]@<host>[:<port>][:<socket>] format, where <passwd> is optional and either <port> or <socket> must be provided. --exclude=<exclude>, -x<exclude> Exclude one or more objects from the operation using either a specific name such as db1.t1 or a search pattern. Use this option multiple times to specify multiple exclusions. By default, patterns use LIKE matching. With the --regexp option, patterns use REG- EXP matching. This option does not apply to grants. --force Drop each database to be copied if exists before copying anything into it. Without this option, an error occurs if you attempt to copy objects into an existing database. --locking=<locking> Choose the lock type for the operation. Permitted lock values are no-locks (do not use any table locks), lock-all (use table locks but no transaction and no consistent read), and snaphot (consistent read using a single transaction). The default is snapshot. --new-storage-engine=<new_engine> The engine to use for all tables created on the destination server. --quiet, -q Turn off all messages for quiet execution. --regexp, --basic-regexp, -G Perform pattern matches using the REGEXP operator. The default is to use LIKE for matching. --rpl=<dump_option>, --replication=<dump_option> Include replication information. Permitted values are master (include the CHANGE MASTER statement using the source server as the master), slave (include the CHANGE MASTER statement using the destination server's master information), and both (include the master and slave options where applicable). --rpl-user=<user[:password]> The user and password for the replication user requirement - e.g. rpl:passwd - default = rpl:rpl. --skip=<objects> Specify objects to skip in the operation as a comma-separated list (no spaces). Permitted values are CREATE_DB, DATA, EVENTS, FUNC- TIONS, GRANTS, PROCEDURES, TABLES, TRIGGERS, and VIEWS. --source=<source> Connection information for the source server in <user>[:<passwd>]@<host>[:<port>][:<socket>] format, where <passwd> is optional and either <port> or <socket> must be provided. --threads Use multiple threads for cross-server copy. The default is 1. --verbose, -v Specify how much information to display. Use this option multiple times to increase the amount of information. For example, -v = verbose, -vv = more verbose, -vvv = debug. --version Display version information and exit. NOTES
You must provide connection parameters (user, host, password, and so forth) for an account that has the appropriate privileges to access all objects in the operation. To copy all objects from a source, the user must have these privileges: SELECT and SHOW VIEW for the database, and SELECT for the mysql database. To copy all objects to a destination, the user must have these privileges: CREATE for the database, SUPER (when binary logging is enabled) for procedures and functions, and GRANT OPTION to copy grants. Actual privileges required may differ from installation to installation depending on the security privileges present and whether the data- base contains certain objects such as views or events and whether binary logging is enabled. The --new-storage-engine and --default-storage-engine options apply to all destination tables in the operation. Some option combinations may result in errors during the operation. For example, eliminating tables but not views may result in an error a the view is copied. The --rpl option is not valid for copying databases on the same server. An error will be generated. EXAMPLES
The following example demonstrates how to use the utility to copy a database named util_test to a new database named util_test_copy on the same server: $ mysqldbcopy --source=root:pass@localhost:3310:/test123/mysql.sock --destination=root:pass@localhost:3310:/test123/mysql.sock util_test:util_test_copy # Source on localhost: ... connected. # Destination on localhost: ... connected. # Copying database util_test renamed as util_test_copy # Copying TABLE util_test.t1 # Copying table data. # Copying TABLE util_test.t2 # Copying table data. # Copying TABLE util_test.t3 # Copying table data. # Copying TABLE util_test.t4 # Copying table data. # Copying VIEW util_test.v1 # Copying TRIGGER util_test.trg # Copying PROCEDURE util_test.p1 # Copying FUNCTION util_test.f1 # Copying EVENT util_test.e1 # Copying GRANTS from util_test #...done. If the database to be copied does not contain only InnoDB tables and you want to ensure data integrity of the copied data by locking the tables during the read step, add a --locking=lock-all option to the command: $ mysqldbcopy --source=root:pass@localhost:3310:/test123/mysql.sock --destination=root:pass@localhost:3310:/test123/mysql.sock util_test:util_test_copy --locking=lock-all # Source on localhost: ... connected. # Destination on localhost: ... connected. # Copying database util_test renamed as util_test_copy # Copying TABLE util_test.t1 # Copying table data. # Copying TABLE util_test.t2 # Copying table data. # Copying TABLE util_test.t3 # Copying table data. # Copying TABLE util_test.t4 # Copying table data. # Copying VIEW util_test.v1 # Copying TRIGGER util_test.trg # Copying PROCEDURE util_test.p1 # Copying FUNCTION util_test.f1 # Copying EVENT util_test.e1 # Copying GRANTS from util_test #...done. To copy one or more databases from a master to a slave, you can use the following command to copy the databases. Use the master as the source and the slave as the destination: $ mysqldbcopy --source=root@localhost:3310 --destination=root@localhost:3311 test123 --rpl=master --rpl-user=rpl # Source on localhost: ... connected. # Destination on localhost: ... connected. # Source on localhost: ... connected. # Stopping slave # Copying database test123 # Copying TABLE test123.t1 # Copying data for TABLE test123.t1 # Connecting to the current server as master # Starting slave #...done. To copy a database from one slave to another attached to the same master, you can use the following command using the slave with the data- base to be copied as the source and the slave where the database needs to copied to as the destination: $ mysqldbcopy --source=root@localhost:3311 --destination=root@localhost:3312 test123 --rpl=slave --rpl-user=rpl # Source on localhost: ... connected. # Destination on localhost: ... connected. # Source on localhost: ... connected. # Stopping slave # Copying database test123 # Copying TABLE test123.t1 # Copying data for TABLE test123.t1 # Connecting to the current server's master # Starting slave #...done. COPYRIGHT
Copyright (c) 2010, 2012, Oracle and/or its affiliates. All rights reserved. This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; version 2 of the License. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MER- CHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program; if not, write to the Free Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA AUTHOR
MySQL Utilities Team COPYRIGHT
2010, Oracle and/or its affiliates. All rights reserved. 1.0.3 May 09, 2012 MYSQLDBCOPY(1)
All times are GMT -4. The time now is 11:56 AM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy