Always output 6 columns regardless of search results


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Always output 6 columns regardless of search results
# 1  
Old 06-02-2014
Always output 6 columns regardless of search results

If I am searching for AA then then BB in a loop, how do I make the output always contain 6 columns of comma separated data even when there may only be 4 search matches?
Code:
AA11
AA12
AA13
AA14
BB11
BB12
BB13
BB14
BB15
BB16

Final output:
Code:
AA11,AA12,AA13,AA14,,,
BB11,BB12,BB13,BB14,BB15,BB16

Thanks for any help!

Last edited by Franklin52; 06-02-2014 at 09:55 AM.. Reason: Please use code tags
# 2  
Old 06-02-2014
Just to clarify, you definitely want 6 columns, not 7.
Your output for the AA?? implies 7 columns...
Your output for the BB?? is 6...
# 3  
Old 06-02-2014
A loop in what? Shell script, awk program, etc?
# 4  
Old 06-02-2014
I need 6 columns regardless of the search results using a UNIX Bash Shell script.

---------- Post updated at 09:46 AM ---------- Previous update was at 09:46 AM ----------

Thanks in advance!
# 5  
Old 06-02-2014
Try this:
Code:
awk     '                       {TMP=substr($1,1,2)}
         NR>1 && LAST!=TMP      {printf "%s", PR
                                 while (CNT++ < 6) printf ","
                                 printf "\n"
                                 PR=DEL=""; CNT=0}
                                {PR=PR DEL $0; DEL=","; CNT++; LAST=TMP}
         END {print PR}
        ' file
AA11,AA12,AA13,AA14,,
BB11,BB12,BB13,BB14,BB15,BB16

What about 7 elements?
This User Gave Thanks to RudiC For This Post:
# 6  
Old 06-02-2014
Sorry about that, I made a typo, I only needed 6. Let me give this a try! Thanks Smilie
# 7  
Old 06-02-2014
Code:
awk 'BEGIN {OFS = ","; n = 1}
  NR == 1 {T = $0;
  gsub(/[0-9]/, X);
  A[n++] = T;
  P = $0;
  next}
  {T = $0;
  gsub(/[0-9]/, X);
  if(P == $0 && n <=6)
    {A[n++] = T}
  else if (P != $0)
    {print A[1], A[2], A[3], A[4], A[5], A[6];
    n = 1;
    split("", A);
    A[n++] = T;
    P = $0}}
  END {print A[1], A[2], A[3], A[4], A[5], A[6];}' file

Login or Register to Ask a Question

Previous Thread | Next Thread

6 More Discussions You Might Find Interesting

1. What is on Your Mind?

Search Results (Search, New, and Today's Topics) Animation Switch

Hey, I added an animation switch on the search results page; so by default the thread previews are off, but if you want to look at them, just click on the green button and the thread previews will turn on (and back off). See image and attached animation: ... (1 Reply)
Discussion started by: Neo
1 Replies

2. Shell Programming and Scripting

Finding files with the name of the results of another search

Dear All, I have a file with this name= xx-nnnn.csv , I has texts in this format, 231048975938093056;234317862284705793;609384034;14955353;1344700706000;1; 231048975938093056;234317958632054785;715450794;52422878;1344700729000;1;... (10 Replies)
Discussion started by: davidfreed
10 Replies

3. Shell Programming and Scripting

Can ctag and cscope support recording search results and displaying the history results ?

Hello , When using vim, can ctag and cscope support recording search results and displaying the history results ? Once I jump to one tag, I can use :tnext to jump to next tag, but how can I display the preview search result? (0 Replies)
Discussion started by: 915086731
0 Replies

4. Shell Programming and Scripting

awk - Matching columns between 2 files and reordering results

I am trying to match 4 colums (first_name,last_name,dob,ssn) between 2 files and when there is an exact match I need to write out these matches to a new file with a combination of fields from file1 and file2. I've managed to come up with a way to match these 2 files based on the columns (see below)... (7 Replies)
Discussion started by: ambroze
7 Replies

5. Shell Programming and Scripting

AWK - no search results

Hi all, I'm new to awk and I'm experiencing syntax error that I don't know how to resolve. Hopefully some experts in this forum can help me out. I created an awk file that look like this: $ cat myawk.awk BEGIN { VAR1=PATTERN1 VAR2=PATTERN2 } /VAR1/ { flag=1 } /VAR2/ { flag=0 } {... (7 Replies)
Discussion started by: hk18
7 Replies

6. Shell Programming and Scripting

Operations on search results

Hi, I am a newbie at Unix scritping, and I have a question. Looking at the search functionality on Unix. Here I have a structure root---------dir1 ------- file1, file2, file3 |_____dir2 ______file1@, file4 |_____dir3_______file1@, file5 Under root directory, I... (4 Replies)
Discussion started by: nj302
4 Replies
Login or Register to Ask a Question