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

  • Home
  • SEARCH
  • 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 8693071
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T00:27:37+00:00 2026-06-13T00:27:37+00:00

I need to use an awk script to extract some information from a file.

  • 0

I need to use an awk script to extract some information from a file.
I have a title line which has 11 field and I split it to an array called titleList.

Student Number:Name:Lab1:Lab2:Lab3:Lab4:Lab5:Lab6:Exam1:Exam2:Final

After finding a proper line I need to print the fields which proceeds by the titles for example if the result is :

92839342:Robert Bloomingdale:9:26:18:22:9:12:25:39:99

I must print it in this way:

Student Number:92839342 Name:Robert Bloomingdale Lab1:9 Lab2:26 Lab3:18
Lab4:22 Lab5:9 Lab6:12 Exam1:25 Exam2:39 Final:99

I use a for loop to manage it:

for (i=0 ;i<=NF ;i++)
{
    printf "%s %s %s %s",titleList[i],":",$i," "
}

everything look good except the result which has 2 problems:
first there is an extra space between each result and second the last field of the searched line is missing

    Student Number : 92839342  Name : Robert Bloomingdale  Lab1 : 9  Lab2 : 26
    Lab3:18  Lab4 : 22  Lab5 : 9  Lab6 : 12  Exam1 : 25  Exam2 : 39  Final 

what should I do?
is there any problem with \n at the end of the search result?

  • 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-13T00:27:38+00:00Added an answer on June 13, 2026 at 12:27 am

    You can correct the amount of extra whitespace between fields by correcting the printf statement:

    awk -F ":" 'NR == 1 { split($0, array, FS) } NR >= 2 { for (i=1; i<=NF; i++) printf "%s:%s ", array[i], $i; printf "\n" }' file.txt
    

    Contents of file.txt:

    Student Number:Name:Lab1:Lab2:Lab3:Lab4:Lab5:Lab6:Exam1:Exam2:Final
    92839342:Robert Bloomingdale:9:26:18:22:9:12:25:39:99
    

    Results:

    Student Number:92839342 Name:Robert Bloomingdale Lab1:9 Lab2:26 Lab3:18 Lab4:22 Lab5:9 Lab6:12 Exam1:25 Exam2:39 Final:99
    

    EDIT:

    Also, your missing the last value because the file you’re working with probably has windows newline endings. To fix this, run: dos2unix file.txt before running your awk code. Alternatively, you can set awk‘s record separater so that it understands newline endings:

    awk 'BEGIN { RS="\r\n"; FS=":" } NR == 1 { split($0, array, FS) } NR >= 2 { for (i=1; i<=NF; i++) printf "%s:%s ", array[i], $i; printf "\n" }' file.txt
    

    EDIT:

    The above requires GNU awk, split() splits on the FS by default so no need to use that as an arg, it’s common to use “next” rather than specifying opposite conditions, and it’s common to use print “” instead of printf “\n” so you use the ORS setting rather than hard-coding it’s value in output statements. So, the above should be tweaked to:

    gawk 'BEGIN { RS="\r\n"; FS=":" } NR == 1 { split($0, array); next } { for (i=1; i<=NF; i++) printf "%s:%s ", array[i], $i; print "" }' file.txt
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have some awk scripts that use gawk from cygwin. Now I need to
I need to read some values from a file with awk and then need
I need to execute a command per line of some file. For example: file1.txt
I'm trying to use the following awk script to count every 1000 records from
I need a sed script that deletes every seventh line in a file. I
I have a file which has the number of .pdfs in my folder. I
I have installed Matlab r2010a on my computer I need use the function xlsread
Need to use own imaged markers instead built-in pins. I have several questions. 1.
I need to use unix Style line endings (only LF) in Zend Studio 8/9
I am trying to extract conversations from a Postfix log file based on the

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.