awk


Awk Printf result of a subtraction between column and fixed value


Let's say I have:
David 1500 2338 Baker
Danny 1290 1550 Bold
Domminic 25 28 Baker
So there are 4 tab-separated columns. size of all columns can vary a lot.
What I would like is a solution using awk printf to do : $2 - 1 for each line.
Here is the result I would like:
David 1499 2338 Baker
Danny 1289 1550 Bold
Domminic 24 28 Baker
And Finally here is what I have done so far:
awk '{if($0 ~/^D/) \
{printf ("%s\t%d\t%d\t%s\n",$1,($2 - 1),$3,$4)}}'
Obviously if I am asking this question, it's because I do not get the expected result. This is what I obtain:
David 1500-1 2338 Baker
Danny 1290-1 1550 Bold
Domminic 25-1 28 Baker
I tried other stuff but it did not worked better...It is really important to me to get a solution in awk with printf.
"help me obi wan kenobi you're my only hope"!
Edit: I made a little mistake in the printf "format" part, the last \t should be a \n .
And then the problem is solved I don't understand why I get this error before. Big thank to Ed for the help (see comment). The awk command is now working.
Just make it simpler by setting input and output field separators to Tab and modify the $2 value as
awk 'BEGIN{FS=OFS="\t"}{$2=$2-1}1' file
You don't need printf at all here. Once the field separators are set, the individual fields can be accessed as $1..$NF. Since the value we are concerned is in $2, just subtract one from it.
The default print action (implied by always-true pattern 1) will rebuild the output line from the fields based on the output field separator (OFS) set.
Since we are modifying only $2 here, the other fields remain intact.
here is another awk
$ awk '/^D/{$2--}1' file | column -t
David 1499 2338 Baker
Danny 1289 1550 Bold
Domminic 24 28 Baker
or, add BEGIN{FS=OFS="\t"} for tabular in/out.

Related Links

Awk syntax in a loop
awk to print line(s) based on conditions in from specific fields in each line
AWK to get average of three columns over an ID
awk to remove duplicates in specific lines based on keyword in field
awk to update field based on if statements except if keyword is in field
awk without printing a newline for multiple files
How to subset a tsv file based on a pattern?
grep multiple directories throws error “Is a directory”
Awk Printf result of a subtraction between column and fixed value
How to sum a selection of columns?
Convert EPOCH time in CSV file (CentOS)
How to evaluate or process if statements in data?
How to remove “ ” in filename only but no the entire file ??
String concatenation in gawk produces an unexpected result
print from match & process several input files
convert “days hh:mm:ss” field to hh:mm:ss or seconds using sed or awk

Categories

HOME
oracle12c
twitter-bootstrap-3
checkstyle
angular-formly
jmx
mstest
oracle-apex-5
sitecore
replace
pdfbox
opendj
google-cloud-functions
carousel
square
polling
angularfire2
ipmitool
filechannel
favicon
gkturnbasedmatch
keytool
easyphp
enterprise-miner
sar
react-toolbox
highstock
resourcebundle
inversion-of-control
user-defined-functions
constexpr
hard-drive
batching
filesystemwatcher
ods
linq-to-objects
spring-mvc-test
odoo-website
naming
avcapturedevice
drupal-theming
apache-directory
vaadin-charts
procfile
phpdbg
opennms
sonarlint-eclipse
singleinstance
wpas
semantic-logging
backbone-collections
boo
er-diagram
reshape
android-instrumentation
redistributable
latin
mongodb-php
jodd
scaffolding
installshield-2010
swizzling
freepbx
nservicebus5
deep
redis-cluster
findfirst
design-principles
gocql
ess
sapscript
adehabitathr
execve
angular-gettext
jama
encryption-asymmetric
named-parameters
twitter-bootstrap-rails
drupal-nodes
high-resolution
http-patch
inputmismatchexception
superscrollorama
insert-id
preload
vfs
thttpd
koken
pinch
denied
uipagecontrol
xui
metaweblog
facebook-iframe
data-mapping
cuda.net
xslcompiledtransform
cardspace

Resources

Mobile Apps Dev
Database Users
javascript
java
csharp
php
android
MS Developer
developer works
python
ios
c
html
jquery
RDBMS discuss
Cloud Virtualization
Database Dev&Adm
javascript
java
csharp
php
python
android
jquery
ruby
ios
html
Mobile App
Mobile App
Mobile App