LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 10-09-2012, 05:26 AM   #1
tropical781
LQ Newbie
 
Registered: Oct 2012
Posts: 2

Rep: Reputation: Disabled
Unhappy I specifically want to delete my grep output string from the text file


My name is Clayton and I'm a little bit new with Linux and I have a problem.
I have a text file called file_a.txt My first
command is => grep -A 12 ".production =" file_a.txt The output is a few block.
Each block of string contains 13 rows

I specifically want to delete all the block of strings I got with the grep
command from the original file file_a.txt I do not want to send the grep
output to a new file. Also I do not want to to use grep -v because it will not work in my case.

I have tried something like things like this, but nothing is working:

cut < grep -A 12 ".production =" file_a.txt

sed -i '/grep -A 12 ".production ="/d' file_a.txt
 
Old 10-09-2012, 06:40 AM   #2
Thad E Ginataom
Member
 
Registered: Mar 2011
Distribution: Ubuntu 12.04 with KXStudio, MATE & Compiz
Posts: 46

Rep: Reputation: 7
Grep is for finding stuff in a file.

sed is for editing a file.

It is a little complex, but you can soon get used to it. Studying the manual entry is essential.

Just to get you started, the basic way to delete a line is /pattern/d. Try...

$ sed '/\.production/d'

This will write its output to your screen. That is because sed is a stream editor, eg


$ sed '/\.production/d' | grep \.production

("\.? --- I think you have to "escape" that dot.)

Should give you no output . You can write that to another file and then copy it back over the first, or, somewhere in the sed manual entry, you'll find the option to get it to edit the file "in place".

Beware of such editing: if it doesn't do what you want, but does something else, you can't recover it. Keep a backup of your real data file, and work on a scratch file.

I see you have already got the /pattern/action idea, but are confused about command substitution, which is not what you want here. Even if it was, you need to get the different quotes ` ' " ...and \ sorted out. A shell primer is the place to do that. Your combination of sed and grep is so wrong. You'll find out why a few pages in. This stuff is hard to get started with, but, with a little learning and experiment, you'll soon find it much easier. Enjoy

I'm a decade out of practice: please take my suggestions as pointers to work with, not as complete answers. They may or may not work

Last edited by Thad E Ginataom; 10-09-2012 at 06:47 AM.
 
Old 10-09-2012, 07:44 AM   #3
pixellany
LQ Veteran
 
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Arch/XFCE
Posts: 17,802

Rep: Reputation: 738Reputation: 738Reputation: 738Reputation: 738Reputation: 738Reputation: 738Reputation: 738
Quote:
I specifically want to delete all the block of strings I got with the grep
command
I'm not sure what this means.....

A command such as GREP or SED operates on "expressions"--AKA "regular expressions" or "regex" for short. As already noted, GREP is used for finding things, and something like SED is better for making changes.

SED operates one line at a time, and can either delete the whole line, or delete or modify portions.
to delete a line containing "rabbit":
Code:
sed '/rabbit/d' filename > newfilename
to delete all occurences of "rabbit" from every line:
Code:
sed 's/rabbit//g' filename > newfilename
to delete all lines from the first "rabbit" to the first "hare":
Code:
sed '/rabbit/,/hare/d' filename > newfilename
For more, go here: http://www.grymoire.com/Unix/Sed.html
 
Old 10-09-2012, 08:02 AM   #4
tropical781
LQ Newbie
 
Registered: Oct 2012
Posts: 2

Original Poster
Rep: Reputation: Disabled
Thanks

Thanks Ginataom for the information and your advices.

I register today on 3 Linux site, so I put my question 3 times;
I think 2 of the sites are the same, because I got a reaction from the moderator.
I put the same question on: stackoverflow.com and on unix.stackexchange.com.

They give my the following solution:
sed -i '/.production =/,+12d' file_a.txt

I tried and works fine.
I am sure that there are more other solutions too.

Again thanks for the help and the advices...especially for learning me always make a backup
of the files before start trying out things.

Last edited by tropical781; 10-09-2012 at 08:04 AM.
 
Old 10-09-2012, 08:13 AM   #5
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,529

Rep: Reputation: 2896Reputation: 2896Reputation: 2896Reputation: 2896Reputation: 2896Reputation: 2896Reputation: 2896Reputation: 2896Reputation: 2896Reputation: 2896Reputation: 2896
Or you could even use exactly what you started with and put it in sed:
Code:
sed -i '/.production =/,+12 d' file_a.txt
 
Old 10-09-2012, 03:48 PM   #6
Thad E Ginataom
Member
 
Registered: Mar 2011
Distribution: Ubuntu 12.04 with KXStudio, MATE & Compiz
Posts: 46

Rep: Reputation: 7
Quote:
/.production =/,+12 d
Oh, I see... sorry, I missed the fact that you wanted to delete 13 lines: I didn't understand "block"

Please note the thing with that dot. /this is a regexp/ as tropical781 points out, and that means that some symbols have special meaning. In this instance, "." means any single character. That includes a dot, of course, so it will match ".production" but it will also match "-production," "xproduction," "yrpoduction," "zproduction." That's the relevance of the \ in my answer.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
bash script- text file manip- delete everything before/after string justin99 Programming 9 11-20-2014 03:16 AM
[SOLVED] using sed and grep with a text string boumphreyfr Linux - Newbie 4 04-29-2011 04:26 PM
Sed/awk/grep search for number string of variable length in text file Alexr Linux - Newbie 10 01-19-2010 01:34 PM
possible with grep? find string and then output that and the rest of the file captain_cthulhu Linux - Newbie 4 05-18-2009 12:09 PM
grep output on stdout and grep output to file don't match xnomad Linux - General 3 01-13-2007 04:56 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 03:03 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration