Visit Our UNIX and Linux User Community


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


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Unix c-shell - replacing/incrementing values in columns?
# 1  
Old 11-09-2009
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!
# 2  
Old 11-09-2009
Hi.

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

Code:
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
# 3  
Old 11-09-2009
Code:
awk '!$1{$2+=1}1' file

# 4  
Old 11-10-2009
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!
# 5  
Old 11-10-2009
It's because you're using csh Smilie

Code:
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.
# 6  
Old 11-10-2009
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?
# 7  
Old 11-10-2009
Quote:
Originally Posted by audrey_flox
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:

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

It will miss records like this one though:

Code:
0 0

In that case you'll need:


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

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

Code:
0 -1

So:

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


Previous Thread | Next Thread
Test Your Knowledge in Computers #758
Difficulty: Easy
The Amiga was a family of personal computers introduced by Microsoft in 1985.
True or False?

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

UNIX replacing and incrementing number

Hi I am unix newbie looking for a unix bash script that can make it easier to do my code work. we have a code number for each code block that we want to incrementally assign. We have 10000 of these and it is very laborious to do this one by one. so what we want is start from the top of the... (4 Replies)
Discussion started by: chamajid
4 Replies

2. Shell Programming and Scripting

Help with shell script: selecting rows that have the same values in two columns

Hello, everyone I am beginner for shell programming. I want to print all lines that have the same values in first two columns data: a b 1 2 a a 3 4 b b 5 6 a b 4 6 what I expected is : a a 3 4 b b 5 6 but I searched for one hour in... (2 Replies)
Discussion started by: nengcheng
2 Replies

3. Shell Programming and Scripting

awk file to read values from Db2 table replacing hard coded values

Hi, I want to replace a chain of if-else statement in an old AWK file with values from Db2 table or CSV file. The part of code is below... if (start_new_rec=="true"){ exclude_user="false"; user=toupper($6); match(user, "XXXXX."); if (RSTART ==2 ) { ... (9 Replies)
Discussion started by: asandy1234
9 Replies

4. Shell Programming and Scripting

Replacing string by incrementing number

Dear all Say I have a file as ! TICKET NBR : 234 !GSI : 102 ! 3100.2.112.1 11/06/2013 15:56:29 ! 3100.2.22.3 98 ! 3100.2.134.2 8 ! ! TICKET NBR : 1809 ! GSI : 102 ! 3100.2.112.1 11/06/2013 16:00:45 ! 3100.2.22.3 65 ! 3100.2.134.2 3 ! ! TICKET NBR : 587 ! GSI : 102 ! 3100.2.112.1... (3 Replies)
Discussion started by: OTNA
3 Replies

5. UNIX for Dummies Questions & Answers

Getting values of 2 columns from sql query in UNIX variables

Hi, I have connected to oracle database with sqlplus -s / <<EOF select ename, age from emp where empid=1234; EOF I want to save the values of ename and age in unix shell variables. Any pointers would be welcome.. Thanks in advance!!1 Cheers :):):):) (1 Reply)
Discussion started by: gonchusirsa
1 Replies

6. Shell Programming and Scripting

Sum up values of columns in 4 files using shell script

I am new to shell script.I have records like below in 4 different files which have about 10000 records each, all records unique and sorted based on column 2. 1 2 3 4 5 6 --------------------------- SR|1010478|000044590|1|0|0| SR|1014759|000105790|1|0|0| SR|1016609|000108901|1|0|0|... (2 Replies)
Discussion started by: reach.sree@gmai
2 Replies

7. Shell Programming and Scripting

Auto incrementing a particular column values

Hi All, I have the input file(.csv) like below. SNo,Step,Data 1,0,1 2,0,2 3,1,1 4,2,2 5,0,3 6,0,4 and I need the following output. 1,0,1 2,0,2 3,1,1 4,2,2 5,3,3 6,3,4 (5 Replies)
Discussion started by: ks_reddy
5 Replies

8. UNIX for Dummies Questions & Answers

Removing columns from a text file that do not have any values in second and third columns

I have a text file that has three columns. But at the end of the text file, there are trailing lines that have missing second and third columns: 4 0.04972604 KLHL28 4 0.0497332 CSTB 4 0.04979822 AIF1 4 0.04983331 DECR2 4 0.04990344 KATNB1 4 4 4 4 How can I remove the trailing... (3 Replies)
Discussion started by: evelibertine
3 Replies

9. Shell Programming and Scripting

Incrementing ascii values

Hi All, I require some help with the below: I am trying to incriment the ascii value of a letter and then print it. So basically "a" becomes "b" and "z" becomes "A". Does anyone have any pointers? Cheers, Parks (10 Replies)
Discussion started by: bParks
10 Replies

10. Shell Programming and Scripting

Replacing word and Incrementing

Hi I'm having difficulty in writing a script with searching a specified word using sed and replaces that word with numbers that is incremented I tried this: #!/bin/sh awk '{ for (i=2010; i<=NF; i++) sed 's/TBA/$i; }' filename.txt > outputfile.txt but it doesn't work. here is my desired... (1 Reply)
Discussion started by: sexyTrojan
1 Replies

Featured Tech Videos