INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

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.

Jobs

line break every two fields

line break every two fields

(OP)
Hello all,

I know this is a simple question, however I haven't used awk in over a year and am having memory issues... (must be that I turned 30 last week).

Anyway, I want to take a txt file with 8 fields on one line and add line breaks every two fields. For example the file looks like this:

0 1538 10 1539 20 1537 30 1533
40 1529 50 1523 60 1519 70 1516
80 1513 90 1510 100 1509 110 1508
120 1506 130 1505 140 1505 150 1504


I want to rearrange the fields to look like this:

0 1538
10 1539
20 1537
30 1533
40 1529
50 1523 ... etc

Any help will be greatly appreciated!

-J

RE: line break every two fields

(OP)
I have this so far:

awk '{for(i=1;i<NF;i++)if(i%2==0){$i=$i"\n"} }1' U1414_velocity_function.txt

However my output looks like this:

CODE --> awk

0 1538
 10 1539
 20 1537
 30 1533
40 1529
 50 1523
 60 1519
 70 1516
80 1513
 etc... 

I am not sure why it is adding a space on the 2nd, 3rd, and 4th rows of each loop...

RE: line break every two fields

(OP)
Bingo. Thanks PH!

RE: line break every two fields

Save the call to awk:

CODE -->

xargs -n 2 < U1414_velocity_function.txt 

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!

Resources

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