Replacing certain fields from certain rows


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Replacing certain fields from certain rows
# 1  
Old 04-16-2011
Replacing certain fields from certain rows

Hi all,

say for example i have the next input file

Code:
   30

Au      7.500000    7.500000    5.774000
Au      7.500000    8.995000    8.363000
Au      7.500000    6.005000    8.363000
Au     20.633000    7.500000    9.226000
Au     20.632000    6.005000    6.637000
Au     20.632000    8.995000    6.637000
Au      9.755453    7.933470    7.400875
Au     18.407230    7.093490    7.665465
H      11.819851    5.334439   10.410752
C      11.940178    5.715861    9.377912
C      13.168054    6.318866    9.010767
S      14.466122    6.254196   10.233180
C      13.396476    6.923076    7.720407
C      12.297938    6.830318    6.824263
H      12.401503    7.219851    5.792611
C      11.048036    6.178032    7.146598
H      10.441122    5.788852    6.299594
C      10.851494    5.591076    8.472222
S       9.412998    4.693344    9.008371
S      15.240980    8.199293    9.985104
C      15.636569    8.091816    8.246799
C      16.885748    8.701619    7.834007
H      17.409941    9.354856    8.565981
C      17.223130    8.827686    6.413005
S      18.529427    9.874926    5.768856
C      16.296723    8.218102    5.498867
H      16.532233    8.233681    4.415469
C      15.107140    7.612621    5.934757
H      14.460039    7.154082    5.160631
C      14.709295    7.536656    7.313049

and i just want to add 0.1 to the second field of the 6th 7th and 8th row; so the final output would be:

Code:
   30

Au      7.500000    7.500000    5.774000
Au      7.500000    8.995000    8.363000
Au      7.500000    6.005000    8.363000
Au     20.733000    7.500000    9.226000
Au     20.732000    6.005000    6.637000
Au     20.732000    8.995000    6.637000
Au      9.755453    7.933470    7.400875
Au     18.407230    7.093490    7.665465
H      11.819851    5.334439   10.410752
C      11.940178    5.715861    9.377912
C      13.168054    6.318866    9.010767
S      14.466122    6.254196   10.233180
C      13.396476    6.923076    7.720407
C      12.297938    6.830318    6.824263
H      12.401503    7.219851    5.792611
C      11.048036    6.178032    7.146598
H      10.441122    5.788852    6.299594
C      10.851494    5.591076    8.472222
S       9.412998    4.693344    9.008371
S      15.240980    8.199293    9.985104
C      15.636569    8.091816    8.246799
C      16.885748    8.701619    7.834007
H      17.409941    9.354856    8.565981
C      17.223130    8.827686    6.413005
S      18.529427    9.874926    5.768856
C      16.296723    8.218102    5.498867
H      16.532233    8.233681    4.415469
C      15.107140    7.612621    5.934757
H      14.460039    7.154082    5.160631
C      14.709295    7.536656    7.313049

How can i manage to do that with awk?
Thanks
# 2  
Old 04-16-2011
Try:
Code:
awk 'NR>5&&NR<9{$2+=0.1}1' file

This User Gave Thanks to bartus11 For This Post:
# 3  
Old 04-16-2011
That really worked just fine! Thank you!
# 4  
Old 04-20-2011
@Bartus:
I have a question about this solution just to understand better:
What is the significance of 1 (red) in the end ?
awk 'NR>5&&NR<9{$2+=0.1}1' file And what would be the Perl equivalent for this solution?
Cheers Smilie

---------- Post updated at 07:43 AM ---------- Previous update was at 07:43 AM ----------

@Bartus:
I have a question about this solution just to understand better:
What is the significance of 1 (red) in the end ?
Code:
awk 'NR>5&&NR<9{$2+=0.1}1' file

And what would be the Perl equivalent for this solution?
Cheers Smilie
# 5  
Old 04-20-2011
The "1" at the end of AWK command is the same as putting "print" statement there. Perl version of that code:
Code:
perl -anle '$F[1]+=0.1 if $.>5 && $.<9;print join " ",@F' file

Output is a bit messed up now, but it can be easily adjusted using printf formatting.
This User Gave Thanks to bartus11 For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Swapping/replacing fields

