Delete line with sed or awk (with specified condition)


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Delete line with sed or awk (with specified condition)
# 1  
Old 01-17-2011
Delete line with sed or awk (with specified condition)

Hello. I'm trying to delete the lines of a file does not contain the letter "T " (for example) at position 26.

So far, I could only print the result:
Code:
awk '{if (substr ($ 1,1,26)! ~ / T /) print}' file.txt

How I can do to eliminate the lines that meet this condition?


Help please. Thanks.

Last edited by Franklin52; 01-18-2011 at 03:17 AM.. Reason: Please use code tags
# 2  
Old 01-17-2011
There are many many ways you could to this. With grep, for instace:
Code:
grep -v '^.........................T' inputfile

(there should be 25 ".")

With awk, I'd:
Code:
awk 'substr($0, 26, 1) != "T"' inputfile

I am sure others will come up with different methods.

Last edited by m.d.ludwig; 01-17-2011 at 04:00 PM.. Reason: missing space
This User Gave Thanks to m.d.ludwig For This Post:
# 3  
Old 01-17-2011
ready. I have it.
# 4  
Old 01-17-2011
Code:
# sed '/^.\{25\}\(T\).*/d' infile

# 5  
Old 01-17-2011
Quote:
delete the lines of a file does not contain the letter "T "
That means print the lines that do, doesn't it?
Code:
awk '$26=="T"' FS= file.txt

Code:
sed '/.\{25\}T/!d' file.txt

Code:
grep '.\{25\}T' file.txt

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

awk replace cells with NaN/delete if condition

Hello, I will like to delete/replace $3 with NaN. condition $3>-2000 (file1.dat) to produce out.dat. I want to retain the structure of the table. I use this code, this output only $3. Any idea on how to modify this code. Thank you. awk -v OFS='' '{for(i=1; i<=NF; i++) if ($i > -2000 || $i ==" >... (4 Replies)
Discussion started by: geomarine
4 Replies

2. Shell Programming and Scripting

If the value > 100 delete the line with awk or sed

if the value > 100 delete the line delay time: 42.978 ms delay time: 43.684 ms delay time: 41.082 ms delay time: 44.845 ms delay time: 40000.057 ms delay time: 41.158 ms delay time: 42.239 ms delay time: 50.579 ms delay time: 46.334 ms to (7 Replies)
Discussion started by: yanglei_fage
7 Replies

3. Shell Programming and Scripting

sed command to grep multiple pattern present in single line and delete that line

here is what i want to achieve.. i have a file with below contents cat fileName blah blah blah . .DROP this REJECT that . --sport 7800 -j REJECT --reject-with icmp-port-unreachable --dport 7800 -j REJECT --reject-with icmp-port-unreachable . . . more blah blah blah --dport 3306... (14 Replies)
Discussion started by: vivek d r
14 Replies

4. Shell Programming and Scripting

Need to delete all lines where any line meets a condition

Here is an example of a file... foo1,good foo1,good foo2,error foo2,good Note that both rows for foo1 have good in the 2nd field, but one of the foo2 rows has error... I need something in ksh/awk/perl that will delete ALL foo2 lines if ANY of them have error in the 2nd field...so: ... (7 Replies)
Discussion started by: dbiggied
7 Replies

5. Shell Programming and Scripting

sed or awk delete character in the lines before and after the matching line

Sample file: This is line one, this is another line, this is the PRIMARY INDEX line l ; This is another line The command should find the line with “PRIMARY INDEX” and remove the last character from the line preceding it (in this case , comma) and remove the first character from the line... (5 Replies)
Discussion started by: KC_Rules
5 Replies

6. Shell Programming and Scripting

Sed or Grep to delete line containing patter plus extra line

I'm new to using sed and grep commands, but have found them extremely useful. However I am having a hard time figuring this one out: Delete every line containing the word CEN and the next line as well. ie. test.txt blue 324 CEN green red blue 324 CEN green red blue to produce:... (2 Replies)
Discussion started by: rocketman88
2 Replies

7. UNIX for Dummies Questions & Answers

Using awk to get a line number to delete, piping through sed

Alright, I'm sure there's a more efficient way to do this... I'm not an expert by any means. What I'm trying to do is search a file for lines that match the two input words (first name, last name) in order to remove that line. The removal part is what I'm struggling with. Here is my code: echo... (4 Replies)
Discussion started by: lazypeterson
4 Replies

8. Shell Programming and Scripting

sed: delete regex line and next line if blank

Hi, I want to write a sed script which from batiato: batiato/giubbe: pip_b.2.txt pip_b.3.txt pip_b.3mmm.txt bennato: bennato/peterpan: 123.txt consoli: pip_a.12.txt daniele: (2 Replies)
Discussion started by: one71
2 Replies

9. UNIX for Advanced & Expert Users

Urgent Help required : awk/sed help to find pattern and delete till end of line

Hi, I need help with using an awk or sed filter on the below line ALTER TABLE "ACCOUNT" ADD CONSTRAINT "ACCOUNT_PK" PRIMARY KEY ("ACCT_ID") USING INDEX PCTFREE 10 INITRANS 2 MAXTRANS 255 STORAGE(INITIAL 65536 FREELISTS 1 FREELIST GROUPS 1) TABLESPACE "WMC_DATA" LOGGING ENABLE Look for... (1 Reply)
Discussion started by: rajan_san
1 Replies

10. Shell Programming and Scripting

Urgent! Sed/Awk Filter Find Pattern Delete Till End Of Line

Hi, I need help with using an awk or sed filter on the below line ALTER TABLE "ACCOUNT" ADD CONSTRAINT "ACCOUNT_PK" PRIMARY KEY ("ACCT_ID") USING INDEX PCTFREE 10 INITRANS 2 MAXTRANS 255 STORAGE(INITIAL 65536 FREELISTS 1 FREELIST GROUPS 1) TABLESPACE "WMC_DATA" LOGGING ENABLE Look for... (2 Replies)
Discussion started by: rajan_san
2 Replies
Login or Register to Ask a Question