Deleting rows starting with a character and word


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Deleting rows starting with a character and word
# 8  
Old 01-12-2013
we can use below sed command also

Code:
sed '/^[# The].*/d' $file


Last edited by Scrutinizer; 01-12-2013 at 03:05 AM.. Reason: code tags
This User Gave Thanks to srinivas matta For This Post:
# 9  
Old 01-12-2013
Quote:
Originally Posted by srinivas matta
we can use below sed command also

Code:
sed '/^[# The].*/d' $file

This means: delete any line that starts with #, space, T, h, or e ..
Quote:
Originally Posted by bipinajith
Code:
grep '^[^$|^#]' file

This means: delete any line that starts with ^, $, | or # ..
This User Gave Thanks to Scrutinizer For This Post:
# 10  
Old 01-12-2013
Quote:
Originally Posted by Scrutinizer
... ... ...
Quote:
Originally Posted by bipinajith
Code:
grep '^[^$|^#]' file

This means: delete any line that starts with ^, $, | or # ..
No. This grep looks for any line in file that starts with any character except for the characters $, |, ^, and #.

---------- Post updated at 00:37 ---------- Previous update was at 00:22 ----------

Quote:
Originally Posted by bioinfo
I have used both the following and they are working, but I want to delete both # and The in one command.

Code:
 egrep -v '^(#|$)'  
 egrep -v '^(The|$)'

I don't understand why you have the |$ in these expressions.

If you want to remove lines starting with # and remove lines starting with The, you can do that with:
Code:
grep -Ev '^(#|The)'

The command:
Code:
grep -Ev '^(#|The|$)'

will remove any lines starting with #, lines starting with The, and empty lines.

---------- Post updated at 00:46 ---------- Previous update was at 00:37 ----------

The commands:
Code:
egrep -v '^(#|The|[$])'

and equivalently
Code:
grep -Ev '^(#|The|[$])'

will remove any line starting with #, any line starting with The, and any line starting with $.
These 2 Users Gave Thanks to Don Cragun For This Post:
# 11  
Old 01-12-2013
Quote:
Originally Posted by Don Cragun
No. This grep looks for any line in file that starts with any character except for the characters $, |, ^, and #.
I tried to translate it into what the net effect would be. It effectively deletes (does not print) lines that start with those characters / it prints lines that do not start with these characters.
edit: I agree there would be a difference in case of empty lines.

Last edited by Scrutinizer; 01-12-2013 at 05:17 AM..
This User Gave Thanks to Scrutinizer For This Post:
# 12  
Old 01-12-2013
Code:
sed '/^[# The].*/d' $file

it delete the lines stared with # and The.

like
Code:
File is....
#srinivas
The book
To Scrutinizer

Here it delete first two lines only

---------- Post updated at 04:19 AM ---------- Previous update was at 04:14 AM ----------

Quote:
Originally Posted by Scrutinizer
I tried to translate it into what the net effect would be. It effectively deletes (does not print) lines that start with those characters / it prints lines that do not start with these characters.
edit: I agree there would be a difference in case of empty lines.
can you give me an example

Last edited by Scrutinizer; 01-12-2013 at 05:30 AM.. Reason: code tags
This User Gave Thanks to srinivas matta For This Post:
# 13  
Old 01-12-2013
Quote:
Originally Posted by srinivas matta
Code:
sed '/^[# The].*/d' $file

it delete the lines stared with # and The.

like
File is....
#srinivas
The book
To Scrutinizer

Here it delete first two lines only
No it would leave only the first line

---------- Post updated at 04:19 AM ---------- Previous update was at 04:14 AM ----------



Quote:
can you give me an example
Code:
$ printf 'This\n\nis\n$a sample\n' 
This

is
$a sample

Code:
$ printf 'This\n\nis\n$a sample\n' | grep '^[^$|^#]'
This
is

vs.

Code:
$ printf 'This\n\nis\n$a sample\n' | grep -v '^[$|^#]'
This

is

This User Gave Thanks to Scrutinizer 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

Get first word only starting of a line

Hi, I am trying to get longest line in a .gz file without extract the file. I am using the below command to get the longest line. zcat /a/b/c/file.gz |awk '{print length, $0}'|sort -nr|head -1 247 AAAAAAAAAAAAAAAAAAAAAAAAA BBBBBBBBBBBBBBBBBBBAAAAAA CCCCCCCCCCC DDDDDDDDDDDD EEEEEEEEEEEEEE... (4 Replies)
Discussion started by: k_manimuthu
4 Replies

2. UNIX for Dummies Questions & Answers

Count of rows starting with 01 in a file

Hi I am very new to unix and please help me in solving the below problem I have a file with 50000+ rows. Each row(line) start with 01 or 02 or 03. Now i have to get the count of rows starting with 01 only, Thanks in advance (3 Replies)
Discussion started by: nmakkena
3 Replies

3. Shell Programming and Scripting

Replace a word in a string starting with another word

Hi All, I have a file in which a number of lines are starting with similar first word but different next words. I want to replace the any nth word(not 1st or 2nd) with another word. Eg:- My file contains are like this:- Ram is a boy. Ram is a good boy. Ram plays cricket. Here I want to... (2 Replies)
Discussion started by: mukeshbaranwal
2 Replies

4. Shell Programming and Scripting

Search the word to be deleted and delete lines above this word starting from P1 to P3

Hi, I have to search a word in a text file and then I have to delete lines above from the word searched . For eg suppose the file is like this: Records P1 10,23423432 ,77:1 ,234:2 P2 10,9089004 ,77:1 ,234:2 ,87:123 ,9898:2 P3 456456 P1 :123,456456546 P2 abc:324234 (2 Replies)
Discussion started by: vsachan
2 Replies

5. Shell Programming and Scripting

Deleting lines not starting with numbers with sed

Title says all :p Thanks for your help (4 Replies)
Discussion started by: drbiloukos
4 Replies

6. Shell Programming and Scripting

In Sed how can I replace starting from the 7th character to the 15th character.

Hi All, Was wondering how I can do the following.... I have a String as follows "ACCTRL000005022RRWDKKEEDKDD...." This string can be in a file called tail.out or in a Variable called $VAR2 Now I have another variable called $VAR1="000004785" (9 bytes long), I need the content of... (5 Replies)
Discussion started by: mohullah
5 Replies

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

8. Shell Programming and Scripting

Deleting all characters from 350th character to 450th character from the log file

Hi All, I have a big log file i want to delete all characters (between 350th to 450th characters) starting at 350th character position to 450th character position. please advice or sample code. (6 Replies)
Discussion started by: rajeshorpu
6 Replies

9. Shell Programming and Scripting

Command to parse a word character by character

Hi All, Can anyone help me please, I have a word like below. 6,76 I want to read this word and check if it has "," (comma) and if yes then i want to replace it with "." (dot). That means i want to be changed to 6.76 If the word doesnot contain "," then its should not be changed. Eg. ... (6 Replies)
Discussion started by: girish.raos
6 Replies

10. UNIX for Dummies Questions & Answers

Deleting lines starting with spaces then non-numerals

I did a search but couldn't find a thread that seemed to answer this but my apologies if it has been answered before. I have some text files and I need to remove any line that does not start with a number (0-9). In actuality every line like this starts with a 'T' (or 't') but there are a... (5 Replies)
Discussion started by: skray
5 Replies
Login or Register to Ask a Question