Hallo Team, I would like to replace filed 4 and 7 with filed 39 how can i achieve this ? -bash-3.2$ cat dip1.csv| cut -f4,7,24,36,39 -d","|sort -u +27113996891,+27113996891,196.35.130.52,828854047,+27873500077 +27116452690,+27825702918,10.0.109.13:5060,+27116452690,+27116452690... (2 Replies)
Discussion started by: kekanap
2 Replies

2. Shell Programming and Scripting

awk to grep rows by multiple fields

Hello, I met a challenge to extract part of the table. I'd like to grep the first three matches based on field1 and field2. Input: D A 92.85 1315 83 11 D A 95.90 757 28 3 D A 94.38 480 20 7 D A 91.21 307 21 6 D A 94.26 244 ... (6 Replies)
Discussion started by: yifangt
6 Replies

3. Shell Programming and Scripting

Extract fields from different rows.

Hi, I have data like below. SID=D6EB96CC0 HID=9C246D6 CSource=xya Cappe=1 Versionc=3670 MAR1=STL MARS2=STL REQ_BUFFER_ENCODING=UTF-8 REQ_BUFFER_ORIG_ENCODING=UTF-8 RESP_BODY_ENCODING=UTF-8 CON_ID=2713 I want to select CSource=xya (18 Replies)
Discussion started by: chetan.c
18 Replies

4. Shell Programming and Scripting

Remove rows with first 4 fields duplicated in awk

Hi, I am trying to use awk to remove all rows where the first 4 fields are duplicates. e.g. in the following data lines 6-9 would be removed, leaving one copy of the duplicated row (row 5) Borgarhraun FH9822 ol24 FH9822_ol24_m20 ol Deformed c Borgarhraun FH9822 ol24 ... (3 Replies)
Discussion started by: tomahawk
3 Replies

5. Shell Programming and Scripting

Replacing fields

Hi! I have a file somefile.txt: 12, 1, a, b, c, d, e, f 12, 1, a, b, c, d, e, f 17, 51, a, b, c, d, e, f ... I've made this script to read two fields from a line and output a third: cat somefile.txt | awk -F, '{if ($1 == "12" && $2== "1") print "19"; else if ($1 == "17" && $2== "51")... (8 Replies)
Discussion started by: Tr0cken
8 Replies

6. Shell Programming and Scripting

Comparing two files and replacing fields

I have two files with ids and email addresses. File 2 cotains a subset of the records in file 1. The key field is the first field containing the id. file 1: 123|myadr@abc.com 456|myadr2@abc.com 789|myadr3@abc.com file 2: 456|adr456@xyz.com Where the record appears in the second... (3 Replies)
Discussion started by: tltroy
3 Replies

7. UNIX for Dummies Questions & Answers

Finding & Replacing specific Fields

All I have a very large file (aproximately 150,000) as shown below separated by pipe "|". I need to replace data in 2, 16, 17, 23 fields that are of time stamp format. My goal is to look in those fields and it ends with "000000|" then replace it with "000|". In other words, make it as 6 digit... (2 Replies)
Discussion started by: ddraj2015
2 Replies

8. Shell Programming and Scripting

Matching fields of rows and then operating

Hi All, I was seaching for script for solaris 5.10 environmet to get a output file from Input file like this. INPUT FILE---------------- 1000KE,MINE,74748 1000KE,YOUR,123998 200KE,MINE,886049 50KE,MINE,474176 50KE,YOUR,379998 100KE,YOUR,999994 50KE,MINE,9601 50KE,YOUR,990393... (3 Replies)
Discussion started by: ashis.tewari
3 Replies

9. Shell Programming and Scripting

Specifying and replacing fields with awk

#cat BATCH007.TXT 01,661060052,061000104,081118,0915,07,80,1,2/ 99,,,2/ I have this file called BATCH007.TXT. I am trying to change fields 2 and 3 on line 2 to have zeroes. Like this: 01,661060052,061000104,081118,0915,07,80,1,2/ 99,0,0,2/ I can use these commands to print identify the... (2 Replies)
Discussion started by: ddurden7
2 Replies

10. Shell Programming and Scripting

Extract duplicate fields in rows

I have a input file with formating: 6000000901 ;36200103 ;h3a01f496 ; 2000123605 ;36218982 ;heefa1328 ; 2000273132 ;36246985 ;h08c5cb71 ; 2000041207 ;36246985 ;heef75497 ; Each fields is seperated by semi-comma. Sometime, the second files is... (6 Replies)
Discussion started by: anhtt
6 Replies
Login or Register to Ask a Question