Issue
hi all im trying to make this
2022-11-14 18:49:59 Indicator is < 3
1 No
2022-11-14 18:49:59 Indicator is < 10
1 No
2022-11-14 18:49:59 Indicator is < 22
1 No
2022-11-14 18:49:59 Indicator is < 1
1 No
into
2022-11-14 18:49:59 Indicator is < 3 1 No
2022-11-14 18:49:59 Indicator is < 10 1 No
2022-11-14 18:49:59 Indicator is < 22 1 No
2022-11-14 18:49:59 Indicator is < 1 1 No
i found that you can use sed 's/something/some//2'
for every second encounter but how to make it for 1st, 3th, 5th,.... and so one
Solution
Try this with awk
using modulo.
$ awk -v val=2 'NR % val == 0{print prev, $0} {prev = $0}' file
2022-11-14 18:49:59 Indicator is < 3 1 No
2022-11-14 18:49:59 Indicator is < 10 1 No
2022-11-14 18:49:59 Indicator is < 22 1 No
2022-11-14 18:49:59 Indicator is < 1 1 No
It looks at the record number NR
and calculates modulo 2 of it. Since every second line comes out as 0 it will then print the previous prev and the current $0
line.
Remarks:
- Printing last odd lines is undefined, the given example is clear. It can even be seen as a feature to not print them (even out a data set).
- This keeps execution time in mind and is a fast approach.
Answered By - Andre Wildberg Answer Checked By - Marie Seifert (WPSolving Admin)