Reversing line and word order using awk


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Reversing line and word order using awk
# 1  
Old 03-21-2011
Computer Reversing line and word order using awk

Hello, I am new to awk and I was wandering if I could reverse line and word order from a text file using awk. I figured out how to do them both separately, but can't quite figure out how to mix them.

Example:

Input file:
Code:
dog cat mouse
1 2 3
I am new to awk

Output of the awk program:
Code:
awk to new am I
3 2 1
mouse cat dog

Thank you in advance for your replies! Smilie

Moderator's Comments:
Mod Comment Please use [code] and [/code] tags when posting code, data or logs etc. to preserve formatting and enhance readability, thanks.

Last edited by zaxxon; 03-21-2011 at 01:45 PM.. Reason: code tags
# 2  
Old 03-21-2011
This sounds a lot like homework. We have a special forum for homework.
You can use the tac command if you are on linux.
Code:
tac filename | awk 'your code goes here'

Otherwise:
Code:
awk ' arr[NR]=$0; END{for(i=NR; i>0;i--) {print arr[i]}}' filename

acts like tac.
SO:
[code]
awk ' arr[NR]=sprintf("%s %s %s", $3 , $2, $1); END{for(i=NR; i>0;i--) {print arr[i]}}' filename

This works for a fixed number of fields in the file.
This User Gave Thanks to jim mcnamara For This Post:
# 3  
Old 03-22-2011
Hi blink_w,

Try with:

Code:
awk '{for (i = 1; i <= NF; i++) a[i, NR] = $i}
END {for (j = NR; j > 0; j--){
for (i = NF; i >= 1; i--) printf("%s ", a[i, j])
printf("\n")}}' inputfile | awk '{sub(/^[ \t]+/, "")};1'
awk to new am I 
3 2 1 
mouse cat dog 

Regards.
This User Gave Thanks to cgkmal For This Post:
# 4  
Old 03-22-2011
Thank you guys very much Smilie!!
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Reversing large data set with awk?

Hello! I have quite a bit of map data that I have to edit. I originally had a DOS script that would reverse x1, y1 coordinates in order to change the direction of a particular segment in a map file. It worked wonderfully and all was well, but my bossman told me that there is a boatload of nodes... (9 Replies)
Discussion started by: Mothra
9 Replies

2. Shell Programming and Scripting

Find word in a line and output in which line the word occurs / no. of times it occurred

I have a file: file.txt, which contains the following data in it. This is a file, my name is Karl, what is this process, karl is karl junior, file is a test file, file's name is file.txt My name is not Karl, my name is Karl Joey What is your name? Do you know your name and... (3 Replies)
Discussion started by: anuragpgtgerman
3 Replies

3. Shell Programming and Scripting

Reversing text order of each line of a file in UNIX

My input is: hello how are you my chemistry book is lost what is up etc... And I want the output to be: 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 (2 Replies)
Discussion started by: Heidi Heweidy
2 Replies

4. Shell Programming and Scripting

sed / awk to get specific word in line

I have http log that I want to get words after specific "tag", this a sample line from the log: 98,POST,200 OK,www.facebook.com,Mozilla/5.0 (Windows NT 6.1; WOW64; rv:9.0.1) Gecko/20100101 Firefox/9.0.1,/ajax/updatestatus.php?__a=1,datr=P_H1TgjTczCHxiGwdIF5tvpC; lu=Si1fMkcrU2SInpY8tk_7tAnw;... (6 Replies)
Discussion started by: erlanq
6 Replies

5. Shell Programming and Scripting

Reversing a line based on column

Hi, I have a file that looks like this (tab seperated): read - DFHJ read1 - IOPE read2 + AAAB read3 + MMMN Basically what i want to do is reverse column 3 if column 2 has a - but leave it if its + so the output would look like this: read - JHFD read1 - EPOI read2 + AAAB... (3 Replies)
Discussion started by: kylle345
3 Replies

6. Shell Programming and Scripting

reversing order of lines in a file

how can i reverse the line order in text files? (but total number of the lines is not constant ) for example i have a file like this: line1 line2 line3 . . lineN i wantto make it like this: lineN . . . line3 (26 Replies)
Discussion started by: gfhgfnhhn
26 Replies

7. Shell Programming and Scripting

reversing a line

Hi, I could not find this anywhere and I am wondering if someone knows a quick way of doing this. So heres the problem... I have a row that looks like this (an example): 5 4 3 2 1 What I want to do is reverse it so it looks like this: 1 2 3 4 5 Does anyone know the simple unix... (7 Replies)
Discussion started by: kylle345
7 Replies

8. Shell Programming and Scripting

Reversing file order using SED

Im trying to develop a shell script that will change the content order of the file. For example I have a file that says a b c d I want to change this to be d c b a Im trying to use sed to this by reading the file and then inserting each line at the top #!/usr/bin/ksh ... (3 Replies)
Discussion started by: MBGPS
3 Replies

9. Shell Programming and Scripting

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 ? #!/bin/sh file=$1... (1 Reply)
Discussion started by: scotty_123
1 Replies

10. Shell Programming and Scripting

output string in reversing order

If I have string { I_love_shell_scripts} anyone knows how to have output {stpircs_llehs_evol_I} by using shell and perl ?I know in perl, there is reverse() funcation, but can it be done by not using reverse()? (3 Replies)
Discussion started by: ccp
3 Replies
Login or Register to Ask a Question