Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6374669
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T01:31:01+00:00 2026-05-25T01:31:01+00:00

The following awk one-liner allows me to split a file according to the character

  • 0

The following awk one-liner allows me to split a file according to the character at position 22:

awk -v pdb="${file}" -F "" '{close(c);c=$22}{print > pdb"_"c".pdb"}' ${file}.1tmp 

My files are of the type:

ATOM   8911  N   SER W   1      -5.412  94.401  12.569  1.00137.46           N  
ATOM   8912  CA  SER W   1      -4.093  93.709  12.370  1.00137.35           C  
ATOM   8913  C   SER W   1      -3.115  93.771  13.604  1.00137.27           C  
ATOM   8914  O   SER W   1      -2.023  93.177  13.570  1.00137.22           O  
ATOM   8915  CB  SER W   1      -3.417  94.212  11.063  1.00137.29           C  
ATOM      1  N   ASP X   7      70.244 176.432 -72.598  1.00121.87           N  
ATOM      2  CA  ASP X   7      70.164 177.938 -72.649  1.00122.11           C  
ATOM      3  C   ASP X   7      68.705 178.495 -72.843  1.00121.38           C  
ATOM      4  O   ASP X   7      68.482 179.724 -72.941  1.00121.16           O  
ATOM      5  CB  ASP X   7      71.128 178.442 -73.745  1.00122.87           C  
ATOM   5143  N   ASP W   7     -68.623 209.141 -11.831  1.00118.10           N  
ATOM   5144  CA  ASP W   7     -67.698 209.756 -12.845  1.00118.36           C  
ATOM   5145  C   ASP W   7     -66.378 210.288 -12.223  1.00118.02           C  
ATOM   5146  O   ASP W   7     -65.657 211.116 -12.802  1.00118.06           O  
ATOM   5147  CB  ASP W   7     -68.436 210.840 -13.657  1.00118.67           C  

However, the script copies all lines with a W at the 22nd position in the same file even if they are in non-contiguous blocks. I would like to split the file in blocks so that the first contiguous block containing W (or whatever other character) will be named W1 and the second W2 and so on. Can this be easily done with awk or should I go for a loop with a counter or something like that?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-25T01:31:02+00:00Added an answer on May 25, 2026 at 1:31 am
    awk -v pdb="${file}" 'BEGIN{f=1} NR==1{n=$5;s[$5]=f} $5!=n{s[$5]=f++ ;n=$5} { print > pdb"_"$5"_"s[$5]".txt" }' ${file}
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

If so the following one-liner utilizing awk might provide a useful template svn log
I write the following awk ( print VAL_1 & VAL_2 if match in file
When I run the following awk -F\, '{print $2,:,$1}' It prints First : Second
I have found the following command in AWK useful in Vim :'<,'>!awk '{ print
Given the following command, echo "1: " | awk '/1/ -F ":" {print $1}'
I'm trying to use a simple perl one-liner on the following input: @F7##########0/1 C4CTA6GCAAC56G67CTCA99C
I created the following awk command in order to print only the line that
I want to apply the following awk command on files with extension *.txt awk
i would like to convert the data.txt file into the following output using awk
I have following files in one directory. common_api.h , api.h, abcd.a and I have

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.