LinuxQuestions.org
Review your favorite Linux distribution.
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 10-30-2012, 11:31 AM   #1
cazanadrian
LQ Newbie
 
Registered: Oct 2012
Posts: 6

Rep: Reputation: Disabled
Question add - (minus) in front of a column with value condition of another column


Hello all,

I need some help with a csv file.

My file looks like:

ACL,0,2,1,1,1,0,0
ACL,1,3,2,0,1,0,0
ACL,2,6,2,1,1,0,0
ACL,3,1,1,0,0,1000,0

How I can add - (minus) in front of column 3 if column 5==1

Desired result:

ACL,0,-2,1,1,1,0,0
ACL,1,3,2,0,1,0,0
ACL,2,-6,2,1,1,0,0
ACL,3,1,1,0,0,1000,0

Thanks
 
Old 10-30-2012, 02:51 PM   #2
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Debian sid + kde 3.5 & 4.4
Posts: 6,823

Rep: Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957
Please use ***[code][/code]*** tags around your code and data, to preserve the original formatting and to improve readability. Do not use quote tags, bolding, colors, "start/end" lines, or other creative techniques.

This is a perfect job for awk.

Code:
$ awk -F, -v OFS=, '$5 == 1 { $3 = -$3 }1' file.txt
ACL,0,-2,1,1,1,0,0
ACL,1,3,2,0,1,0,0
ACL,2,-6,2,1,1,0,0
ACL,3,1,1,0,0,1000,0
 
  


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
How to display 2 different column field values as one column value in mysql VijayaRaghavanLakshman Linux - General 2 04-16-2012 10:56 AM
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 05: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 12:48 PM
Concatenate column 1 and column 2 of related lines cgcamal Programming 4 11-20-2008 11:43 AM


All times are GMT -5. The time now is 04:31 AM.

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