CSV file REPLACE COLUMN if it matches


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting CSV file REPLACE COLUMN if it matches
# 1  
Old 09-01-2014
CSV file REPLACE COLUMN if it matches

I have a file
Code:
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.

Last edited by Franklin52; 09-01-2014 at 10:54 AM.. Reason: Please use code tags
# 2  
Old 09-01-2014
Hello Aravindj80,

Could you please use code tags for commands and codes which you are using in your post. This is one of the very important rule for this forum which you have agreed during your user account creation. you can refer the forum rule guidlines also for same as follows.
https://www.unix.com/misc.php?do=cfrules

Following may help you in your requirement.

Code:
awk -F"," '($3 !~ 0) {gsub(/.*/," XXXX ",$3);} 1' OFS=","   filename

Output will be as follows.

Code:
AAAA , BBBB , XXXX , DDDD
DFDF , DFDF , XXXX , FDDD
AA11 , DFDF , 0000 , UTIO
ADSD , WERT, 0000 , JKJL

Thanks,
R. Singh

Last edited by RavinderSingh13; 09-01-2014 at 09:42 AM.. Reason: Added rules link for user
# 3  
Old 09-01-2014
I tried it , but all the rows are getting replaced with XXXX
# 4  
Old 09-01-2014
Code:
awk -F '[ ]*,[ ]*' '$3 != "0000" {$3="XXXX"}1' OFS=, file

If you want spaces before and after ,
Code:
awk -F '[ ]*,[ ]*' '$3 != "0000" {$3="XXXX"}1' OFS=' , ' file


Last edited by SriniShoo; 09-01-2014 at 10:03 AM.. Reason: Corrected solution
# 5  
Old 09-01-2014
Hi Aravind,

Code which I have given is working fine for me, could you please show us which output you are getting along with your OS details and please use code tags while using commands/codes.

Thanks,
R. Singh
# 6  
Old 09-01-2014
Thanks it worked great , if i want to start the replacement from the 2nd line as first line has header , how it is possible ?
# 7  
Old 09-01-2014
Hi Aravind,

If you are referring to my solution then following may help.

Code:
awk -F"," ' NR>1 && ($3 !~ 0) {gsub(/.*/," XXXX ",$3);} 1' OFS=","   filename

if you are reffering to Srini's solution then kindly add NR>1 in the starting of the solution.


Thanks,
R. Singh

Last edited by RavinderSingh13; 09-02-2014 at 02:36 AM.. Reason: Adding && to solution, Thanks to Rudi C
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Replace all string matches in file with unique random number

Hello Take this file... Test01 Ref test Version 01 Test02 Ref test Version 02 Test66 Ref test Version 66 Test99 Ref test Version 99 I want to substitute every occurrence of Test{2} with a unique random number, so for example, if I was using sed, substitution would be something... (1 Reply)
Discussion started by: funkman
1 Replies

2. Shell Programming and Scripting

Replace Double quotes within double quotes in a column with space while loading a CSV file

Hi All, I'm unable to load the data using sql loader where there are double quotes within the double quotes As these are optionally enclosed by double quotes. Sample Data : "221100",138.00,"D","0019/1477","44012075","49938","49938/15043000","Television - 22" Refurbished - Airwave","Supply... (6 Replies)
Discussion started by: mlavanya
6 Replies

3. Shell Programming and Scripting

Compare 2 files of csv file and match column data and create a new csv file of them

Hi, I am newbie in shell script. I need your help to solve my problem. Firstly, I have 2 files of csv and i want to compare of the contents then the output will be written in a new csv file. File1: SourceFile,DateTimeOriginal /home/intannf/foto/IMG_0713.JPG,2015:02:17 11:14:07... (8 Replies)
Discussion started by: refrain
8 Replies

4. Shell Programming and Scripting

Difference of the same column when two other column matches and one column differs less than 1 hour

This is my input file : # cat list 20130430121600, cucm, location,76,2 20130430121600,cucm1,location1,76,4 20130430122000,cucm,location,80,8 20130430122000,cucm1,location1,90,8 20130430140000,cucm1,location1,87,11 20130430140000, cucm,location,67,9 This is the required output ... (1 Reply)
Discussion started by: Lakshmikumari
1 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

Replace 2nd column for each line in a csv file with fixed string+random number

Hi experts, My csv file looks like this U;cake;michael;temp;;;; U;bread;john;temp;;;; U;cocktails;sarah;temp;;;; I'd like to change the value fo 2nd column to cf+random number , which will look maybe something like this U;cf20187;michael;temp;;;; U;cf8926;john;temp;;;;... (7 Replies)
Discussion started by: tententen
7 Replies

7. Shell Programming and Scripting

Replace 2nd column of CSV file with numbers on line

I have a csv file with occasional multiple entries in the second column. 111111,104,07-24-2011,3.15,N, 222222,020 140,07-24-2011,10.00,N,I want the result 111111,104,07-24-2011,3.15,N, 222222,020,07-24-2011,10.00,N, 222222,140,07-24-2011,10.00,N, I know I can get the output of the second... (5 Replies)
Discussion started by: ffdstanley
5 Replies

8. Shell Programming and Scripting

Match column 3 in file1 to column 1 in file 2 and replace with column 2 from file2

Match column 3 in file1 to column 1 in file 2 and replace with column 2 from file2 file 1 sample SNDK 80004C101 AT XLNX 983919101 BB NETL 64118B100 BS AMD 007903107 CC KLAC 482480100 DC TER 880770102 KATS ATHR 04743P108 KATS... (7 Replies)
Discussion started by: rydz00
7 Replies

9. Shell Programming and Scripting

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... (6 Replies)
Discussion started by: Krrishv
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