Grep and neglect a specific string


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Grep and neglect a specific string
# 1  
Old 04-09-2018
Grep and neglect a specific string

Hi,

I have a file with "n" number of lines. I need to get rid of a specific line having a specific string from the file. I tried some possibilities but not successful.

For ex: in a file named "test"
hope should be removed along with the line.

Code:
PASS=test1/worked:fail/withthe/test/:go-to-school/grab/some:get/the/hope/to/work:success
LIB_WORK=/lets/hope/for/the/best \

output should be
Code:
PASS=test1/worked:fail/withthe/test/:go-to-school/grab/some:success

# 2  
Old 04-09-2018
What did you try, and what wrong output did you get?
# 3  
Old 04-09-2018
Quote:
Originally Posted by ricky-row
. . . get rid of a specific line having a specific string
.
.
.
hope should be removed along with the line.
.
.
.
Unclear: you write you want to remove line(s) but in your sample, only parts of the line are removed. Pls. clarify.
# 4  
Old 04-10-2018
@RudiC My requirement is both as you could see from the first line "hope" lies between :*hope*: the other line doesn't have any : so, it should remove the entire content.

@MadeInGermany
I have used the regex for replacing words with others, not in an expected way. so, I am not aware of the pattern to be used.

Code:
sed -i -re "s,hope,,g" test

Moderator's Comments:
Mod Comment Please use CODE tags as required by forum rules!

Last edited by RudiC; 04-10-2018 at 04:32 AM.. Reason: Added CODE tags.
# 5  
Old 04-10-2018
Like so?
Code:
sed 's/\(:*\)[^:]*hope[^:]*:*/\1/g; /^$/d' file
PASS=test1/worked:fail/withthe/test/:go-to-school/grab/some:success

# 6  
Old 04-10-2018
Hi RudiC
This is works really great when the content is in two seperate lines as
Code:
PASS=test1/worked:fail/withthe/test/:go-to-school/grab/some:get/the/hope/to/work:success

LIB_WORK=/lets/hope/for/the/best \

Code:
:~/Desktop/test1$ sed 's/\(:*\)[^:]*hope[^:]*:*/\1/g; /^$/d' te
PASS=test1/worked:fail/withthe/test/:go-to-school/grab/some:success

but some output come within a single line, in that case, it loses some data,
Could you give a modification according to that

Code:
PASS=test1/worked:fail/withthe/test/:go-to-school/grab/some:get/the/hope/to/work:success LIB_WORK=/lets/hope/for/the/best \

Code:
:~/Desktop/test1$ sed 's/\(:*\)[^:]*hope[^:]*:*/\1/g; /^$/d' te
PASS=test1/worked:fail/withthe/test/:go-to-school/grab/some:


if some explanation provided on the logic, it will much better to me to understand make use of it in, instead of wasting your valuable time.

---------- Post updated at 01:33 PM ---------- Previous update was at 01:20 PM ----------

Hi

If the regex is given in two different conditions too, its welcome.
Like one condition check for
Code:
:

as delimiter and remove the content with along with on delimiter
Code:
hope

2nd can check for the
Code:
hope

and if no delimiter was there it can delete the entire content
Code:
LIB_WORK=/lets/hope/for/the/best

Adding this to make clear in need

Last edited by ricky-row; 04-10-2018 at 04:51 AM.. Reason: code not visible
# 7  
Old 04-10-2018
It is usually best and most efficient for all parties to carefully phrase the request in the first place, cogitating all the possible ramifications e.g. difference in structures and positions in files. Better than letting drip in information droplet by droplet after a solution was found for the first but non-fitting sample.

The easiest way would be to force the long lines into several independent ones:
Code:
tr ' ' '\n' < file | sed ...

The logics lie in the regex: find a string of zero or more colons (parenthesized for later "back reference") then any number of non-colons, the "hope" string, non-colons again, and finally zero or more colons. Replace by the first colon if exists else the empty string (i.e. remove)
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

