Unix c-shell - replacing/incrementing values in columns?

2 21
1 12
3 123
4 1234
6 49
0 49
33 212

I need to replace/increment all the values in the 2nd column that correspond to 0 in the first column.

so for
0 49
i would get
0 50

this can be done through:
paste num4.txt num.txt | grep ^0 | dm x1 x2+1

the most important thing is to preserve this increment in the file which the above command fails to do.

basically i need the completed file looking something like this:
2 21
1 12
3 123
4 1234
6 49
0 50
33 212

what would be the most effective way to go about it...?

thanks!

Hi.

It looks like you are using programs from the suite described by:

The |STAT Handbook

                   Data Analysis Programs on UNIX and MSDOS

                                 Gary Perlman

If this is the case, did you look at the documentation for dm? ... cheers, drl

awk '!$1{$2+=1}1' file

thank you for you input.
i tried awk '!$1{$2+=1}1', however it gives me a bunch of errors. problem is that can not decipher the syntax of this awk expression for successful debugging. would you mind clarifying?

paste num.txt num2.txt | awk '!$1{$2+=1}1'

output in terminal:
paste num.txt num2.txt | awk 'num2.txt1{$2+=1}1'
awk: syntax error at source line 1
context is
>>> num2 <<< .txt1{$2+=1}1
awk: bailing out at source line 1

thank you!

It's because you're using csh :slight_smile:

awk '\!$1&&$2++||9' infile

And, of course, you should use gawk, nawk or /usr/xpg4/bin/awk on Solaris.

Edit: Just to clarify - you need to escape the bang (!) only if you're using (t)csh.

given I am using tcsh, and I need to use awk-if command

Basically, logic is as follows -
If some value in column one is equals to 0, then increment the corresponding value in column 2 by one.

What is the correct syntax for this?

Well, did you try my suggestion (to escape the !)?

Actually, you can use this code also:

awk '$1||$2++' infile

It will miss records like this one though:

0 0

In that case you'll need:

awk '$1||++$2' infile

... and it will miss records like this one:

0 -1

So:

awk '$1||$2++||"This one works"' infile

i appreciate input, I have tried both, however nothing is incremented in the finished file, and there are no errors -- what do you think may be the problem??

:~/desktop] Audrey% awk '$1||$2++' num3.txt

1       3
3       12
12      14
14      21
0       34
34      34
34      40
40      90

thank you once more.

Could you post the content of num3.txt?

Just copy paste:

cat num3.txt

I get this:

% cat infile 
1 3
3 12
12 14
14 21
0 34
34 34
34 40
40 90
% awk '$1||$2++' infile
1 3
3 12
12 14
14 21
0 35
34 34
34 40
40 90

here it is:

:~/desktop] Audrey% cat num3.txt

1       3
3       12
12      14
14      21
0       34
34      34
34      40
40      90

If your fields are tab separated you'll need to add OFS too.

awk '$1||$2++' OFS='\t' infile

Could you attach a small part of your input file?

---------- Post updated at 06:13 PM ---------- Previous update was at 06:09 PM ----------

danmero's solution works too:

$ awk '\!$1{$2+=1}1' OFS='\t' infile
1       3
3       12
12      14
14      21
0       35
34      34
34      40
40      90

And of course, I have no time to check what drl suggested, but I'm sure it's a valid solution (probably the best one).

it WORKED! thank you! thank you! thank you!!!!

Hi.

I waited until the awk solutions were working for the OP to avoid confusing the issue. I think awk is best for this kind of task. The language may look odd at first, but it allows solutions that are flexible, powerful, and succinct However, if |stat routines are required, here is how it might look:

#!/usr/bin/env bash

# @(#) s1	Demonstrate manipulation with |stat dm.

echo
set +o nounset
LC_ALL=C ; LANG=C ; export LC_ALL LANG
echo "Environment: LC_ALL = $LC_ALL, LANG = $LANG"
echo "(Versions displayed with local utility \"version\")"
version >/dev/null 2>&1 && version "=o" $(_eat $0 $1) dm
set -o nounset
echo

FILE=${1-data1}

echo " Data file $FILE:"
cat $FILE

echo
echo " Results:"
dm x1 "if x1 = 0 then x2+1 else x2" < $FILE

exit 0

Producing:

% ./s1

Environment: LC_ALL = C, LANG = C
(Versions displayed with local utility "version")
OS, ker|rel, machine: Linux, 2.6.26-2-amd64, x86_64
Distribution        : Debian GNU/Linux 5.0 
GNU bash 3.2.39
dm - (local: ~/executable/dm Nov 9 16:22 )

 Data file data1:
2 21
1 12
3 123
4 1234
6 49
0 49
33 212

 Results:
2	21
1	12
3	123
4	1234
6	49
0	50
33	212

This was done using the information in man dm. If the earlier awk solutions (involving bang [!]) were to be placed in a Bourne shell family script (sh, bash, zsh), the troubles may not have occurred. I use tcsh a lot for interactive work, but very rarely for scripting, hence I would not have written the command interactively, but I would have placed it in a script -- anything used more than twice, takes arguments, or is "long", I place into a script.

Knowing more than one approach is often useful, and is the heart of *nix systems ... cheers, drl

I didn't know about |STAT.
Thanks for the useful information.

Hi, radoulov.

You're welcome. More information is available at |STAT Home Page and Handbook : Package Home Page ... cheers, drl