LinuxQuestions.org
Share your knowledge at the LQ Wiki.
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 05-30-2012, 12:56 PM   #1
iconig
LQ Newbie
 
Registered: May 2012
Posts: 28

Rep: Reputation: Disabled
Substituting zeros with dots in 2nd and 3rd field of a text file.


Say I have this data ped.rc with fields;
1 0 0
2 0 0
3 0 0
4 0 0
5 0 0
6 0 0
7 0 0
8 0 0
9 0 0
10 0 0
11 0 0
12 14 3
13 0 0
14 17 9

I am trying to replace the 2nd and 3rd fields that have only 0's with dots but leave others that are non zeros the way they are. Can anyone help me out. Thanks
 
Old 05-30-2012, 01:36 PM   #2
Snark1994
Senior Member
 
Registered: Sep 2010
Location: Wales, UK
Distribution: Arch
Posts: 1,632
Blog Entries: 3

Rep: Reputation: 345Reputation: 345Reputation: 345Reputation: 345
Right forum, wrong thread title use a name that's descriptive of the problem. However, something like:

Code:
awk '{ if($2 == 0 && $3 ==0) { gsub( "0 0$", ". .", $0); } print }' ped.rc
should work.

Last edited by Snark1994; 05-30-2012 at 01:37 PM. Reason: Stupid code mistake
 
Old 05-30-2012, 01:49 PM   #3
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,252

Rep: Reputation: 2685Reputation: 2685Reputation: 2685Reputation: 2685Reputation: 2685Reputation: 2685Reputation: 2685Reputation: 2685Reputation: 2685Reputation: 2685Reputation: 2685
Do both the second and third have to be zero for a replacement or is it replace a zero in those fields no matter if both or one?
Here is an alternative assuming not both:
Code:
awk '{$2 = $2 == 0?".":$2;$3 = $3 == 0?".":$3}1' ped.rc
 
1 members found this post helpful.
Old 05-30-2012, 01:56 PM   #4
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976
sed?
Code:
sed -r 's/^([0-9]+) 0 0$/\1 . ./' file


Moderator note: please use a descriptive title for your thread excluding words like 'urgent', 'help' or 'hello'. Using a proper title makes it easier for members to help you. This thread has been reported for title modification. Please do not add replies that address the thread title.

Last edited by colucix; 05-30-2012 at 02:22 PM.
 
Old 05-30-2012, 02:38 PM   #5
Snark1994
Senior Member
 
Registered: Sep 2010
Location: Wales, UK
Distribution: Arch
Posts: 1,632
Blog Entries: 3

Rep: Reputation: 345Reputation: 345Reputation: 345Reputation: 345
Quote:
Originally Posted by grail View Post
Code:
awk '{$2 = $2 == 0?".":$2;$3 = $3 == 0?".":$3}1' ped.rc
Sorry to be a pain, but I don't understand your code (new to awk it's one of the languages which I'm trying to make efforts to learn). What does the '1' at the end do? Does it just tell awk to print the line out, or is it something deeper than that? I understand the a?b:c constructs you've got.
 
Old 05-30-2012, 02:50 PM   #6
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,252

Rep: Reputation: 2685Reputation: 2685Reputation: 2685Reputation: 2685Reputation: 2685Reputation: 2685Reputation: 2685Reputation: 2685Reputation: 2685Reputation: 2685Reputation: 2685
Quote:
Does it just tell awk to print the line out
Got it in one Any expression not in braces is evaluated and if true the braces are entered, but if no braces then the default action is performed, which is print.
 
1 members found this post helpful.
Old 05-30-2012, 03:03 PM   #7
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976
Quote:
Originally Posted by grail View Post
Got it in one Any expression not in braces is evaluated and if true the braces are entered, but if no braces then the default action is performed, which is print.
In addition it's worth to mention what is true and false in awk
 
Old 05-30-2012, 03:59 PM   #8
iconig
LQ Newbie
 
Registered: May 2012
Posts: 28

Original Poster
Rep: Reputation: Disabled
Hey thanks, I used the awk and it worked
 
Old 05-31-2012, 02:57 AM   #9
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,252

Rep: Reputation: 2685Reputation: 2685Reputation: 2685Reputation: 2685Reputation: 2685Reputation: 2685Reputation: 2685Reputation: 2685Reputation: 2685Reputation: 2685Reputation: 2685
Please mark as SOLVED once you have a solution.
 
Old 05-31-2012, 10:19 AM   #10
Snark1994
Senior Member
 
Registered: Sep 2010
Location: Wales, UK
Distribution: Arch
Posts: 1,632
Blog Entries: 3

Rep: Reputation: 345Reputation: 345Reputation: 345Reputation: 345
Thanks grail and colucix
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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
Need to get this code to run through whole text file while changing a field shayno90 Linux - Newbie 3 10-15-2009 02:30 PM
remove the extra numeric field in a text file powah Programming 13 01-08-2008 09:24 PM
substituting or deleting a few characters in text i.you Linux - Software 3 12-19-2007 03:39 AM
Command to separate 2nd line's 3rd word from a text file senthilvael Linux - Newbie 7 11-30-2007 12:36 AM
how not to print the 4th field from a text file with six fields livetoday Red Hat 3 10-02-2007 02:19 PM


All times are GMT -5. The time now is 04:06 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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration