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 8324527
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T00:06:22+00:00 2026-06-09T00:06:22+00:00

i have a small problem. i need to sort a file according to the

  • 0

i have a small problem. i need to sort a file according to the certain starting string of the file. for example in this form according to digits 04, 05 or 06:

04..............
................
................
05..............
................
................
06..............
................
................ etc.. 

here is my awk code: http://pastebin.com/dLsWkV3q

or just here:

echo "starting...input file"
read file
echo "reading file..."                                                  

echo "... now sorting..."
cat $file | awk '{
if($1=="04"){
print >> "04_file.txt";
}
if($1=="05"){
print >> "05_file.txt";
 }

if($1=="06"){
print >> "06_file.txt";
 }
}'

echo "finished, bye?"
read wait
echo "bye"

the goal is that i need multiple files as a result which contain only corresponding blocks, for the example above: as a result i will have 3 files. 04_file.txt, 05_file.txt and 06_file.txt. AND 05_file.txt doesnot have any line from 04 block. final 04_file.txt file will have only this:

04..............
................
................

my problem is that it is saving other blocks also into 04_file.txt..

i will appreciate any help. thanks a lot

  • 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-06-09T00:06:25+00:00Added an answer on June 9, 2026 at 12:06 am

    I assume that the only lines starting with 04, 05 etc. are the lines separating the different blocks:

    awk '
        BEGIN { mode="unknown" }
        /^04/ { mode="04" }
        /^05/ { mode="05" }
        /^06/ { mode="06" }
              { if (mode != "unknown") print $0 >> sprintf("%s_file.txt", mode)}' < $file
    

    awk is basically an environment to execute actions depending on patterns. Every pattern-action statement has the form

    pattern { action }
    

    In its most simple form a pattern is a regular expression matching the current input line. BEGIN is a special case which “matches” before the input is read and there is also an END “pattern” which gets executed after the input file is consumed.

    On execution awk reads the input file line by line and executes all actions where the pattern matches the line. In the code above the mode variable is set if the input line begins (^) with 04, 05 etc. The last line (without the pattern) matches to all lines and just writes the whole line to the corresponding file.

    I’ll try to summarize this in some pseudo code:

    mode := "unknown"
    for each line of input do
        if line starts with 04 then set mode to 04 endif
        if line starts with 05 then set mode to 05 endif
        if line starts with 06 then set mode to 06 endif
    
        # mode is now either "unknown" if no 04/05/06 pattern has been read
        # from the file yet or holds the most recently read block start (04/05/06)
    
        if mode is not "unknown" then
            append whole line to file named 'mode'_file.txt
        endif
    endfor
    

    If you want to go further have a look at the man page or browse the web for awk introduction. There you also can learn more about the pattern part which can do much more than just match the current input line as in the example above.


    In case all blocks start with two digits (any digits) the code above can be shortened to

    awk '
        BEGIN { mode="unknown" }
        /^[[:digit:]][[:digit:]]/ { mode=substr($0, 1, 2) }
              { if (mode != "unknown") print $0 >> sprintf("%s_file.txt", mode)}' < $file
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

OSX 10.7, XCode 4. I have a small problem: I need to push back
I am currently working on my final thesis.I have a small problem.I need to
I have given a diagram of my current small problem that I need help
I know that this question seems to be banal, but I have small problem.
i have a small problem with my script and i'm stuck... I need to
have small problem, and would very much appreciate help :) I should convert byte
I have small problem with Spring MVC. Basically what I'm trying to do is
I have a small problem and I've figured out where when and why it
I have a small problem which i can't seem to figure out. I tried
I have a small problem which i can't seem to solve myself. Look at

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.