Decrement one from 3rd Column


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Decrement one from 3rd Column
# 1  
Old 03-10-2012
Decrement one from 3rd Column

Hi,

I need a script that will subtract 1 from the third column of the line beginning with %, leaving all other values the same. So 158 should be 157, 308 should be 307, 458 should be 457.

Code:
Before:
#    30109    xyz abc Data
%    30109    158    5    8    2
000023f
01f4145
#    30109    xyz abc Data
%    30109    308    5    8    2
003a300
01f4145
#    30109    xyz abc Data
%    30109    458    5    8    2
39c3b92
01f4145

After:
Code:
Before:
#    30109    xyz abc Data
%    30109    157    5    8    2
000023f
01f4145
#    30109    xyz abc Data
%    30109    307    5    8    2
003a300
01f4145
#    30109    xyz abc Data
%    30109    457    5    8    2
39c3b92
01f4145

Thanks for your help,
morrbie

Last edited by Franklin52; 03-11-2012 at 07:11 AM.. Reason: Please use code tags for data and code samples, thank you
morrbie
# 2  
Old 03-10-2012
Code:
awk '/^%/ { $3-=1;} {print $0}'  oldfile > newfile

# 3  
Old 03-10-2012
Does the below suffice your requirement ?

Code:
awk '{if ($1 ~ /^%/){$3-=1;print $0}else print $0}' input_file


Last edited by codemaniac; 03-10-2012 at 11:36 PM.. Reason: decremented
# 4  
Old 03-11-2012
Yes both of the above answers work.

I also figured out a solution after doing a little research on awk scripting:
Code:
awk -v val="1" '$1=="'%'" {for(i=3;i<=3;i++) $i=$i-val}1' infile>outfile

Thanks you for a quick response and for all your help!

Last edited by Franklin52; 03-11-2012 at 07:12 AM.. Reason: Please use code tags for data and code samples, thank you
morrbie
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 script to compare 3rd column value with first column and display

Hello Team, My source data (INput) is like below EPIC1 router EPIC2 Targetdefinition Exp1 Expres rtr1 Router SQL SrcQual Exp1 Expres rtr1 Router EPIC1 Targetdefinition My output like SQL SrcQual Exp1 Expres Exp1 Expres rtr1 Router rtr1 Router EPIC1 Targetdefinition... (5 Replies)
Discussion started by: sekhar.lsb
5 Replies

2. Shell Programming and Scripting

Solution for replacement of 4th column with 3rd column in a file using awk/sed preserving delimters

input "A","B","C,D","E","F" "S","T","U,V","W","X" "AA","BB","CC,DD","EEEE","FFF" required output: "A","B","C,D","C,D","F" "S", T","U,V","U,V","X" "AA","BB","CC,DD","CC,DD","FFF" tried using awk but double quotes not preserving for every field. any help to solve this is much... (5 Replies)
Discussion started by: khblts
5 Replies

3. Linux

Print the 1st column and the value in 2nd or 3rd column if that is different from the values in 1st

I have file that looks like this, DIP-17571N|refseq:NP_651151 DIP-17460N|refseq:NP_511165|uniprotkb:P45890 DIP-17571N|refseq:NP_651151 DIP-19241N|refseq:NP_524261 DIP-19241N|refseq:NP_524261 DIP-17151N|refseq:NP_524316|uniprotkb:O16797 DIP-19588N|refseq:NP_731165 ... (2 Replies)
Discussion started by: Syeda Sumayya
2 Replies

4. Shell Programming and Scripting

Changing values only in 3rd column and 4th column

#cat file testing test! nipw asdkjasjdk ok! what !ok host server1 check_ssh_disk!102.56.1.101!30!50!/ other host server 2 des check_ssh_disk!192.6.1.10!40!30!/ #grep check file| awk -F! '{print $3,$4}'|awk '{gsub($1,"",$1)}1' 50 30 # Output: (6 Replies)
Discussion started by: kenshinhimura
6 Replies

5. UNIX for Dummies Questions & Answers

Search word in 3rd column and move it to next column (4th)

Hi, I have a file with +/- 13000 lines and 4 column. I need to search the 3rd column for a word that begins with "SAP-" and move/skip it to the next column (4th). Because the 3rd column need to stay empty. Thanks in advance.:) 89653 36891 OTR-60 SAP-2 89653 36892 OTR-10 SAP-2... (2 Replies)
Discussion started by: AK47
2 Replies

6. Shell Programming and Scripting

AWK script to create max value of 3rd column, grouping by first column

Hi, I need an awk script (or whatever shell-construct) that would take data like below and get the max value of 3 column, when grouping by the 1st column. clientname,day-of-month,max-users ----------------------------------- client1,20120610,5 client2,20120610,2 client3,20120610,7... (3 Replies)
Discussion started by: ckmehta
3 Replies

7. Shell Programming and Scripting

1st column,2nd column on first line 3rd,4th on second line ect...

I need to take one column of data and put it into the following format: 1st line,2nd line 3rd line,4th line 5th line,6th line ... Thanks! (6 Replies)
Discussion started by: batcho
6 Replies

8. Shell Programming and Scripting

Decrement using bash!!

Hi all, Thanks in Advance! I want a simple script to print today and yesterdays date. using this command date +%d%m%Y i can able get today's date but i want yesterday's date with the same format. so i tried using simple decrement operator but... (2 Replies)
Discussion started by: anishkumarv
2 Replies

9. Shell Programming and Scripting

Need to get the 3rd column

Hi Trying to do get the 3rd column out. Any help would be much appreciated Hardware OS Client --------------- --------------- -------------- PC WindowsNET test1 PC WindowsNET test2 PC WindowsNET test3... (6 Replies)
Discussion started by: bombcan1
6 Replies

10. Shell Programming and Scripting

get 3rd column of nth line

hi; i have a file.txt and its 9th, 10th and 11th line lines are: RbsLocalCell=S2C1 maxPortIP 4 (this is 9th line) RbsLocalCell=S3C1 maxPortIP 4 (this is 10th line) RbsLocalCell=S1C1 ... (11 Replies)
Discussion started by: gc_sw
11 Replies
Login or Register to Ask a Question