Reverse even lines


Login or Register for Dates, Times and to Reply

 
Thread Tools Search this Thread
# 8  
Code:
pesky awk-s - don't flush output  unless you explicitly close the pipped cmd
awk 'NR % 2 {print;next}; {print | "rev";close("rev")}' myfile

Code:
# 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


Last edited by vgersh99; 10-30-2013 at 06:23 PM..
This User Gave Thanks to vgersh99 For This Post:
# 9  
try also:
Code:
awk '{if (NR % 2) {print} else {r="rev" ; print | r; close(r);}; }' input

# 10  
gawk / mawk / bwk variation to vgersh99's suggestion:
Code:
awk '!(NR%2){for(i=2;i<=NF;i++) $1=$i $1; $0=$1}1' FS= file


Last edited by Scrutinizer; 10-31-2013 at 05:40 AM..
# 11  
Thank you all for your help.
Login or Register for Dates, Times and to Reply

Previous Thread | Next Thread
Thread Tools Search this Thread
Search this Thread:
Advanced Search

Test Your Knowledge in Computers #358
Difficulty: Medium
ibmawk is a fork of mawk, allowing applications to embed multiple parallel instances of awk interpreters.
True or False?

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Reverse content

I have file and need to reverse the contents: cat filename 2345 AXY 34567 Output expects 34567 AXY 2345 (1 Reply)
Discussion started by: Maayi
1 Replies

2. UNIX for Dummies Questions & Answers

Reverse complement

I want to reverse some DNA sequences and complement them at the same time. Thus, A changes to T; C to G; T to A and G to C. example: infile >GHL8OVD01CMQVT SHORT1 TTGATGT >GHL8OVD01CMQVT SHORT2 TTGATGT outfile: >GHL8OVD01CMQVT SHORT1 ACATCAA >GHL8OVD01CMQVT SHORT2 ACATCAA The... (4 Replies)
Discussion started by: Xterra
4 Replies

3. 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

4. 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

5. Shell Programming and Scripting

Reverse multiword

Just check out the script... 1 #!/bin/bash 2 3 echo -n "Enter a string :: " 4 read str 5 echo -n "Reverse is :: " 6 l=`expr length "$str"` 7 while 8 do 9 m=`echo $str | cut -c "$l"` 10 echo -n $m 11 ... (3 Replies)
Discussion started by: lipun4u
3 Replies

6. 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

7. 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

8. 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

9. Linux

Reverse Proxy

I have configured reverse proxy through apache...conf file is attached My reverse proxy has a public ip.it is redirecting the request to 172.16.1.43 which is http server.....Now i have a link in Http server's home page which will redirect the request to another Lan zone machine... (0 Replies)
Discussion started by: dipanrc
0 Replies

10. 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

Featured Tech Videos