Printing every alternate columns in a text file


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Printing every alternate columns in a text file
# 1  
Old 12-05-2014
Printing every alternate columns in a text file

Hi,

I have a big matrix with 1 million columns and 21 rows, which is a tab delimited file. I would like to print every alternate columns into a new file. For each row, the start column will be always the first column.

For example the input file
Code:
1 2 4 5 8 0 7 9
2 5 6 3 0 6 9 2
3 6 3 6 6 0 2 1

the desired output

Code:
1 4 8 7
2 6 0 9
3 3 6 2

Please let me know how to do it in awk. I tried the following, but didn't work
Code:
awk -F'\t' '{ for (i=1;i<=NF;i+=2) print $i }' input.txt > output.txt

# 2  
Old 12-05-2014
Quote:
Originally Posted by Kanja
Hi,

I have a big matrix with 1 million columns and 21 rows, which is a tab delimited file. I would like to print every alternate columns into a new file. For each row, the start column will be always the first column.

For example the input file
Code:
1 2 4 5 8 0 7 9
2 5 6 3 0 6 9 2
3 6 3 6 6 0 2 1

the desired output

Code:
1 4 8 7
2 6 0 9
3 3 6 2

Please let me know how to do it in awk. I tried the following, but didn't work
Code:
awk -F'\t' '{ for (i=1;i<=NF;i+=2) print $i }' input.txt > output.txt

Please do a google site search before posting. It saves mutual times.

Code:
awk '{for(x=1;x<=NF;x++)if(x % 2)printf "%s", $x (x == NF || x == (NF-1)?"\n":" ")}'

# 3  
Old 12-05-2014
You weren't too far off:
Code:
awk  '{for (i=1;i<=NF;i+=2) printf "%s ", $i; printf "\n" }' file
1 4 8 7 
2 6 0 9 
3 3 6 2

If you want <TAB> separators, it gets a bit more tedious:
Code:
awk  '{DL=""; for (i=1;i<=NF;i+=2) {printf "%s%s", DL, $i; DL="\t"}; printf "\n" }' file
1    4    8    7
2    6    0    9
3    3    6    2

This User Gave Thanks to RudiC For This Post:
# 4  
Old 12-05-2014
Alternatively, with GNU sed:
Code:
sed -r 's/\t[^\t]*(\t|$)/\1/g' file


--
or in this case of course:
Code:
cut -f1,3,5,7 file

With 21 columns that would become:
Code:
cut -f1,3,5,7,9,11,13,15,17,19,21 file


Last edited by Scrutinizer; 12-05-2014 at 09:03 PM..
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Print every alternate column in row in a text file

Hi, I have a comma separated file. I would like to print every alternate columns into a new row. Example input file: Name : John, Age : 30, DOB : 30-Oct-2018 Example output: Name,Age,DOB John,30,30-Oct-2018 (3 Replies)
Discussion started by: Lini
3 Replies

2. Shell Programming and Scripting

Bash script - printing range of lines from text file

I'm working on a new exercise that calls for a script that will take in two arguments on the command line (representing the range of line numbers) and will subsequently print those lines from a a specified file. Command line would look like this: ./lines_script.bash 5 15 <file.txt. The script would... (8 Replies)
Discussion started by: ksmarine1980
8 Replies

3. Shell Programming and Scripting

Modify blocks of text by printing missing columns

Hi Experts, I have a problem where I want to print missing columns (3,4) within a block of text. Each block is separated by "###". Some rows have missing column 3 and 4 which should be same as the previous value in column 3 and 4. The file is space delimited. For example: INPUT ###... (5 Replies)
Discussion started by: mira
5 Replies

4. Shell Programming and Scripting

How to concatenate 2-columns by 2 -columns for a text file?

Hello, I want to concatenate 2-columns by 2-columns separated by colon. How can I do so? For example, I have a text file containing 6 columns separated by tab. I want to concatenate column 1 and 2; column 3 and 4; column 5 and 6, respectively, and put a colon in between. input file: 1 0 0 1... (10 Replies)
Discussion started by: huiyee1
10 Replies

5. Shell Programming and Scripting

Printing multiple columns from a file

Hi, I need just to print the values of second and fourth column from a file # cat dispaly id Name Std Specialist 1 sss X mathematics 2 uyt IX geography 3 vcd X English i tried with some NF command.. I think am wrong.. Is there anyother way to print my requirement (3 Replies)
Discussion started by: Priya Amaresh
3 Replies

6. UNIX for Dummies Questions & Answers

Removing columns from a text file that do not have any values in second and third columns

I have a text file that has three columns. But at the end of the text file, there are trailing lines that have missing second and third columns: 4 0.04972604 KLHL28 4 0.0497332 CSTB 4 0.04979822 AIF1 4 0.04983331 DECR2 4 0.04990344 KATNB1 4 4 4 4 How can I remove the trailing... (3 Replies)
Discussion started by: evelibertine
3 Replies

7. UNIX for Dummies Questions & Answers

Comparing the 2nd column in two different files and printing corresponding 9th columns in new file

Dear Gurus, I am very new to UNIX. I appreciate your help to manage my files. I have 16 files with equal number of columns in it. Each file has 9 columns separated by space. I need to compare the values in the second column of first file and obtain the corresponding value in the 9th column... (12 Replies)
Discussion started by: Unilearn
12 Replies

8. UNIX for Dummies Questions & Answers

How to convert text to columns in tab delimited text file

Hello Gurus, I have a text file containing nearly 12,000 tab delimited characters with 4000 rows. If the file size is small, excel can convert the text into coloumns. However, the file that I have is very big. Can some body help me in solving this problem? The input file example, ... (6 Replies)
Discussion started by: Unilearn
6 Replies

9. Shell Programming and Scripting

Comparing Columns and printing the difference from a particular file

Gurus, I have one file which is having multiple columns and also this file is not always contain the exact columns; sometimes it contains 5 columns or 12 columns. Now, I need to find the difference from that particular file. Here is the sample file: param1 | 10 | 20 | 30 | param2 | 10 |... (6 Replies)
Discussion started by: buzzusa
6 Replies

10. Shell Programming and Scripting

read input from 2 files and print them in alternate columns

Hi Frnz Please help me out. I have two text files. A.txt one two three four B.txt Jan Feb Mar Apr I need the output as (1 Reply)
Discussion started by: sriram.s
1 Replies
Login or Register to Ask a Question