Reverse even lines


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Reverse even lines
# 1  
Old 10-30-2013
Reverse even lines

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:

Code:
$ awk '(NR % 2) {print}; !(NR % 2) {print | "rev";}' myfile

Any idea what I might have done wrong?

Thank you.

Last edited by Scrutinizer; 10-30-2013 at 06:23 PM.. Reason: Added code tags
# 2  
Old 10-30-2013
Quote:
Originally Posted by ivpz
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
# 3  
Old 10-30-2013
Input:

Code:
@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:
Code:
@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.
Moderator's Comments:
Mod Comment
Please use code tags when posting data and code samples!

Last edited by vgersh99; 10-30-2013 at 05:58 PM.. Reason: code tags, please!
# 4  
Old 10-30-2013
Code:
 awk '!(FNR%2) {t="";l=length;for(i=1;i<=l;i++) t=substr($0,i,1) t;$0=t}1' myFile

# 5  
Old 10-30-2013
Quote:
Originally Posted by vgersh99
Code:
 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.
# 6  
Old 10-30-2013
Code:
$ 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

# 7  
Old 10-30-2013
Try

Code:
$ awk  '{if(NR%2==0)print $0 | "rev" ;else print }' file

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Reverse sort

Hello, I have a large list of names and would like to do a reverse sort on them i.e. the sort should be by the ending and not by the beginning of the word. I had written in awk a small script but it does wrong things { for(i=length($0);i>=1;i--) printf("%s/n",substr($0,i,1)); } Could anyone... (3 Replies)
Discussion started by: gimley
3 Replies

2. Shell Programming and Scripting

Reverse of a string

Hi All, I have a String str="Manish". I would like to reverse it. I know the option to do this in bash is: echo "Manish" | rev but I have seen an alternate solution somewhere, which states that: str="Manish" echo $str | awk '{ for(i=length($0);i>=1;i--) printf("%s",substr($0,i,1));... (7 Replies)
Discussion started by: manishdivs
7 Replies

3. Shell Programming and Scripting

How to reverse output?

hi, I have to reverse the command output like below: output: online offline disable maintening killed How to reverse this output like: killed maintening disable offline online It should be ksh script. (4 Replies)
Discussion started by: a2156z
4 Replies

4. Shell Programming and Scripting

how to reverse file

i am using AIX -ksh how can i reverse any file ,i have already try tac cmd it is not in AIX: please help me out. (3 Replies)
Discussion started by: RahulJoshi
3 Replies

5. Shell Programming and Scripting

Joining lines in reverse. append line 1 to line 2.

Hi I have used many times the various methods to append two lines together in a file. This time I want to append the 1st line to the second and repeat for the complete file.... an example This is the file owns the big brown dog joe owns the small black dog jim What I want is ... (7 Replies)
Discussion started by: dwalley
7 Replies

6. Shell Programming and Scripting

reverse an integer

i have created a script that will reverse any given ineter. #!/bin/ksh echo "Enter the number" read n if then a=`expr $n / 10` b=`expr $n % 10` c=`expr $b \* 10 + $a` fi echo $c --------------------------------------------------------------------- the problem with this script... (4 Replies)
Discussion started by: ali560045
4 Replies

7. Shell Programming and Scripting

How reverse cut or read rows of lines

Hi, My records are like this BSC403_JAIN03|3153_TropicalFarm_LIMJM1-3_97| BSC403_JAIN03|3410_PantaiAceh_PCEHM1_4_97| BSC406_BMIN02|1433_JomHebohTV3_COW7M1_11_97| I want to extract the value before _97| This command BSC_ID=`echo $DATA | cut -f5 -d"_"` gives me _97|, 4, 11 and by... (16 Replies)
Discussion started by: doer
16 Replies

8. Shell Programming and Scripting

string in reverse

Can we print any string in reverse order? For example: oracle 16294 1 0 Aug 11 ? 0:00 ora_reco_crepd oracle 16276 1 0 Aug 11 ? 0:19 ora_dbw0_crepd I need second last column from this output. (0:00 & 0:19). I can use awk print $2 after reversing the string. ... (4 Replies)
Discussion started by: malaymaru
4 Replies

9. Shell Programming and Scripting

Reverse *

when I do $ ls z* List of all files begining with 'z'. But what if I want to do a reverse lookup. Just for interest sake ;) $ ls ztr should be same as $ ls ztr* $ ls zt* $ ls z* (2 Replies)
Discussion started by: azmathshaikh
2 Replies

10. IP Networking

Reverse lookup

Help having problems accesing various sites that require me to be a registered .gov domain. My IP is a registered as an .gov but my nameserver record has changed on my DNS configurartion(I don't know why) from something.gov to somethingelse.gov. Same IP, though. When a reverse lookup is... (1 Reply)
Discussion started by: jpalmer320
1 Replies
Login or Register to Ask a Question