Sponsored Content
Top Forums UNIX for Dummies Questions & Answers Shell script for search and replace by field Post 302707765 by chandrath on Sunday 30th of September 2012 12:18:40 AM
Old 09-30-2012
Shell script for search and replace by field

Hi,
I have an input file with below data and rules file to apply search and replace by each field in the input based on exact value or pattern.
Could you please help me with unix script to read input file and rules file and then create the output and reject files based on the rules file.
Input file:
Code:
field1  field2
rc11    rc12
rc$21  rc#22
XX31   yy32
rc41    r!42

rules file:
Code:
field   search   condition   replace
field1 XX         Equals      rc
field1  $          contains   
field2  #          contains
field2  !           contauns  c
field2 yy          equals      rc

Output
Code:
field1 field2
rc11   rc12
rc21   rc22
rc31  rc32
rc41   rc42

reject file:
Code:
field1 field2
rc$21 rc#22
XX31 yy32
rc41 r!42

Appreciate your help with this.

Thank you!

Last edited by Scrutinizer; 09-30-2012 at 04:48 PM.. Reason: code tags
 

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

search and replace dynamic data in a shell script

Hi, I have a file that looks something like this: ... 0,6,256,87,0,0,0,1187443420 0,6,438,37,0,0,0,1187443380 0,2,0,0,0,10,0,1197140320 0,3,0,0,0,10,0,1197140875 0,2,0,0,0,23,0,1197140332 0,3,0,0,0,23,0,1197140437 0,2,0,0,0,17,0,1197140447 0,3,0,0,0,17,0,1197140543... (8 Replies)
Discussion started by: csejl
8 Replies

2. Shell Programming and Scripting

awk search and replace field

I am writing a c++ program that has many calls of pow(input,2). I now realize that this is slowing down the program and these all should be input * input for greater speed. There should be a simple way of doing this replacement throughout my file with awk, but I am not very familiar with awk.... (2 Replies)
Discussion started by: bluejayek
2 Replies

3. Shell Programming and Scripting

Perl - search and replace a particular field

Hi, I have a file having around 30 records. Each record has 5 fields delimited by PIPE. Few records in the file having Junk characters in the field2 and field4. I found the junk charcter and I tested it and replace the junk with space with the command below perl -i -p -e "s/\x00/ /g"... (1 Reply)
Discussion started by: ramkrix
1 Replies

4. Shell Programming and Scripting

Search duplicate field and replace one of them with new value

Dear All, I have file with 4 columns: 1 AA 0 21 2 BB 0 31 3 AA 0 21 4 CC 0 41 I would like to find the duplicate record based on column 2 and replace the 4th column of the duplicate by a new value. So, the output will be: 1 AA 0 21 2 BB 0 31 3 AA 0 -21 4 CC 0 41 Any suggestions... (3 Replies)
Discussion started by: ezhil01
3 Replies

5. Shell Programming and Scripting

Search a string,get line and replace with second field

Hi, I need to search for source path in file2 , as per file1 and if found get the next line and take the field value and put it in URL value of file1. In file1, NF is not same for all the lines. file1: <type source="/home/USER/Desktop" Dest="/home/USER/DIR1/Desktop" URL="ssh/path"/> <type... (8 Replies)
Discussion started by: greet_sed
8 Replies

6. Shell Programming and Scripting

Search and replace field?

I have 2 files A.txt and B.txt A.txt 3 fields and separate by a comma some,thing,florida any1,thing1,california some2,thing2,dallas just,fun,kansas B.txt has 8 fields and separate by a comma what,ever,florida-state,,,,,, some,one,dallas_state,,,,,, You will see 3rd fields are the... (5 Replies)
Discussion started by: sabercats
5 Replies

7. Shell Programming and Scripting

awk search and replace in a targeted field instead of $0

Hi I would like to apply this gawk command: gawk '{$0=gensub(/\y+\y/,"","g"); print}' file not to the whole $0 but just to the part of $0 that is between: (a number)"> and </mrk> Is it possible? thanks for your help. (4 Replies)
Discussion started by: louisJ
4 Replies

8. Shell Programming and Scripting

Search field in text file and replace value

Hi there, First of all this is my first post here. Thank you in advance for your help. What I am trying to do is the following. I have a text file where each field of each row is separated by a tabulator. Looks like this: ATOM 1 N HSE A 26 3.033 -10.429 -2.262 1.00 17.07 ... (8 Replies)
Discussion started by: doom4
8 Replies

9. Shell Programming and Scripting

Search for a value and replace other field in the same set

Hello friends, I have huge file with many sets where each "set" has few lines and each set always begins with "Set" in Sq brackets as shown above. # cat file1 (2 Replies)
Discussion started by: magnus29
2 Replies

10. UNIX for Dummies Questions & Answers

Search and replace the last field

Hi All, Seeking for your assistance on how to search and replace the last field/column. please see sample below: inputfile1.csv ="8923523434",="543623534"="afd23535623",="100"="200" ="8923523431",="543623536"="afd23535626",="101"="201"... (3 Replies)
Discussion started by: poginiks
3 Replies
SORT(1) 						    BSD General Commands Manual 						   SORT(1)

