The UNIX and Linux Forums  
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.

Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
.
google unix.com



Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Reversing file order using SED MBGPS Shell Programming and Scripting 3 01-07-2009 10:10 AM
disabiling telnet and ftp from reversing DNS aladdin UNIX for Advanced & Expert Users 1 06-27-2007 06:40 PM
output string in reversing order ccp Shell Programming and Scripting 3 11-19-2005 11:05 AM
Reversing UID's dreaming1 UNIX for Dummies Questions & Answers 3 03-16-2005 07:36 PM
reversing a list TioTony Shell Programming and Scripting 8 10-14-2002 05:00 PM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Bulgarian Greek Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 04-01-2007
scotty_123 scotty_123 is offline
Registered User
  
 

Join Date: Mar 2007
Posts: 17
awk and reversing

Hello

I'm writing script in awk that reverse order the fields of every line in file.
My script have problem with spaces - if there is more spaces between fields
in line of file - my script erase them .
I want my script work like command "tac" - how to change it ?

Code:
#!/bin/sh

file=$1
awk '
 {        
   for(i=NF;i>0;i--)
     printf("%s " , $i); printf("\n")
 } ' $file
  #2 (permalink)  
Old 04-01-2007
reborg's Avatar
reborg reborg is offline Forum Staff  
Administrator
  
 

Join Date: Mar 2005
Location: Ireland
Posts: 4,233
Untested, but something like this might work, depending on your version of awk:
Code:
{
   len=length($0)
   for(i=0;i<len;i++) {
        s=substr($0, len - i , 1 )
        printf("%s" , s )
   }
   printf("\n")
}
just as a comparison, this would do the same thing in ruby:
Code:
ruby -pe '$_.reverse!' file

Last edited by reborg; 04-01-2007 at 06:49 PM..
Closed Thread

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -4. The time now is 09:40 PM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0