replace a column in a file if it matches certain pattern


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting replace a column in a file if it matches certain pattern
# 1  
Old 03-11-2009
replace a column in a file if it matches certain pattern

Hi,

I want to replace a column in a file if it matches certain pattern. Can you help me on this.

Here is the file content.

000000 1111111 2222222
011111 0123445 1234556
023445 1111111 2343455

if second column contains 1111111 i need to replace it with 0000000

Can you help me on this.
# 2  
Old 03-11-2009
Code:
nawk -v col=2 -v val=1111111 -v valN=0000000 '$col == val {$col = valN}1' myFile


Last edited by vgersh99; 03-11-2009 at 07:49 PM.. Reason: oooops - typo
# 3  
Old 03-11-2009
Its erroring out.

Code:
$ cat test
000000 1111111 2222222
011111 0123445 1234556
023445 1111111 234345
$ nawk -v col=2 -v val=1111111 -v valN=0000000 '$col == val {$cal = valN}1' test
nawk: illegal field $()
 input record number 1, file test
 source line number 1
$

# 4  
Old 03-11-2009
Okie i got it. its working now.there was a typo error $cal it should be $col. Can you explain me the command please. Because i have lot of files like this. Want to do different things.
# 5  
Old 03-11-2009
Ooops - my bad - sorry 'bout that - fixed the post.
Code:
nawk -v col=2 -v val=1111111 -v valN=0000000 '$col == val {$col = valN}1' myFile

'-v col=2' - we're assigning awk's variable 'col' a value of '2' - this is your COLUMN designator.

'-v val=1111111' - we're assigning awk's variable 'val' a value of '1111111 ' - this is your OLD value.

'-v valN=0000000' - we're assigning awk's variable 'valN' a value of '0000000' - this is your NEW value.

Code:
# if the VALUE of the field/column referenced by var 'col' equals the value of
# variable 'val' (or OLD value), then assign the value of variable 'valN'
# to the column/field referenced by variable 'col'.
#
'$col == val {$col = valN}1'

# 6  
Old 03-11-2009
Thanks..That worked great..Awesome.
# 7  
Old 03-12-2009
Here is also a KornShell version, which is longer than the proposed solution,
however it works with an arbitrary number of columns and you don't need to specify yourself the number of column:

Code:
#!/bin/ksh

IFS=" \t\n"

current_result=""
final_result=""

while read current_line
do
    
    current_result=""
    
    for current_token in $current_line
    do
        if [[ $current_token = "1111111"  ]]
        then
            current_result="$current_result 0000000"
        else
            current_result="$current_result $current_token"
        fi
    done
    
    current_result="${current_result# }"
    
    final_result="$final_result$current_result\n"
    
done < $1

final_result="${final_result%\\n}"

exec 4> $1
print -u4 "$final_result"

1 <&-
4 >&-

Where $1 is the name of your file in which you want to replace "11111111".

Regards,
Smilie
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

If pattern in column 3 matches pattern in column 2 (any row), print value in column 1

Hi all, I have searched and searched, but I have not found a solution that quite fits what I am trying to do. I have a long list of data in three columns. Below is a sample: 1,10,8 2,12,10 3,13,12 4,14,14 5,15,16 6,16,18 Please use code tags What I need to do is as follows: If a... (4 Replies)
Discussion started by: bleedingturnip
4 Replies

2. Shell Programming and Scripting

Extract all the sentences from a text file that matches a pattern list

Hi I have a big text file. I want to extract all the sentences that matches at least 70% (seventy percent) of the words from each sentence based on a word list called A. Say the format of the text file is as given below: This is the first sentence which consists of fifteen words... (4 Replies)
Discussion started by: my_Perl
4 Replies

3. Shell Programming and Scripting

Remove entire line from a file if 1st column matches a pattern

I have one requirement to delete all lines from a file if it matches below scenario. File contains three column. Employee Number, Employee Name and Employee ID Scenario is: delete all line if Employee Number (1st column) contains below 1. Non-numeric Employee Number 2. Employee Number that... (3 Replies)
Discussion started by: anshu ranjan
3 Replies

4. Shell Programming and Scripting

CSV file REPLACE COLUMN if it matches

I have a file cat 1.txt AAAA , BBBB , CCCC , DDDD DFDF , DFDF , DFDF , FDDD AA11 , DFDF , 0000 , UTIO ADSD , WERT, 0000 , JKJL If the 3rd column is not equal to "0000" , then it should replace "0000" with "XXXX" and if its equal to "0000" then print the line as it is. need help. (9 Replies)
Discussion started by: aravindj80
9 Replies

5. Shell Programming and Scripting

Replace column that matches specific pattern, with column data from another file

Can anyone please help with this? I have 2 files as given below. If 2nd column of file1 has pattern foo1@a, find the matching 1st column in file2 & replace 2nd column of file1 with file2's value. file1 abc_1 foo1@a .... abc_1 soo2@a ... def_2 soo2@a .... def_2 foo1@a ........ (7 Replies)
Discussion started by: prashali
7 Replies

6. Shell Programming and Scripting

String replacement when particular pattern matches in a file

I have a file file123.xml which looks like this xmlEntry="username"="josh" <property="never_back_down"> phone="<178652>" apn=property:address="wonderland" xmlEntry="username"="jessica" <property="never_back_down"> phone="<178653>" apn=property:address="wonderland"... (5 Replies)
Discussion started by: poga
5 Replies

7. Shell Programming and Scripting

Merge lines from one file if pattern matches

I have one comma separated file (a.txt) with two or more records all matching except for the last column. I would like to merge all matching lines into one and consolidate the last column, separated by ":". Does anyone know of a way to do this easily? I've searched the forum but most talked... (6 Replies)
Discussion started by: giannicello
6 Replies

8. Shell Programming and Scripting

get value that matches file name pattern

Hi I have files with names that contain the date in several formats as, YYYYMMDD, DD-MM-YY,DD.MM.YY or similar combinations. I know if a file fits in one pattern or other, but i donīt know how to extract the substring contained in the file that matches the pattern. For example, i know that ... (1 Reply)
Discussion started by: pjrm
1 Replies

9. Shell Programming and Scripting

Displaying lines of a file where the second field matches a pattern

Howdy. I know this is most likely possible using sed or awk or grep, most likely a combination of them together, but how would one go about running a grep like command on a file where you only try to match your pattern to the second field in a line, space delimited? Example: You are... (3 Replies)
Discussion started by: LordJezoX
3 Replies

10. Shell Programming and Scripting

Replace if regex on specific column matches expression?

I am attempting to convert rewrite rules to Nginx, and since due to the mass amount of rewrites we must convert, I've been trying to write a script to help me on a specific part, easily. So far I have this: rewrite ^action/static/(+)/$ staticPage.php?pg=$1&%$query_string; What I want done... (5 Replies)
Discussion started by: EXT3FSCK
5 Replies
Login or Register to Ask a Question