deleting rows that dont have a certain # of columns


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting deleting rows that dont have a certain # of columns
# 1  
Old 12-02-2009
deleting rows that dont have a certain # of columns

Hi,

I want to delete rows that dont have a certain # of columns. In my case, rows that are less than 8 should be removed (those greater than 8 are ok).

For instance:
Code:
1  2  3  4  5  6  7  8
2  3  2  4  3  2  1  5
1  2  3  4  5  6  8
2  2  4  3  1
1  1  1  1  1  1  1  1


after:

Code:
1  2  3  4  5  6  7  8
2  3  2  4  3  2  1  5
1  1  1  1  1  1  1  1

thanks
# 2  
Old 12-02-2009
Code:
awk 'NF>7' oldfile > newfile

# 3  
Old 12-02-2009
thanks

what if the values were on a string instead of in columns

eg.

Code:
# 123P5678
# 334434U3
# 23H1233

lets say i want to remove anything less than 8 on column 2

so the end result would be:

Code:
# 123P5678
# 334434U3

# 4  
Old 12-02-2009
Code:
awk 'length($0)>7' infile > outfile

or 

awk 'NF>7' FS= infile > outfile

Use gawk, nawk or /usr/xpg4/bin/awk on Solaris.

Last edited by danmero; 12-02-2009 at 07:02 PM.. Reason: add note
# 5  
Old 12-02-2009
Code:
awk 'length($2)>7'

# 6  
Old 12-02-2009
shell
Code:
while read -r line
do
    set -- $line
    if [ "$#" -ge 8 ];then
        echo $line
    fi    
done <"file"

# 7  
Old 12-02-2009
Try :

Code:
sed -n '/^.\{1,7\}$/p' file

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. 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

2. 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

3. 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

4. Shell Programming and Scripting

deleting rows under a certain condition

there are 20 variables and I would like to delete the rows if 13th-20th columns are all NA. Thank you! FID IID aspirpre statihos fibrahos ocholhos arbhos betabhos alphbhos cacbhos diurehos numbcig.x toast1 toast2 toast3 toast4 ischoth1 ischoth2 ischoth3 ischoth4 101 101 1 1 1 1 1 2 1 2... (2 Replies)
Discussion started by: johnkim0806
2 Replies

5. UNIX for Advanced & Expert Users

Help in Deleting columns and Renaming Mutliple columns in a .Csv File

Hi All, i have a .Csv file in the below format startTime, endTime, delta, gName, rName, rNumber, m2239max, m2239min, m2239avg, m100016509avg, m100019240max, metric3min, m100019240avg, propValues 11-Mar-2012 00:00:00, 11-Mar-2012 00:05:00, 300.0, vma3550a, a-1_CPU Index<1>, 200237463, 0.0,... (9 Replies)
Discussion started by: mahi_mayu069
9 Replies

6. UNIX for Dummies Questions & Answers

Deleting all rows with empty columns

I have a text file that looks like this: 1 rs523634 8.22486 1 1 rs585160 8.22488 1 rs497228 8.2249 1 1 rs600933 8.225 1 rs480106 8.22531 1 rs600199 8.22533 1 rs529015 8.22534 1 rs598894 8.22534 I want to delete the rows with empty... (2 Replies)
Discussion started by: evelibertine
2 Replies

7. Shell Programming and Scripting

Extract columns from a file if the name dont exist put blank

Hi, I am very new to Unix script. Suppose i have a file with column header: NAME1 NAME2 Address Tel And I always need to make a file with column header: ID NAME1 NAME2 EMail Address Tel For the columns that do not exist in the file, I would still like to make a column with blank. ... (11 Replies)
Discussion started by: nightrider
11 Replies

8. Shell Programming and Scripting

Deleting specific rows in large files having rows greater than 100000

Hi Guys, I need help in modifying a large text file containing more than 1-2 lakh rows of data using unix commands. I am quite new to the unix language the text file contains data in a pipe delimited format sdfsdfs sdfsdfsd START_ROW sdfsd|sdfsdfsd|sdfsdfasdf|sdfsadf|sdfasdf... (9 Replies)
Discussion started by: manish2009
9 Replies

9. Shell Programming and Scripting

deleting rows that dont have ....

Hi I posted earlier. This is sorta similar but I want to delete rows that dont have R, T, Y or U. Nam1 RTYU Nam2 RRTT Nam3 RYTU Nam4 IRTT So the output would look like this? Nam1 RTYU Nam2 RRTT Nam3 RYTU too many problems thanks (3 Replies)
Discussion started by: kylle345
3 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