How to use a grep search to search for a specific string within multiple directories?

Lets say I have a massive directory which is filled with other directories all filled with different c++ scripts and I want a listing of all the scripts that contain the string: "this string". Is there a way to use a grep search for that? I tried: grep -lr "this string" * but I do not... (3 Replies)
Discussion started by: Circuits
3 Replies

2. Shell Programming and Scripting

Grep only words containing specific string

Hello, I have two files. All urls are space seperated. source http://xx.yy.zz http://df.ss.sd.xz http://09.09.090.01 http://11.22.33 http://canada.xx.yy http://01.02.03.04 http://33.44.55 http://98.87.76.65 http://russia.xx.zz http://aa.tt.xx.zz http://1w.2e.3r.4t http://china.rr.tt ... (4 Replies)
Discussion started by: baris35
4 Replies

3. Shell Programming and Scripting

Grep string in a file and paste next line in a specific way

Hello, I know there are many questions and replies regarding grep command. What I would like to do is a bit different. File A: hello world welcome to my page this is my test site how are you I am fine, thank you where have you been I was in hospital really hope you are fine now Thanks,... (10 Replies)
Discussion started by: baris35
10 Replies

4. UNIX for Dummies Questions & Answers

Grep contains specific string

i have file input dsgfdgdfgd> cab |egrep -i '(active|cbu)' 130502-11:34:11 10.133.1.153 9.0j stopfile=/tmp/15959 Trying password from ipdatabase file: /opt/ericsson/amos/moshell/sitefiles/ipdatabase... .. 0 1 CBU1 OFF ON 16HZ ROJ1192209/1 R5E TU8BZ04466... (3 Replies)
Discussion started by: radius
3 Replies

5. UNIX for Dummies Questions & Answers

How to grep cells that contain a specific string?

How do you grep cells that contain a specific string. I tried grep but it greps the whole line and not just the cells. Thanks! (4 Replies)
Discussion started by: evelibertine
4 Replies

6. UNIX for Dummies Questions & Answers

Grep Specific String In CSV

Hi All, I have a csv file like the following: "ABCD2","EFGH2","XXXX","1" "ABCD2","EFGH2","XXXX","2" I want to grep out the row which contains the value of 2 within the 4th column, so then i can use the extracted record to cut up and store into numerous variables. Obviously when... (3 Replies)
Discussion started by: RichZR
3 Replies

7. Programming

How to grep the specific string or user's list from the file

I have a file on UNIX system from where I want to grep the list of all users associated to the particular repository.If the user's list is in single line then I fetch all list but if it is in two separate lines it doesn't.I use the below command a=KESTREL-DEV;b=users;cat access_file|grep... (1 Reply)
Discussion started by: rohit22hamirpur
1 Replies

8. Shell Programming and Scripting

Remove a specific line from grep output string

Dear All I want to search string "1000" from input file and if it found i want remove line that contain 1000 and also remove 3 line above it and 2 line below it. INPUT FILE: BHAT-D 2 aaa ID CODE GS UPDATE MODE LANG MCO MCL NUMPAGES 50 ... (7 Replies)
Discussion started by: jaydeep_sadaria
7 Replies

9. Shell Programming and Scripting

How To Neglect Delimiter

Hi all Iam having a text file of records seperated by "SPACE" and another text File of Records seperated by "TAB" i'm writing a Generic code for Extracting Duplicate Records Iam using "cut" Command to extract data. How Can I Neglect Delimiter And write a generic code for all cases ... (4 Replies)
Discussion started by: G.Aavudai
4 Replies

10. Shell Programming and Scripting

grep - searching for a specific string

ppl, this is my "file" with fields orderno orderdate orderdesc telno street city 1 01/04/2006 abc 123 100 tampa 2 01/04/2006 abc 123 100 tampa 3 01/04/2006 abc 123 100 tampa 4 01/04/2006 abc ... (2 Replies)
Discussion started by: manthasirisha
2 Replies
Login or Register to Ask a Question