Reverse even lines


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Reverse even lines
# 8  
Old 10-30-2013
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 05:23 PM..
This User Gave Thanks to vgersh99 For This Post:
# 9  
Old 10-30-2013
try also:
Code:
awk '{if (NR % 2) {print} else {r="rev" ; print | r; close(r);}; }' input

# 10  
Old 10-31-2013
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 04:40 AM..
# 11  
Old 10-31-2013
Thank you all for your help.
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