NAME
sort -- sort or merge text files SYNOPSIS
sort [-bcdfHilmnrSsu] [-k field1[,field2]] [-o output] [-R char] [-T dir] [-t char] [file ...] DESCRIPTION
The sort utility sorts text files by lines. Comparisons are based on one or more sort keys extracted from each line of input, and are per- formed lexicographically. By default, if keys are not given, sort regards each input line as a single field. The following options are available: -c Check that the single input file is sorted. If the file is not sorted, sort produces the appropriate error messages and exits with code 1; otherwise, sort returns 0. sort -c produces no output. -H Ignored for compatibility with earlier versions of sort. -m Merge only; the input files are assumed to be pre-sorted. -o output The argument given is the name of an output file to be used instead of the standard output. This file can be the same as one of the input files. -S Don't use stable sort. Default is to use stable sort. -s Use stable sort, keeps records with equal keys in their original order. This is the default. Provided for compatibility with other sort implementations only. -T dir Use dir as the directory for temporary files. The default is the value specified in the environment variable TMPDIR or /tmp if TMPDIR is not defined. -u Unique: suppress all but one in each set of lines having equal keys. If used with the -c option, check that there are no lines with duplicate keys. The following options override the default ordering rules. When ordering options appear independent of key field specifications, the requested field ordering rules are applied globally to all sort keys. When attached to a specific key (see -k), the ordering options over- ride all global ordering options for that key. -d Only blank space and alphanumeric characters are used in making comparisons. -f Considers all lowercase characters that have uppercase equivalents to be the same for purposes of comparison. -i Ignore all non-printable characters. -l Sort by the string length of the field, not by the field itself. -n An initial numeric string, consisting of optional blank space, optional minus sign, and zero or more digits (including decimal point) is sorted by arithmetic value. (The -n option no longer implies the -b option.) -r Reverse the sense of comparisons. The treatment of field separators can be altered using these options: -b Ignores leading blank space when determining the start and end of a restricted sort key. A -b option specified before the first -k option applies globally to all -k options. Otherwise, the -b option can be attached independently to each field argument of the -k option (see below). Note that the -b option has no effect unless key fields are specified. -t char char is used as the field separator character. The initial char is not considered to be part of a field when determining key offsets (see below). Each occurrence of char is significant (for example, ``charchar'' delimits an empty field). If -t is not specified, the default field separator is a sequence of blank-space characters, and consecutive blank spaces do not delimit an empty field; further, the initial blank space is considered part of a field when determining key offsets. -R char char is used as the record separator character. This should be used with discretion; -R <alphanumeric> usually produces undesir- able results. The default record separator is newline. -k field1[,field2] Designates the starting position, field1, and optional ending position, field2, of a key field. The -k option replaces the obso- lescent options +pos1 and -pos2. The following operands are available: file The pathname of a file to be sorted, merged, or checked. If no file operands are specified, or if a file operand is -, the standard input is used. A field is defined as a minimal sequence of characters followed by a field separator or a newline character. By default, the first blank space of a sequence of blank spaces acts as the field separator. All blank spaces in a sequence of blank spaces are considered as part of the next field; for example, all blank spaces at the beginning of a line are considered to be part of the first field. Fields are specified by the -k field1[,field2] argument. A missing field2 argument defaults to the end of a line. The arguments field1 and field2 have the form m.n and can be followed by one or more of the letters b, d, f, i, l, n, and r, which correspond to the options discussed above. A field1 position specified by m.n (m, n > 0) is interpreted as the nth character in the mth field. A miss- ing .n in field1 means '.1', indicating the first character of the mth field; if the -b option is in effect, n is counted from the first non- blank character in the mth field; m.1b refers to the first non-blank character in the mth field. A field2 position specified by m.n is interpreted as the nth character (including separators) of the mth field. A missing .n indicates the last character of the mth field; m = 0 designates the end of a line. Thus the option -k v.x,w.y is synonymous with the obsolescent option +v-1.x-1-w-1.y; when y is omitted, -k v.x,w is synonymous with +v-1.x-1-w+1.0. The obsolescent +pos1 -pos2 option is still supported, except for -w.0b, which has no -k equivalent. ENVIRONMENT
If the following environment variable exists, it is used by sort. TMPDIR sort uses the contents of the TMPDIR environment variable as the path in which to store temporary files. FILES
/tmp/sort.* Default temporary files. outputNUMBER Temporary file which is used for output if output already exists. Once sorting is finished, this file replaces output (via link(2) and unlink(2)). EXIT STATUS
Sort exits with one of the following values: 0 Normal behavior. 1 On disorder (or non-uniqueness) with the -c option 2 An error occurred. SEE ALSO
comm(1), join(1), uniq(1), qsort(3), radixsort(3) HISTORY
A sort command appeared in Version 5 AT&T UNIX. This sort implementation appeared in 4.4BSD and is used since NetBSD 1.6. BUGS
Posix requires the locale's thousands separator be ignored in numbers. It may be faster to sort very large files in pieces and then explic- itly merge them. NOTES
This sort has no limits on input line length (other than imposed by available memory) or any restrictions on bytes allowed within lines. To protect data sort -o calls link(2) and unlink(2), and thus fails on protected directories. Input files should be text files. If file doesn't end with record separator (which is typically newline), the sort utility silently supplies one. The current sort uses lexicographic radix sorting, which requires that sort keys be kept in memory (as opposed to previous versions which used quick and merge sorts and did not.) Thus performance depends highly on efficient choice of sort keys, and the -b option and the field2 argument of the -k option should be used whenever possible. Similarly, sort -k1f is equivalent to sort -f and may take twice as long. BSD
December 18, 2010 BSD
All times are GMT -4. The time now is 10:28 AM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy