Unix/Linux Go Back    


Shell Programming and Scripting BSD, Linux, and UNIX shell scripting — Post awk, bash, csh, ksh, perl, php, python, sed, sh, shell scripts, and other shell scripting languages questions here.

Reversing text order of each line of a file in UNIX

Shell Programming and Scripting


Tags
linux, reverse, text file, ubuntu, word order

Closed    
 
Thread Tools Search this Thread Display Modes
    #1  
Old Unix and Linux 07-15-2013   -   Original Discussion by Heidi Heweidy
Heidi Heweidy's Unix or Linux Image
Heidi Heweidy Heidi Heweidy is offline
Registered User
 
Join Date: Jul 2013
Last Activity: 22 July 2013, 2:42 AM EDT
Posts: 2
Thanks: 2
Thanked 0 Times in 0 Posts
Reversing text order of each line of a file in UNIX

My input is:

Code:
hello how are you
my chemistry book is lost
what is up
etc...

And I want the output to be:

Code:
you are how hello
lost is book chemistry my
up is what
....

I found an earlier response to a similar question but it was not accurate as it required a certain string length for each line
Sponsored Links
    #2  
Old Unix and Linux 07-15-2013   -   Original Discussion by Heidi Heweidy
Skrynesaver's Unix or Linux Image
Skrynesaver Skrynesaver is offline Forum Advisor  
Grumpy old geek
 
Join Date: Mar 2011
Last Activity: 13 November 2017, 5:30 AM EST
Location: Éire
Posts: 931
Thanks: 25
Thanked 223 Times in 217 Posts

Code:
perl -ne 'chomp;print join(" ",(reverse(split(/\s+/,$_)))),"\n";' filename

The Following User Says Thank You to Skrynesaver For This Useful Post:
Heidi Heweidy (07-15-2013)
Sponsored Links
    #3  
Old Unix and Linux 07-15-2013   -   Original Discussion by Heidi Heweidy
Scrutinizer's Unix or Linux Image
Scrutinizer Scrutinizer is online now Forum Staff  
Moderator
 
Join Date: Nov 2008
Last Activity: 13 December 2017, 11:09 PM EST
Location: Amsterdam
Posts: 11,650
Thanks: 518
Thanked 3,385 Times in 2,984 Posts
With awk:

Code:
awk '{n=split($0,F); for(i in F) $i=F[n-i+1]}1' file

Sponsored Links
Closed

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Linux More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Reversing line and word order using awk blink_w UNIX for Dummies Questions & Answers 3 03-22-2011 05:30 AM
reversing order of lines in a file gfhgfnhhn Shell Programming and Scripting 26 07-30-2009 09:01 AM
reversing a line kylle345 Shell Programming and Scripting 7 06-03-2009 07:34 AM
Reversing file order using SED MBGPS Shell Programming and Scripting 3 01-07-2009 10:10 AM
output string in reversing order ccp Shell Programming and Scripting 3 11-19-2005 11:05 AM



All times are GMT -4. The time now is 12:17 AM.