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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T13:52:26+00:00 2026-06-12T13:52:26+00:00

$file is a csv (tab-delimited) with 59 cols and 64 rows. Column 1 is

  • 0

$file is a csv (tab-delimited) with 59 cols and 64 rows. Column 1 is always a string and cols 2+ are always an integer (except once when the value is NULL).

cat ${file} | while read line 
    do awk -F'\t' '{ for (i=2; i<=NF; i++) print $1 "\t" $i "." }';
    done;

Outputs:

Excellent   .
Good    .
…

And yet switching $i to $2 works:

Excellent   29.
Good    7.
…

Why??

EDIT:

#lines 1 & 2 from data.csv (columns truncated for brevity):
Excellent   29  54  47  46  38  22  50
Good    7   14  27  24  26  36  20

#reform.sh
file=$1;
awk -F'\t' '{ for (i=2; i<=NF; i++) print $1 "\t" $i; }' ${file};

Still behaves/returns like the previous.

RESOLUTION:

The approved answer does provide the proper output from AWK. After restarting my Terminal application, the script performed as described. I was not able to determine the cause of the Terminal app issue.

  • 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-12T13:52:27+00:00Added an answer on June 12, 2026 at 1:52 pm

    I think you’re mistaken. The script as you have it doesn’t output anything intelligent (a) because, while you’re attempting to read each line into $line, you’re not actually giving them to awk.

    You can get rid of the superfluous (and incorrect) loop with something like:

    awk -F'\t' '{ for (i=2; i<=NF; i++) print $1 "\t" $i "." }' ${file}
    

    as shown in the following transcript:

    pax> echo 'A 1 2
    ...> B 3 4
    ...> C 5 6' >qq.in
    
    pax> cat qq.in
    A 1 2
    B 3 4
    C 5 6
    
    pax> awk -F' ' '{ for (i=2; i<=NF; i++) print $1 " " $i "." }' qq.in
    A 1.
    A 2.
    B 3.
    B 4.
    C 5.
    C 6.
    

    As you can see (although I’m using spaces rather than tabs), this gives you the output you desire.


    In response to your assertion that it’s still not working, I’m afraid I have to beg to differ. The following transcript (with tabs) shows that it works as advertised.

    pax> cat qq.in
    Excellent   29      54      47      46      38      22      50
    Good        7       14      27      24      26      36      20
    
    pax> awk -F'\t' '{ for (i=2; i<=NF; i++) print $1 "\t" $i; }' qq.in
    Excellent   29
    Excellent   54
    Excellent   47
    Excellent   46
    Excellent   38
    Excellent   22
    Excellent   50
    Good        7
    Good        14
    Good        27
    Good        24
    Good        26
    Good        36
    Good        20
    

    If it’s not actually working in your environment, that’s a different issue. You may have a buggy awk or any other number of reasons why it would fail.

    For a start, figure out what version of awk and the operating system you’re using, such as with:

    awk --version
    uname -a
    

    (a): It does actually output something, but almost certainly not what you’d expect. Let’s actually look at what happens in reality. Consider the following transcript which is similar to your original:

    pax> ( echo 1; echo 2; echo 3 ) | while read line ; do
    ...>     awk '{print "[" $0 "]"}'
    ...> done
    [2]
    [3]
    

    Now that looks rather strange, it appears to be throwing away the first line.

    The reason for this is the disconnect between the while and the awk. The while reads the first line from standard input and assigns it to $line, then executes the body of the do..done section.

    That body is an awk with no input file, hence it takes its input from standard input!

    That means it will “suck up” the rest of your standard input stream and process it.

    Then, it will return to the while loop but, with no more data on standard input, it will finish. It’s perhaps better illustrated with:

    pax> ( echo 1; echo 2; echo 3 ) | while read line ; do
    ...>     echo "read: $line"
    ...>     awk '{print "awk:  " $0}'
    ...> done
    read: 1
    awk:  2
    awk:  3
    

    If you actually connect the while and the awk with the echo "$line" | section below, you’ll see it works properly:

    pax> ( echo 1; echo 2; echo 3 ) | while read line ; do
    ...>     echo "$line" | awk '{print "[" $0 "]"}'
    ...> done
    [1]
    [2]
    [3]
    

    Of course, it makes little sense to break apart your input into lines and send them to awk one at a time, when awk is perfectly capable of handling multiple lines one at a time on its own.

    So the single-line awk command shown in the first code block of this answer is still a better way to do it.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a malformed tab delimited csv file Name AA BB CC AA BB
I need to read from a CSV/Tab delimited file and write to such a
I'm using the CSV module to read a tab delimited file. Code below: z
I'm looking at my delimited-file (e.g. CSV, tab seperated, etc.) parsing options based on
Fellow Overflowers, I have a tab delimited csv file which contains dates in this
I have a large tab-delimited csv file with the following format: #mirbase_acc mirna_name gene_id
So I want to convert a simple tab delimited text file into a csv
I'm trying to read in a (tab separted) csv file in R. When I
I have .csv file that contain 2 columns delimited with , . file.csv word1,word2
I read in a text file that is tab delimited, i then have a

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.