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
CodePudding user response:
$ awk '{printf "%s%s", $0, (NR%2 ? OFS : ORS)}' 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
The minor differences between this and @AndreWildberg's answer are:
- This holds no lines in memory while Andres holds 1 line (not an issue, just a difference).
- If there were an odd number of lines in the input, this would print all of them (though with a blank instead of a terminating newline at the end of the last line) while Andres would delete the last line (but produce a terminating newline), e.g.:
$ seq 5 | awk '{printf "%s%s", $0, (NR%2 ? OFS : ORS)}'
1 2
3 4
5 $
vs
$ seq 5 | awk 'NR % 2 == 0{print prev, $0} {prev = $0}'
1 2
3 4
$
CodePudding user response:
Another approach
$ paste - - <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
or (more fragile)
$ pr -w112 -2at 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
CodePudding user response:
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.