I'm trying to reverse every even line in my file using the awk command below but it prints only the odd lines but nothing else:
$ awk '(NR % 2) {print}; !(NR % 2) {print | "rev";}' myfile
Any idea what I might have done wrong?
Thank you.
I'm trying to reverse every even line in my file using the awk command below but it prints only the odd lines but nothing else:
$ awk '(NR % 2) {print}; !(NR % 2) {print | "rev";}' myfile
Any idea what I might have done wrong?
Thank you.
Show input and expected output
Input:
@MVM-RI-I124161:36:000000000-A326L:1:1101:14597:1343 1:N:0:NTTACTCG
TCAGCGAAAACGCTTCGGAATGGAGGATATCTATTTTCCAAAATCCTCTGTGGTCTTTTTGCAGCATTCTGGAAAAAGACCACAGAGGATTTTGGAAAATAGATATATTTAATGGAGGCAAGTGTGAAAAATCACATTGAAATGCA
+
>>111>1111B1AEFE?000ACB10?0FBFGHCHHHHGFFD1CFECGGFFFFBFFGGHHH?A21B0EGGHEE1B100//E>0?/010/0FGGGD/1@10B1@1FDGBDGH2>F2110AEEAFGFHBFHBDAFGFGFGHBGFBGBFF
Output:
@MVM-RI-I124161:36:000000000-A326L:1:1101:14597:1343 1:N:0:NTTACTCG
ACGTAAAGTTACACTAAAAAGTGTGAACGGAGGTAATTTATATAGATAAAAGGTTTTAGGAGACACCAGAAAAAGGTCTTACGACGTTTTTCTGGTGTCTCCTAAAACCTTTTATCTATAGGAGGTAAGGCTTCGCAAAAGCGACT
+
FFBGBFGBHGFGFGFADBHFBHFGFAEEA0112F>2HGDBGDF1@1B01@1/DGGGF0/010/?0>E//001B1EEHGGE0B12A?HHHGGFFBFFFFGGCEFC1DFFGHHHHCHGFBF0?01BCA000?EFEA1B1111>111>>
Thanks.
awk '!(FNR%2) {t="";l=length;for(i=1;i<=l;i++) t=substr($0,i,1) t;$0=t}1' myFile
This is doing the trick but any idea why my command doesn't work? And can you please explain your command? Thank you.
$ cat input
1
1 2
1 2 3
1 2 3 4
$ awk '(NR % 2 == 0) { print }' input
1 2
1 2 3 4
$ awk '(NR % 2 == 1) { print }' input
1
1 2 3
Try
$ awk '{if(NR%2==0)print $0 | "rev" ;else print }' file
pesky awk-s - don't flush output unless you explicitly close the pipped cmd
awk 'NR % 2 {print;next}; {print | "rev";close("rev")}' myfile
# for every EVEN line
!(FNR%2) {
# initialize a temp var t to an empty string
t=""
# temp var 'l' contains the length of the current record/line
l=length
# iterate through the current line assigning the reversed chars to 't'
for(i=1;i<=l;i++)
t=substr($0,i,1) t
# assign the final reversed record/line back to $0
$0=t
}
# 1 in awk is default/shortcut to 'print' current/$0 record/line
1
try also:
awk '{if (NR % 2) {print} else {r="rev" ; print | r; close(r);}; }' input
gawk / mawk / bwk variation to vgersh99's suggestion:
awk '!(NR%2){for(i=2;i<=NF;i++) $1=$i $1; $0=$1}1' FS= file
Thank you all for your help.