Bash Replace value in specific column


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Bash Replace value in specific column
# 1  
Old 06-25-2012
Bash Replace value in specific column

Hi all,

I have two files with the following format:

file1
Code:
BBB;33
AAA;2
CCC;5

file2
Code:
5;.;.;.
33;.;.;.

The first file contain a list of code and numbers. The second file only the number.
I would like to replace the corresponding code in the first column of the file1 with the corresponding value in column1 (and just in column1) of file2 so to have:

output
Code:
CCC;.;.;.
BBB;.;.;.

Hope this is clear,
Please notice that not all of the code are contained as number in file two and also the two file are not in order.

Thanks for suggestions.

Last edited by Scrutinizer; 06-25-2012 at 04:49 PM.. Reason: code tags
# 2  
Old 06-25-2012
Code:
awk 'NR==FNR { A[$2]=$1; next }; $1 in A { $1=A[$1] } 1' FS=";" OFS=";" file1 file2 > file3

This User Gave Thanks to Corona688 For This Post:
# 3  
Old 06-26-2012
Thanks. Works really well. Could you please explain a little bit how it works?
Thanks.
# 4  
Old 06-26-2012
Code:
awk '
# NR is the total number of lines processed so far
# FNR is the line number in the present file
# When they are the same, that means awk is reading the first file.
# So, when reading the first file, load elements into A like
# A["33"]="BBB" for easy lookup later.
# $1 and $2 are the first and second columns in the line, respectively.
NR==FNR { A[$2]=$1; next }

# If the first column is found in A's array indexes, then
# set the first column to the data for A[$1].
$1 in A { $1=A[$1] }

# Print every line ( Prints whenever the statement is true, and 1 is always true)
#
# FS is the input column separator, OFS is the output column separator.
# Any number of variables can be specified before files by putting VARNAME=value.
#
# After that comes the list filenames to process in order.
1'  FS=";" OFS=";" file1 file2 > file3

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Replace specific column delimiter

Hi All, I have a file with a pipe delimiter. I need to replace the delimiter with html tags. I managed to get all the delimiters replaced along with first and last but the requirement is that I need to change 7th delimiter with slight change. File1: ... (2 Replies)
Discussion started by: shash
2 Replies

2. Shell Programming and Scripting

Search Replace Specific Column using RegEx

Have Pipe Delimited File: > BRYAN BAKER|4/4/2015|518 VIRGINIA AVE|TEST > JOE BAXTER|3/30/2015|2233 MockingBird RD|ROW2On 3rd column where the address is located, I want to add a space after every numeric value - basically doing a "s//&\ / ": > BRYAN BAKER|4/4/2015|5 1 8 VIRGINIA AVE|TEST > JOE... (5 Replies)
Discussion started by: svn
5 Replies

3. Shell Programming and Scripting

Replace a specific column with a specific value

Hi, I am looking to replacing value of a specific column of /etc/pam.d/system-auth file. My file looks like this password sufficient pam_unix.so md5 shadow nullok try_first_pass use_authtok expected result password sufficient pam_unix.so md5 shadow nullok try_first_pass use_authtok... (5 Replies)
Discussion started by: Litu1988
5 Replies

4. Shell Programming and Scripting

How to replace a character in a specific column in a file?

This is a file that I have test line 1 (55) ) test line 2 (45) ) I would like to change all the parens in position 1 of this file to a ); i only want to check position 1 in every line of the file. I have tried different varations of sed, but cannot seem to be able to limit it to... (1 Reply)
Discussion started by: JoeG
1 Replies

5. Shell Programming and Scripting

Help with replace specific column command

Input file: ASD_QAW 12 A_@ AE_AQ 21 PA_123 ASDA_@ 23 ADA_AS . . Output file: ASD_QAW 12 A @ AE_AQ 21 PA 123 ASDA_@ 23 ADA AS . . Do anybody know how to just specific and replace "_" in column 3 with tab delimiter (\t)? Thanks for advice. (2 Replies)
Discussion started by: perl_beginner
2 Replies

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

7. Shell Programming and Scripting

Help with replace column data with specific word

Input file: Populus_trichocarpa 30 0 50 0 0 US Vitis_vinifera 1 18 2 8 6 US Populus_trichocarpa 1 5 100 0 0 US Arabidopsis_lyrata_subsp._lyrata 0 90 0 0 0 US Glycine_max 0 2 3 0 70 UK Desired output file: Populus_trichocarpa YES NO YES NO NO US Vitis_vinifera YES YES YES YES YES US... (4 Replies)
Discussion started by: perl_beginner
4 Replies

8. UNIX for Dummies Questions & Answers

Use sed to replace but only in a specific column of the text file

Hi, I would like to use sed to replace NA to x ('s/NA/x/g'), but only in the 5th column of the space delimited text file, nowhere else. How do I go about doing that? Thanks! (1 Reply)
Discussion started by: evelibertine
1 Replies

9. Shell Programming and Scripting

How to replace a specific word in specific column?

Hi My orginal file is like (100s of lines) id host ip location remarks 1 host1 ip1 - xxx 2 host2 ip2 - xxx 3 host3 ip3 - xxx -- -- 9 host9 ip9 - xxx I have a ref file like host1 location1 host2 location2 host3 location3 -- --... (6 Replies)
Discussion started by: ./hari.sh
6 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