Split column data if the table has n number of column's with some record


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Split column data if the table has n number of column's with some record
# 1  
Old 10-20-2015
Split column data if the table has n number of column's with some record

Split column data if the table has n number of column's with some record then how to split n number of colmn's line by line with records

Code:
Table
---------
Col1 col2 col3 col4 ....................col20 
1     2    3    4   .................... 20
a     b    c    d   .................... v

Code:
Output Display
.....................
Col1 col2 col3 col4 ....................col10
1     2    3    4   .................... 10
a     b    c    d .......................j

display Coulmn along with all the record which coulmn 1 to 10 has contained
Code:
Col11 col2 col3 col4 ....................col20
11     12   13   14 .....................20
j      k     l    m  .....................v


Last edited by Don Cragun; 10-20-2015 at 08:36 AM.. Reason: Add CODE tags.
# 2  
Old 10-20-2015
Not sure I understand your problem. Please improve the spec. Any attempts from your side?
# 3  
Old 10-20-2015
Table
-------
Code:
1 2 3 a b c
3 4 5 c d e
7 8 9 f g h

Output Should display
-------
Code:
1 2 3 
3 4 5 
7 8 9 
a b c
c d e
f g h

Moderator's Comments:
Mod Comment Please use CODE tags when displaying sample input, sample output, and code segments.

Last edited by Don Cragun; 10-20-2015 at 10:27 AM.. Reason: Add CODE tags.
# 4  
Old 10-20-2015
The following shell script works with any amount of whitespace-delimited columns
Code:
#!/bin/sh
awk '
NR==1 {x=NF/2}
{
  line=$0
  if (NR==FNR) {
    sub("[[:space:]]+$","",line)
    for (i=NF; i>x; i--) sub("[[:space:]]+[^[:space:]]+$", "", line)
  } else {
    for (i=1; i<=x; i++) sub("[^[:space:]]+[[:space:]]+", "", line)
  }
  print line
}
' "$1" "$1"

The shell script takes the file name as parameter.
# 5  
Old 10-20-2015
Quote:
Originally Posted by MadeInGermany
The following shell script works with any amount of whitespace-delimited columns
Code:
#!/bin/sh
awk '


NR==1 {x=NF/2}
{
  line=$0
  if (NR==FNR) {
    sub("[[:space:]]+$","",line)
    for (i=NF; i>x; i--) sub("[[:space:]]+[^[:space:]]+$", "", line)
  } else {
    for (i=1; i<=x; i++) sub("[^[:space:]]+[[:space:]]+", "", line)
  }
  print line
}
' "$1" "$1"

The shell script takes the file name as parameter.
Sorry not explain properly
Table
Code:
col1 col2 col3 col4 col5 col6    
drwxr-xr-x 4096 Oct 12 07:51 Templates
drwxr-xr-x 4096 Oct 12 07:51 Public
drwxr-xr-x 4096 Oct 12 07:51 Pictures
drwxr-xr-x 4096 Oct 12 07:51 Music
drwxr-xr-x 4096 Oct 12 07:51 Downloads

output should be display col1 and col2 below col1 and col2
col3 col4 col5 col6 should be display.

Code:
col1 col2
drwxr-xr-x 4096 
drwxr-xr-x 4096 
drwxr-xr-x 4096 
drwxr-xr-x 4096 
drwxr-xr-x 4096 
col3 col4 col5 col6
Oct 12 07:51 Templates
Oct 12 07:51 Public
Oct 12 07:51 Pictures
Oct 12 07:51 Music
Oct 12 07:51 Downloads

---------- Post updated at 02:36 AM ---------- Previous update was at 02:09 AM ----------

can I add 3 different awk statement make a single awk statement first statement output using the second statement and second statement output using the third awk statement

Last edited by Don Cragun; 10-20-2015 at 07:09 PM.. Reason: code tags, please
# 6  
Old 10-20-2015
Quote:
Originally Posted by Priti2277
Sorry not explain properly
Table
Code:
col1 col2 col3 col4 col5 col6    
drwxr-xr-x 4096 Oct 12 07:51 Templates
drwxr-xr-x 4096 Oct 12 07:51 Public
drwxr-xr-x 4096 Oct 12 07:51 Pictures
drwxr-xr-x 4096 Oct 12 07:51 Music
drwxr-xr-x 4096 Oct 12 07:51 Downloads

output should be display col1 and col2 below col1 and col2
col3 col4 col5 col6 should be display.

Code:
col1 col2
drwxr-xr-x 4096 
drwxr-xr-x 4096 
drwxr-xr-x 4096 
drwxr-xr-x 4096 
drwxr-xr-x 4096 
col3 col4 col5 col6
Oct 12 07:51 Templates
Oct 12 07:51 Public
Oct 12 07:51 Pictures
Oct 12 07:51 Music
Oct 12 07:51 Downloads

---------- Post updated at 02:36 AM ---------- Previous update was at 02:09 AM ----------

can I add 3 different awk statement make a single awk statement first statement output using the second statement and second statement output using the third awk statement
Every post you have submitted has different requirements for which fields are to be printed on which lines.

There is no pattern I can see that tells us which input fields should be printed on which output lines for an input file that contains N fields. When there were 20 input fields, it appeared that you wanted fields 1-10 printed followed by field 11-20. When there were 6 input fields, first you said you wanted fields 1-3 printed followed by fields 4-6; but now when there are 6 input fields you want fields 1-2 printed followed by fields 3-6.

