0

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

tink
  • 14,342
  • 4
  • 46
  • 50
Philip Scot
  • 171
  • 2
  • 2
  • 9
  • So you want to join all pairs of lines starting with the first and second? If yes `sed -n 'N;s/\n/\t/;p' file` should do the trick (with a tab to separate the two joined lines, replace `\t` by anything you want). – Renaud Pacalet Dec 01 '22 at 13:36
  • Does [this](https://stackoverflow.com/questions/7841607/how-can-i-combine-odd-and-even-numbered-lines) answer your question? – tink Dec 01 '22 at 17:08
  • Or [this](https://stackoverflow.com/questions/9605232/how-to-merge-every-two-lines-into-one-from-the-command-line)? – tink Dec 01 '22 at 17:08

3 Answers3

2
$ 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:

  1. This holds no lines in memory while Andres holds 1 line (not an issue, just a difference).
  2. 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
$
Ed Morton
  • 188,023
  • 17
  • 78
  • 185
  • 1
    Also, this elegantly scales to different modulo values, where Andre's answer is basically hard-coded to 2. (It could be fixed, but then this is essentially the fix.) – tripleee Dec 01 '22 at 16:59
2

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
karakfa
  • 66,216
  • 7
  • 41
  • 56
  • 1
    You can use `pr -2ats' '` (the `-s` option turns off line truncation, so no need to calculate page width) – Sundeep Dec 02 '22 at 04:28
0

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.
Andre Wildberg
  • 12,344
  • 3
  • 12
  • 29