LinuxQuestions.org
Review your favorite Linux distribution.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 02-20-2021, 10:08 AM   #1
karamunix
LQ Newbie
 
Registered: Feb 2021
Posts: 1

Rep: Reputation: Disabled
Replace first 0 by 1 in specific column


Hi Everyone,

I'm trying to replace in same file only the first 0 found in the fourth column by 1.

And to keep remaining 0 under the fourth column as is without any change.

To mention that columns are separated by a comma.

0,0,0,1
0,0,0,4
0,0,0,0
0,0,0,0
0,0,1,0
0,0,0,5
0,0,0,3
0,0,0,0
0,0,2,2


Desired output should be:
-------------------------

0,0,0,1
0,0,0,4
0,0,0,1 --> Here the 0 in the third row for the fourth column was replaced by 1.
0,0,0,0 --> Here the 0 should not be changed.
0,0,1,0
0,0,0,5
0,0,0,3
0,0,0,0
0,0,2,2


If we run the script another time:
----------------------------------
Desired output should be:
-------------------------

0,0,0,1
0,0,0,4
0,0,0,1
0,0,0,1 --> Here the 0 was replaced by 1 this time and so on.
0,0,1,0
0,0,0,5
0,0,0,3
0,0,0,0
0,0,2,2

Any help would be very appreciated.

Thanks in advance.
 
Old 02-20-2021, 10:14 AM   #2
Turbocapitalist
LQ Guru
 
Registered: Apr 2005
Distribution: Linux Mint, Devuan, OpenBSD
Posts: 7,328
Blog Entries: 3

Rep: Reputation: 3726Reputation: 3726Reputation: 3726Reputation: 3726Reputation: 3726Reputation: 3726Reputation: 3726Reputation: 3726Reputation: 3726Reputation: 3726Reputation: 3726
Welcome. This looks like an easy task for AWK or perl. In either case, just set the input field separator to a comma. See -F for FS in AWK, or -a for autosplit in perl. (Edit: in AWK, the variables FS and OFS might help also.)

What have you tried so far, and where are you stuck? Which distro is this for?

Last edited by Turbocapitalist; 02-20-2021 at 10:26 AM.
 
1 members found this post helpful.
Old 02-20-2021, 12:52 PM   #3
computersavvy
Senior Member
 
Registered: Aug 2016
Posts: 3,345

Rep: Reputation: 1484Reputation: 1484Reputation: 1484Reputation: 1484Reputation: 1484Reputation: 1484Reputation: 1484Reputation: 1484Reputation: 1484Reputation: 1484
Another candidate to solve this would be sed.
 
Old 02-20-2021, 01:01 PM   #4
shruggy
Senior Member
 
Registered: Mar 2020
Posts: 3,670

Rep: Reputation: Disabled
@computersavvy. Yes, but changing an arbitrary field with sed is not as convenient as with awk. Unless like in the example posted by OP this happens to be the last field in the line: then doing it with sed would be quite easy indeed.
 
Old 02-20-2021, 04:51 PM   #5
JeremyBoden
Senior Member
 
Registered: Nov 2011
Location: London, UK
Distribution: Debian
Posts: 1,947

Rep: Reputation: 511Reputation: 511Reputation: 511Reputation: 511Reputation: 511Reputation: 511
Who invents these artificial "problems"?
 
Old 02-21-2021, 07:31 AM   #6
MadeInGermany
Senior Member
 
Registered: Dec 2011
Location: Simplicity
Posts: 2,806

Rep: Reputation: 1207Reputation: 1207Reputation: 1207Reputation: 1207Reputation: 1207Reputation: 1207Reputation: 1207Reputation: 1207Reputation: 1207
If it's always the last field then it's easy (though not trivial) with GNU sed:
Code:
sed -i '0,/,0$/ s//,1/' filename
But if you want to handle any given field, then it's nearly impossible with sed.
With perl (as suggested, the -F option splits to @F):
Code:
perl -i -F"," -le 'BEGIN { $f=3; $,=","; $d=0; } $F[$f] eq "0" and !$d++ and $F[$f]="1"; print @F;' filename
The same with awk; lacking a -i option, its output goes to a temporary file:
Code:
file=filename
awk -F"," 'BEGIN { f=4; OFS="," } { $f == "0" && !d++ && $f="1"; print }' "$file" >"$file".new &&
mv "$file".new "$file"

Last edited by MadeInGermany; 02-21-2021 at 07:50 AM.
 
Old 02-21-2021, 08:48 AM   #7
shruggy
Senior Member
 
Registered: Mar 2020
Posts: 3,670

Rep: Reputation: Disabled
Starting with version 4.1.0, GNU awk even provides the in-place file editing functionality:
Code:
gawk -F, -vOFS=, -vf=4 -i inplace '!d&&!$f{$f=d=1}1' filename

Last edited by shruggy; 02-21-2021 at 08:59 AM.
 
1 members found this post helpful.
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
compare second column of a file then print the first column of it in a ne fil if true java_girl Linux - Newbie 2 03-16-2012 04:50 AM
how to sort the 2nd column on the basis of first column without repeating the value ? zediok Linux - Newbie 15 12-20-2011 11:48 AM
[SOLVED] Change to capital first letter of every word over specific column cgcamal Programming 10 05-02-2010 07:22 AM
[SOLVED] Replace pattern in specific lines and column with AWK cgcamal Programming 10 04-26-2010 01:11 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 06:14 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration