How to remove a line if column 1 does not contain any value?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting How to remove a line if column 1 does not contain any value?
# 1  
Old 09-24-2012
Tools How to remove a line if column 1 does not contain any value?

Input File
PHP Code:
            science        maths
tom                2        3
peter                       5
rodger             5        7

Desired Ouput

            science        maths
tom                2        3
rodger             5        7 
Here Peter does not have any value for column Science, how can we delete Peter's row.
assuming this is abc.txt file; and am using bash shell.

Last edited by vishalgoyal; 09-24-2012 at 07:09 AM.. Reason: new user, trying to put data in code tags. (code not php...)
# 2  
Old 09-24-2012
How are we supposed to tell that peter 5 should be suppressed? Col 1 DOES contain value "peter". Pls be more specific and put your input and output data into code tags.
# 3  
Old 09-24-2012
Can u post the delimiter for the file

---------- Post updated at 05:28 AM ---------- Previous update was at 05:06 AM ----------

If it is tab pipe delimiter use this one
Code:
 awk -F\| 'length($2)>0{print $0 >"above.txt";next}' test1.txt

# 4  
Old 09-24-2012
Assuming a <TAB> delimiter, try
Code:
$ awk  '$2' FS="\t" test
          science    maths
tom       2    3
rodger    5    7

# 5  
Old 09-24-2012
With sed:
Code:
sed -n '/.              ./!p' <abc.txt

Between the two points, you've to insert two consecutive TABs.
To make a single TAB, you press [CTRL]+[V] and then [TAB].
--
Bye
# 6  
Old 09-24-2012
Quote:
Originally Posted by RudiC
Assuming a <TAB> delimiter, try
Code:
$ awk  '$2' FS="\t" test
          science    maths
tom       2    3
rodger    5    7

That would also leave out lines with "0" in the second field, since awk would interpret that in numerical context. This can be avoided by forcing a string context, for example:
Code:
awk  'x$2' FS='\t' file

# 7  
Old 09-24-2012
Assuming as per the input provided....Smilie

Code:
awk 'NR == 1 || NF >= max{ print;max=NF}' file

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Remove entire line from a file if 1st column matches a pattern

I have one requirement to delete all lines from a file if it matches below scenario. File contains three column. Employee Number, Employee Name and Employee ID Scenario is: delete all line if Employee Number (1st column) contains below 1. Non-numeric Employee Number 2. Employee Number that... (3 Replies)
Discussion started by: anshu ranjan
3 Replies

2. Shell Programming and Scripting

How to remove a line based on contents of the first column?

Good day all. Using basic UNIX/Linux tools, how would you delete a line based on a character found in column 1? For example, if the CITY name contains an 'a' or 'A', delete the line: New York City; New York Los Angeles; California Chicago; Illinois Houston; Texas Philadelphia;... (3 Replies)
Discussion started by: BRH
3 Replies

3. Shell Programming and Scripting

Remove character from a column in each line

Hi, I am a newbie to shell scripting (.sh). Please guide me on how to do the below issue. My input file has below data. I want to remove $ sysmbol from the fourth column of each line. (ie, between 4th and 5th pipe symbol) ABC25160|51497|06/02/2010|$32,192.07|MARK|$100|A... (3 Replies)
Discussion started by: rsreejithmenon
3 Replies

4. Shell Programming and Scripting

1st column,2nd column on first line 3rd,4th on second line ect...

I need to take one column of data and put it into the following format: 1st line,2nd line 3rd line,4th line 5th line,6th line ... Thanks! (6 Replies)
Discussion started by: batcho
6 Replies

5. Shell Programming and Scripting

compare files and remove a line from a file if first column is greater than 25

my files are as follows fileA sepearated by tab /t 00 lieferungen 00 attractiop 01 done 02 forness 03 rasp 04 alwaysisng 04 funny 05 done1 fileB funnymou120112 funnymou234470 mou3raspnhdhv rddfgmoudone1438748 so all those record which are greater than 3 and which are not... (4 Replies)
Discussion started by: rajniman
4 Replies

6. Shell Programming and Scripting

Remove line feed from csv file column

Hi All, i have a csv file . In the 7th column i have data that has line feed in it. Requirement is to remove the line feed from the 7th column whenever it appears There are 11 columns in the file C1,C2,C3,C4,C5,C6,C7,C8,C9,C10,C11 The value in C7 contains line feed ( Alt + Enter ),... (2 Replies)
Discussion started by: r_t_1601
2 Replies

7. Shell Programming and Scripting

Remove line feed from csv file column

Hi All, My requirement is to remove line (3 Replies)
Discussion started by: r_t_1601
3 Replies

8. Shell Programming and Scripting

awk : Remove column1 and last column in a line

Hi All, How to remove col1 and last column in a line. Please suggest some awk stuffs. Input col1 col2 col3 col4 col1 col2 col3 col4 col5 col1 col2 col3 col4 col1 col2 col3 Output Processing col2 col3 ... Processing col2 col3 col4 ... Processing col2 col3 ... Processing... (5 Replies)
Discussion started by: k_manimuthu
5 Replies

9. Shell Programming and Scripting

Remove duplicate line detail based on column one data

My input file: AVI.out <detail>named as the RRM .</detail> AVI.out <detail>Contains 1 RRM .</detail> AR0.out <detail>named as the tellurite-resistance.</detail> AWG.out <detail>Contains 2 HTH .</detail> ADV.out <detail>named as the DENR family.</detail> ADV.out ... (10 Replies)
Discussion started by: patrick87
10 Replies

10. Shell Programming and Scripting

remove new line characters from a partcular column data

Dear friends, I have a pipe delimited file having 5 columns. However the column no-3 is having extra new line characters as the data owing to owing , I am having issues. Ideally my file should have only newline termination at the end of each record and not within column data of any of... (1 Reply)
Discussion started by: sureshg_sampat
1 Replies
Login or Register to Ask a Question