0

I have a list of values. I want to shift the values at specific rows and 2nd column by 0.2. That is-

1 2 3
4 5 6  
7 8 9

into

1 2.2 3
4 5 6
7 8.2 9

....

for row in 1, 3,6,24,41,47,42,48,29,35,30,36,17,23,18,24
do
slight_up_COORD=`echo "$slight_up_COORD"|awk 'FNR =="${row}" {a=$2} FNR=="${row}" {$2=a+0.2} {print}'`
done
0

3 Answers 3

2

Could you please try following, where in split you need to define all the line numbers for which you want to add .2 in lines. In here I have done for 1,3,6 you could mention more lines here.

awk '
BEGIN{
  num=split("1,3,6",array,",")
  for(i=1;i<=num;i++){
    array1[array[i]]
  }
}
FNR in array1{
  $2+=.2
}
1
' Input_file

Explanation: Adding detailed explanation for above code here.

awk '                               ##Starting awk program from here.
BEGIN{                              ##Starting BEGIN section of this awk program from here.
  num=split("1,3,6",array,",")      ##Splitting 1,3,6 values into array with delimiter comma and getting their total length(of passed line numbers) in variable named num here.
  for(i=1;i<=num;i++){              ##Starting a for Loop starting from i=1 to value of num
    array1[array[i]]                ##Creating array named array1 whose index is array[i] value.
  }
}
FNR in array1{                      ##Checking condition if current line number is present in array1 then do following.
  $2+=.2                            ##Adding .2 to current Line 2nd field here.
}
1                                   ##1 will print edited/non-edited lines here.
' Input_file                        ##Mentioning Input_file name here.
Sign up to request clarification or add additional context in comments.

1 Comment

Hmm, you also used +=.2, funny.
2

As you already have the list of records numbers in comma-delimited list to process, we're going to be practical and use it by reading it in from the STDIN:

$ echo 1, 3,6,24,41,47,42,48,29,35,30,36,17,23,18,24 |
awk '
NR==FNR {
    for(i=1;i<=NF;i++)   # all comma-separated fields
        p[$i+0]          # hash and fix that space in the list with +0
        next             # on to the file
}

(FNR in p) {             # if the record number is in the p hash to be processed
        $2+=.2           # add 0.2 to $2 (this could be $2 ".2" but for change)
        # $2=$2 ".2"     # for non-numeric data
}1' FS=, - FS=\  file    # stdin first, comma delim, then file, space delim

Output:

1 2.2 3
4 5 6  
7 8.2 9

Comments

1

another awk approach

$ seq 9 | paste -d' '  - - - | 
  awk -v rows="1,3,6,24" -v c=, 'c rows c ~ c NR c {$2+=.2}1'

1 2.2 3
4 5 6
7 8.2 9

if you data is in a file

$ awk -v rows="1,3,6,24" -v c=, 'c rows c ~ c NR c {$2+=.2}1' file

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.