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?
# 8  
Old 11-10-2009
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
Code:
1       3
3       12
12      14
14      21
0       34
34      34
34      40
40      90

thank you once more.

Last edited by radoulov; 11-10-2009 at 01:09 PM.. Reason: added code tags
# 9  
Old 11-10-2009
Could you post the content of num3.txt?

Just copy paste:

Code:
cat num3.txt

I get this:

Code:
% 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

# 10  
Old 11-10-2009
here it is:

:~/desktop] Audrey% cat num3.txt
Code:
1       3
3       12
12      14
14      21
0       34
34      34
34      40
40      90


Last edited by radoulov; 11-10-2009 at 01:08 PM.. Reason: please use code tags
# 11  
Old 11-10-2009
If your fields are tab separated you'll need to add OFS too.

Code:
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:
Code:
$ 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).
# 12  
Old 11-10-2009
it WORKED! thank you! thank you! thank you!!!!
# 13  
Old 11-10-2009
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:
Code:
#!/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:
Code:
% ./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
# 14  
Old 11-10-2009
I didn't know about |STAT.
Thanks for the useful information.
Login or Register to Ask a Question

Previous Thread | Next Thread

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
Login or Register to Ask a Question