selective concatenation of rows & columns


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers selective concatenation of rows & columns
# 1  
Old 04-13-2009
selective concatenation of rows & columns

Dear unix gurus,

I have a data file that looks something like this ...

Code:
 
   x   y      x    y      x    y      x    y      x    y
   0  3836    30  3915    60  5984    90  7388   120  8385
   150  9038   180  9453   210  9745   240  9906   270  9962
   300  9953   330  9915   350  9887   400  9726   450  9528
   500  9300   600  8873   700  8471   800  8066   900  7670
  1000  7311  1100  6952  1200  6605  1300  6303  1400  5997
  1500  5704  1600  5435  1700  5176  1800  4930  1900  4698
  2000  4477  2100  4255  2200  4060  2300  3870  2400  3688
  2500  3520  2600  3353  2700  3205  2800  3055  2900  2914
  3000  2789

What I would like to do is to have two columns of data , i.e one column of x and one of y such that ....

Code:
 
x     y
0    3836
30   3915
60   5984
90   7388
120  8385
150  9038
180  9453
 ...    ...
3000 2789

Can anyone help? Thanks
# 2  
Old 04-13-2009
Remove x,y from file then run

awk '{ for(i=1;i<=NF;i++){
printf $i " "
printf $(i+1)
i++
print ""
}
}' filename
# 3  
Old 04-13-2009
Quote:
Originally Posted by krabu
Remove x,y from file then run

awk '{ for(i=1;i<=NF;i++){
printf $i " "
printf $(i+1)
i++
print ""
}
}' filename
you can shrink it toSmilie
Code:
 
awk 'BEGIN{print "x y"}NR>1{for(i=1;i<=NF;i++){print $i" "$(i+1);i++}}' filename

# 4  
Old 04-13-2009
i am new to shell scripting Smilie.
# 5  
Old 04-13-2009
Quote:
Originally Posted by krabu
i am new to shell scripting Smilie.
ok no problem you will learn these little tricks later keep up the good work Smilie
# 6  
Old 04-13-2009

Code:
{
 read
 echo "   x     y"
 printf "%4s %4s\n" $( cat )
} < "$FILE"

# 7  
Old 04-13-2009
Thanks guys!

The solutions by Krabu and Vidyadhar85 worked nicely. The x and y I put in for clarity, the original datafile contains only numbers. your solutions works for either case.

The solution by cfaJohnson...I couldn't get to work. It just hangs waiting for an input from me. Please could you rewrite it assuming that the letters x , y do not exist in the inputfile and that I would like the output piped to an "outputfile"? thanks.
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Rows to columns

trying to sort an array, only the 4th column is uniq, have this: dog cat bird house1 dog cat bird house2 dog cat bird house3 rose daisy tulip house1 rose daisy tulip house3 would like this: dog cat bird house1 house2 house3 rose daisy tulip house1 missing house3 any help... (6 Replies)
Discussion started by: jimmyf
6 Replies

2. UNIX for Dummies Questions & Answers

Merge selective columns from files based on common key

Hi, I am trying to selectively merge two files based on keys reported in the 1st column. File1: #file1-header1 file1-header2 111 qwe rtz uio 198 asd fgh jkl 165 yxc 789 poi uzt rew 89 lkj File2: #file2-header2 file2-header2 165 ghz nko2 ... (2 Replies)
Discussion started by: dovah
2 Replies

3. Shell Programming and Scripting

Compare 2 csv files by columns, then extract certain columns of matcing rows

Hi all, I'm pretty much a newbie to UNIX. I would appreciate any help with UNIX coding on comparing two large csv files (greater than 10 GB in size), and output a file with matching columns. I want to compare file1 and file2 by 'id' and 'chain' columns, then extract exact matching rows'... (5 Replies)
Discussion started by: bkane3
5 Replies

4. Shell Programming and Scripting

Columns and rows

echo "Month" echo "Feb_2014" echo "March_2014" echo "Apr_2014" echo "No of files" grep ,y, Feb_2014.csv |wc -l grep ,y, Mar_2014.csv |wc -l grep ,y, Apr_2014.csv |wc -l echo "Total no of success" awk -F"," '{x+=$4}END{print x}' Feb_2014.csv awk -F"," '{x+=$4}END{print x}'... (3 Replies)
Discussion started by: shelllearner
3 Replies

5. Shell Programming and Scripting

Evaluate 2 columns, add sum IF two columns match on two rows

Hi all, I know this sounds suspiciously like a homework course; but, it is not. My goal is to take a file, and match my "ID" column to the "Date" column, if those conditions are true, add the total number of minutes worked and place it in this file, while not printing the original rows that I... (6 Replies)
Discussion started by: mtucker6784
6 Replies

6. Shell Programming and Scripting

Deleting all the fields(columns) from a .csv file if all rows in that columns are blanks

Hi Friends, I have come across some files where some of the columns don not have data. Key, Data1,Data2,Data3,Data4,Data5 A,5,6,,10,, A,3,4,,3,, B,1,,4,5,, B,2,,3,4,, If we see the above data on Data5 column do not have any row got filled. So remove only that column(Here Data5) and... (4 Replies)
Discussion started by: ks_reddy
4 Replies

7. Shell Programming and Scripting

Selective multiplication of two columns in two files

Hi again, I have two files e.g. file2 e.g. file1 BB152 6.279650E+02 AA124 6.279650E+02 AA124 6.0273E-01 9.7800E-01 AA124 6.3239E-01 9.7800E-04 AA124 6.4585E-01 7.3839E-02 BB152 6.6250E-01 2.4450E-04 BB152 7.0932E-01 1.3496E-02 CC124 7.1378E-01 2.2690E-02 CC124 7.2279E-01... (8 Replies)
Discussion started by: f_o_555
8 Replies

8. Shell Programming and Scripting

Perl script to get info from specific rows & columns (.xls file)

Hi all, I want to read some specific rows & columns in the .xls file with my script to get the data to be manipulated. Now, I can read the .xls file correctly & i can go to the specific sheet that I want but i have a problem to specify the specific rows & columns. I mean, I want to get the info... (0 Replies)
Discussion started by: Yohannita
0 Replies

9. UNIX for Dummies Questions & Answers

Search for & edit rows & columns in data file and pipe

Dear unix gurus, I have a data file with header information about a subject and also 3 columns of n rows of data on various items he owns. The data file looks something like this: adam peter blah blah blah blah blah blah car 01 30 200 02 31 400 03 57 121 .. .. .. .. .. .. n y... (8 Replies)
Discussion started by: tintin72
8 Replies

10. Shell Programming and Scripting

deleting rows & columns form a csv file

Hi , I want to delete some rows & columns from file. can someone please help me on this? Regards. (2 Replies)
Discussion started by: code19
2 Replies
Login or Register to Ask a Question