Reputation: 11
I'm working on a little code with awk: I'm looking for a pattern and if its find I would like to print the newt 3 lines. Without an if, no problem:
awk '/\/1/ {x=NR+3}(NR<=x) {print > "out"}' input
the file I use:
@_5:1:7:9569:21200/1
CAGAATGCCGTGGAACTGAAACGTCTGGC
+
CCCFFFFFHHHHHJJJJIJJIHIJJIJJI
@_5:1:7:9569:21200/2
GCACCATCATCACCGGTTCCGGGCAGCGC
+
CCCFFFFFHHFHHJJJGHJJJJJJJIGGI
@_5:1:11:12099:7543/1
CAGAATGCCGTGGAACTGAAACGTCTGGC
I would like to separate this file in two others as follow
File 1
@_5:1:7:9569:21200/1
CAGAATGCCGTGGAACTGAAACGTCTGGC
+
CCCFFFFFHHHHHJJJJIJJIHIJJIJJI
@_5:1:11:12099:7543/1
CAGAATGCCGTGGAACTGAAACGTCTGGC
File 2
@_5:1:7:9569:21200/2
GCACCATCATCACCGGTTCCGGGCAGCGC
+
CCCFFFFFHHFHHJJJGHJJJJJJJIGGI
But with the if I have syntax error on the print
awk '{ if (/\/1/) {x=NR+3}(NR<=x) {print > "file1"};} else (/\/2/) {x=NR+3}(NR<=x) {print > "file2"}' "input_file"
If someone has an idea to fix that
Thanks!
Upvotes: 1
Views: 522
Reputation: 41446
Some like this:
awk -F"/" '/^@/ {f=$2} {print > ("file"f+0)}' data
Need to add the +0
to remove space after the line.
cat file1
@_5:1:7:9569:21200/1
CAGAATGCCGTGGAACTGAAACGTCTGGC
+
CCCFFFFFHHHHHJJJJIJJIHIJJIJJI
@_5:1:11:12099:7543/1
CAGAATGCCGTGGAACTGAAACGTCTGGC
cat file2
@_5:1:7:9569:21200/2
GCACCATCATCACCGGTTCCGGGCAGCGC
+
CCCFFFFFHHFHHJJJGHJJJJJJJIGGI
Using -F/
divide line into $1
and $2
by the separator /
{f=$2}
store the last digit of line starting with @
Then data is written to "file"f"
so when f=1, it would be file1
etc.
Upvotes: 1