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

Retrieving process id using sshcmd on unix
How to insert two lines for every data frame using awk?
awk: divide odd columns by following even column
Combine END and another condition like in `$1<count || END`
awk + gsub special characters
Multiple replacement awk script
Need an awk script to prepend missing zeroes from Solaris mac address
awk multiple lines from a command and print horizontally in a row
awk - Rounding to 2 decimal places in subtotals
Awk input variable as a rule
Awk/Sed add a column to index data
union of rows in multiple files based on key using awk or linux commands
Editing text file using awk/sed
Merging multiple lines in sed, awk
Adding data that start with common string
–°oncatenate two lines on condition with awk

Categories

HOME
batch-file
linkedin
apache-jena
jsf-2.2
web-hosting
maxscript
python-multithreading
nstableheaderview
performancepoint
qpython
fabric.io
windows-ce
opengl-es-3.0
speech-synthesis
enterprise-architect
docx
scsi
xmonad
google-container-registry
production
poedit
rubymotion
springfox
react-toolbox
visual-studio-extensions
easeljs
scriptella
aggregation
tosca
dapper-extensions
vex
panoramas
catch-unit-test
spring-security4
clickjacking
classcastexception
fossil
yowsup
batching
swagger-php
imagenet
signals-slots
kill
preloader
importerror
fwrite
linter
jquery-ui-draggable
user-defined-fields
tinymce-3
reindex
scalding
azure-cli
excel-dna
oozie-coordinator
jtree
django-1.10
javafx-css
omxplayer
custom-lists
google-cloud-console
myspace
boost-serialization
html-to-pdf
beanstalk
design-principles
relativelayout
linklabel
wiredep
xvim
iced-coffeescript
zscript
wso2greg
rdflib
cross-join
wcm
with-statement
codeplex
terminate
vsx
clickbank
ecslidingviewcontroller
notorm
mysql-error-1044
oocss
tlf
scringo
chart-director
apache-abdera
idn
android-loadermanager
fragmenttransaction
boost-msm
vfs
listitem
fitbounds
stress
hibernateexception
ikimagebrowserview
filemerge
lr
django-paypal
commerceserver2007
maven-1
jqueryform
filesystemobject
sun

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