Saturday, May 4, 2024
 Popular · Latest · Hot · Upcoming
1
rated 0 times [  1] [ 0]  / answers: 1 / hits: 441  / 3 Years ago, mon, september 27, 2021, 9:26:32

I have a file that looks like:



@ATDGGSGDTSG
NTCCCCC
+
nddhdhnadn
@ATDGGSGDTSG
NTCCCCC
+
nddhdhnadn


Now its a repeating pattern of "4" lines and I every time want to print only the 2nd line i.e. the line after the line starting with "@" i.e 2nd line..6th line..etc.



How can I do it?


More From » perl

 Answers
1

There are many possible solutions to the problem. With awk, the following line fits the requirements:



awk '{if ((NR+2) % 4 == 0) print $0}'


Test:



$ cat test.txt
@ATDGGSGDTSG
NTCCCCC
+
nddhdhnadn
@ATDGGSGDTSG
NTCCCCC
+
nddhdhnadn

$ awk '{if ((NR+2) % 4 == 0) print $0}' test.txt
NTCCCCC
NTCCCCC


It displays every 4th line starting from line 2 (because the logical expression ((NR+2) % 4 == 0 is true, where NR contains the actual row number).



If your input happens to have these empty lines in between, then the problem is about displaying the 3rd line and then every 8th.



awk '{if ((NR+5) % 8 == 0) print $0}'

[#28855] Tuesday, September 28, 2021, 3 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
ionash

Total Points: 214
Total Questions: 111
Total Answers: 116

Location: Sudan
Member since Thu, May 7, 2020
4 Years ago
;