0

I am very new to script language. I have multiple output files with the names in a similar pattern as: output1 output2 etc.

In each output file, the records follow the pattern as:

"295534
4750
4 -0.28 -0.01 -0.03 0 0 0 0 1 425.1 -1 4 0
0 0.28 -0.01 -0.025 0 0 0 0 1 425.1 -1 4 0
0 0.28 -0.01 -0.02 0 0 0 0 1 425.1 -1 4 0
....."

What I would like to do is to divide the field in the second line by 100 in each file (i.e. 4750/100). How should I do it?

I appreciate any advice.

5
  • 2
    Please edit your question and show us what output you are expecting. Should the original file be modified and the 4750 be replaced by 47.5? Should the script simply print 47.5? Should the script print the modified file instead of changing the original? Also, please add your operating system to your question since that will tell us what awk implementation you are likely to have.
    – terdon
    Apr 29, 2021 at 9:29
  • In case you only need the result of the division, there is no need for awk: for f in output*; do echo $(($(tail -n+2 $f | head -n1)/100)); done. I am not sure if all types of shells support that; the Bourne shell probably doesn't. Apr 29, 2021 at 10:30
  • 1
    @berndbausch that will just be slightly slower than using awk, with more complex and longer syntax, and will break if the number isn't an integer. Also, please don't post solutions in comments because that circumvents the normal QC process of the site since we cannot vote on comments.
    – terdon
    Apr 29, 2021 at 10:51
  • @terdon Strictly speaking, it's not an answer, since OP wants AWK. Apr 29, 2021 at 14:15
  • Oh, that's fine @berndbausch. Providing an answer in a different tool is welcome here. The OP might not accept the answer, but it will still be useful to others who might have the same issue but not share the requirement for a specific tool.
    – terdon
    Apr 29, 2021 at 14:27

1 Answer 1

2
awk 'NR==2{ $0/=100 }1' output1 >output1.fixed

applying on multiple input files :

for file in ./output*; do
    awk 'NR==2{ $0/=100 }1' "$file" >fixed_"$file"
done

apply changes on specifc field (if more than a field) and on second line number:

for file in ./output*; do
    awk 'NR==2{ $1/=100 }1' "$file" >fixed_"$file"
done

$1 represents first field value, change this to any other field you want to change.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .