How to print lines that have values in certain columns ?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting How to print lines that have values in certain columns ?
# 1  
Old 04-14-2017
How to print lines that have values in certain columns ?

Hi, everyone
I have a dataset like this:
Code:
A B C D 
A C 
  C D E
F G H
  F D K
  Y
  X A
K K C G

some of columns have no values in each line. I want to print all lines that have 1/2/3/4 values, export separately to four files. What I expected is like this:
file1
Code:
Y

file 2
Code:
A C
X A

file 3
Code:
C D E
F D K

file 4
Code:
A B C D
K K C G

It's not necessarily to merge the command, I can do it individually if the pattern is the same.
Thanks very much.
# 2  
Old 04-14-2017
Code:
$ 
$ cat dataset_1.txt
A B C D 
A C 
  C D E
F G H
  F D K
  Y
  X A
K K C G
$ 
$ awk '{$1=$1; print>"file"NF}' dataset_1.txt
$ 
$ cat file1
Y
$ 
$ cat file2
A C
X A
$ 
$ cat file3
C D E
F G H
F D K
$ 
$ cat file4
A B C D
K K C G
$ 
$

This User Gave Thanks to durden_tyler For This Post:
# 3  
Old 04-14-2017
Quote:
Originally Posted by durden_tyler
Code:
$ 
$ cat dataset_1.txt
A B C D 
A C 
  C D E
F G H
  F D K
  Y
  X A
K K C G
$ 
$ awk '{$1=$1; print>"file"NF}' dataset_1.txt
$ 
$ cat file1
Y
$ 
$ cat file2
A C
X A
$ 
$ cat file3
C D E
F G H
F D K
$ 
$ cat file4
A B C D
K K C G
$ 
$

wow, that's great, very appreciated. Smilie
# 4  
Old 04-14-2017
That will work with some versions of awk and give you a syntax error with other versions of awk. The following should work with almost any version of awk:
Code:
awk '{$1=$1; print>("file"NF)}' dataset_1.txt

These 3 Users Gave Thanks to Don Cragun For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Print values within groups of lines with awk

Hello to all, I'm trying to print the value corresponding to the words A, B, C, D, E. These words could appear sometimes and sometimes not inside each group of lines. Each group of lines begins with "ZYX". My issue with current code is that should print values for 3 groups and only is... (6 Replies)
Discussion started by: Ophiuchus
6 Replies

2. Shell Programming and Scripting

Selecting lines having same values for first two columns

Hello to all. This is first post. Kindly excuse me if I do not adhere to any rules and regulations of this forum. I have a file containing some rows with three columns each per row(separeted by a space). There are certain rows for which first two columns have same value but the value in... (6 Replies)
Discussion started by: manojmalhotra13
6 Replies

3. Shell Programming and Scripting

Print columns matching to specific values

Hello Friends, I have a CDR file and i need to print out 2 columns with their field position which matches to some constant values, a part of input file CZ=1|CZA=1|DIAL=415483420001|EE=13|ESF=1|ET=|FF=0|9|MNC=99|MNP=9041|MTC=0|NID=2|NOA=international|ON=1| OutPut ... (3 Replies)
Discussion started by: EAGL€
3 Replies

4. UNIX for Dummies Questions & Answers

[Solved] Print a line using a max and a min values of different columns

Hi guys, I already search on the forum but i can't solve this on my own. I have a lot of files like this: And i need to print the line with the maximum value in last column but if the value is the same (2 in this exemple for the 3 last lines) i need get the line with the minimum value in... (4 Replies)
Discussion started by: MetaBolic0
4 Replies

5. Shell Programming and Scripting

Print lines containing same values in a group

Hi, I have a table like this: Name A1 A2 A3 B1 B2 B3 111 abc abc abc cbc cbc cbc 222 acv acv n_n bbc bbc bbc 333 bvv bvv bvv cBx ccx ccx 444 ttk ttk ttk kke kke kke 555 mcr mcr mcr mcr mcr mcr The 1st column is just names... (3 Replies)
Discussion started by: polsum
3 Replies

6. UNIX for Dummies Questions & Answers

Only print lines with 3 numeric values

Hey guys & gals, I am hoping for some advice on a sed or awk command that will allow to only print lines from a file that contain 3 numeric values. From previous searches here I saw that ygemici used the sed command to remove lines containing more than 3 numeric values ; however how... (3 Replies)
Discussion started by: TAPE
3 Replies

7. Shell Programming and Scripting

how to retrieve lines that the first 4 columns have different values

Hi, all: I am not familiar with unix,and just started awk scripts. I want to retrieve lines that have the first 4 columns with different values. For example, the input is like this (tab delimited file with one header) r1 A A A A x r2 A B B A x r3 B B B B x the output should be (header is... (15 Replies)
Discussion started by: new2awkin2011
15 Replies

8. Shell Programming and Scripting

check values in columns and print certain error messages if value > 10, 40 , 60

Hi Experts, I need your assistance to check values in certain columns (5 and 8 %Err) from awk o/p and if the value is > 10 print "<Minor>: <complete row>" , if value > 40 then print "<Major>: <complete row> and If value > 60 then print "<Critical>: <complete row>". and separates the repetition... (3 Replies)
Discussion started by: Dendany83
3 Replies

9. Shell Programming and Scripting

perl script to print to values in rows and columns

Hi guys I want to print the values by using this script but its giving the no of rows and columns as input instead of values Would you plz help me on this FILE- chr1.txt 1981 1 1971 1 1961 1 1941 1 perl script #!/usr/bin/perl -w $infile1 = 'chr1.txt'; $outfile3 = 'out3.txt'; ... (3 Replies)
Discussion started by: nogu0001
3 Replies

10. UNIX for Dummies Questions & Answers

Can I search columns and print lines?

Hi. First post here. I'm relatively new to UNIX (Solaris8) and writing my first real script, but I seem to have hit a brick wall. The odd thing is that it seems like it should be a simple task....but not with UNIX apparently. I've got a large text file: 14000 lines with 10 columns. I want... (2 Replies)
Discussion started by: Ant1815
2 Replies
Login or Register to Ask a Question