×
INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Contact US

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here

AWK move lines of a file down

AWK move lines of a file down

AWK move lines of a file down

(OP)
Hello,

I own
input

CODE --> awk

AB-0004 XXX XXX
A8-0005 XXX
AB-0007 XXX XXX
AB-0008 XXX 


and I would like to get

Output

CODE --> awk

AB-0004 XXX XXX
AB-0007 XXX XXX
A8-0005 XXX
AB-0008 XXX 


I would like to be able to move lines of a file down according to the number of columns whether with an awk or a sed but without relying on the line number

If anyone had an idea! Thank you very much !

RE: AWK move lines of a file down

My only idea is, that you can read all lines into an array and in the END{} print the array elements in order you need

RE: AWK move lines of a file down

You can use awk with sort and cut:

For example we have this file:

CODE

$ cat judkil_sortfile.txt 
AB-0004 XXX XXX
A8-0005 XXX
A8-0006 XXX XXX XXX
AB-0007 XXX XXX
AB-0008 XXX 
and we want to sort the lines according to the number of columns.

First, we can use awk to print number of columns (NF = Number of Fields) in every line as the the 1st column

CODE

$ awk '{print NF, $0}' judkil_sortfile.txt
3 AB-0004 XXX XXX
2 A8-0005 XXX
4 A8-0006 XXX XXX XXX
3 AB-0007 XXX XXX
2 AB-0008 XXX 

Then, we can use sort, to sort awk's output according to the 1st column in reverse order

CODE

$ awk '{print NF, $0}' judkil_sortfile.txt | sort -nr
4 A8-0006 XXX XXX XXX
3 AB-0007 XXX XXX
3 AB-0004 XXX XXX
2 AB-0008 XXX
2 A8-0005 XXX 

Finally, we can use cut, to select from sort's output only the second through last columns (fields)

CODE

$ awk '{print NF, $0}' judkil_sortfile.txt | sort -nr | cut -f2- -d' '
A8-0006 XXX XXX XXX
AB-0007 XXX XXX
AB-0004 XXX XXX
AB-0008 XXX
A8-0005 XXX 
.. and that seems to be what you need

RE: AWK move lines of a file down

(OP)
It's ok !! Thank you very much !

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members! Already a Member? Login

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close