Please go back to the beginning and explain clearly in English how your script is supposed to determine how many sets of output lines are to be created for the input lines and which input fields are supposed to appear in each set of output lines. Without a clear specification of what you want (other than saying that every suggestion provided is not producing the right output, even though it exactly matches the sample output you said you wanted), we are all wasting our time.

AND, PLEASE use CODE tags when displaying sample input, sample output, and sample code segments.
# 7  
Old 10-20-2015
a little bit verbose, but should be a good starting point - hopefully I understood the reqs.
To get fields 1 and 2: awk -f priti.awk myFile
To get fields 3 and 5: awk -v fld='3 5' -f priti.awk myFile
where priti.awk is:
Code:
{
  if (!fld) fld="1 2"
  split(fld, t, FS)
  for(i=1;i in t;i++)
    fldA[t[i]]
}
{
  for(i=1; i<=NF;i++)
   if (i in fldA) printf("%s%s", $i, OFS)
   else
     recA[FNR]=(FNR in recA)?recA[FNR] OFS $i:$i
  printf ORS
  fnr=FNR
}
END {
  for(i=1;i<=fnr; i++)
    if (i in recA)
     print recA[i]
}

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

awk split columns to row after N number of column

I want to split this with every 5 or 50 depend on how much data the file will have. And remove the comma on the end Source file will have 001,0002,0003,004,005,0006,0007,007A,007B,007C,007E,007F,008A,008C Need Output from every 5 tab and remove the comma from end of each row ... (4 Replies)
Discussion started by: ranjancom2000
4 Replies

2. Shell Programming and Scripting

Split column data if the table has n number of column's

please write a shell script Table -------------------------- 1 2 3 a b c 3 4 5 c d e 7 8 9 f g h Output should be like this --------------- 1 2 3 3 4 5 7 8 9 a b c c d e f g h (1 Reply)
Discussion started by: Priti2277
1 Replies

3. Shell Programming and Scripting

Split column into two on first occurence of any number

Input : abc def 1 xyz zzz bca cde 2 yyy xxx Expected output : abc def |1 xyz zzz bca cde |2 yyy xxx I have tried the command below and losing the number. Any help is greatly appreciated 1. sed 's//|/' num.txt Result: abc def | xyz zzz bca cde |... (7 Replies)
Discussion started by: kbsuryadev
7 Replies

4. UNIX for Dummies Questions & Answers

Match sum of values in each column with the corresponding column value present in trailer record

Hi All, I have a requirement where I need to find sum of values from column D through O present in a CSV file and check whether the sum of each Individual column matches with the value present for that corresponding column present in the trailer record. For example, let's assume for column D... (9 Replies)
Discussion started by: tpk
9 Replies

5. Shell Programming and Scripting

Problem facing to compare different column and print out record with smallest number

Hi, Input file 1 : 37170 37196 77 51 37174 37195 73 52 37174 37194 73 53 Desired Output file 1 : 37170 37196 77 51 Input file 2 : 37174 37195 73 0 37170 37196 77 0 Desired Output file 2 : 37174 37195 73 0 (1 Reply)
Discussion started by: cpp_beginner
1 Replies

6. Shell Programming and Scripting

Problem to print out record got smallest number in specific column

Hi, Anybody know how to print out the record that shown smallest number among column 3 and column 4 Case 1 Input : 37170 37196 77 51 37174 37195 73 52 37174 37194 73 53 Case 1 Output : 37170 37196 77 51 Case 2 Input : 469613 469660 73 ... (4 Replies)
Discussion started by: cpp_beginner
4 Replies

7. Shell Programming and Scripting

awk to sum a column based on duplicate strings in another column and show split totals

Hi, I have a similar input format- A_1 2 B_0 4 A_1 1 B_2 5 A_4 1 and looking to print in this output format with headers. can you suggest in awk?awk because i am doing some pattern matching from parent file to print column 1 of my input using awk already.Thanks! letter number_of_letters... (5 Replies)
Discussion started by: prashob123
5 Replies

8. UNIX for Advanced & Expert Users

Data from table to column

Hi All, in bash I have a text file which is something like 7.96634E-07 1.0000 0.00000E+00 0.0000 0.00000E+00 0.0000 1.59327E-06 0.7071 2.23058E-05 0.1890 6.61207E-05 0.1098 1.13919E-04 0.0865 1.47377E-04 0.0747 .... .... 0.00000E+00 0.0000 0.00000E+00 0.0000 ... (6 Replies)
Discussion started by: f_o_555
6 Replies

9. Windows & DOS: Issues & Discussions

Split Data in one column into 2 column in Excel using DOS or VBScript

Hi I have some data in my Excel File.However all the data is in one single column.I want to split it into two columns. Current Data: 1,Hi Everyone,I am 7,New To Dos,And 17,VB Script,i could 110,have tried this thing 1800,in UNIX Desired Output CELL1|CELL 2 1 |Hi... (3 Replies)
Discussion started by: dashing201
3 Replies

10. Shell Programming and Scripting

Split single file into multiple files based on the number in the column

Dear All, I would like to split a file of the following format into multiple files based on the number in the 6th column (numbers 1, 2, 3...): ATOM 1 N GLY A 1 -3.198 27.537 -5.958 1.00 0.00 N ATOM 2 CA GLY A 1 -2.199 28.399 -6.617 1.00 0.00 ... (3 Replies)
Discussion started by: tomasl
3 Replies
Login or Register to Ask a Question