1834935 Members
2298 Online
110071 Solutions
New Discussion

Re: unix command

 
SOLVED
Go to solution
mango_1
Frequent Advisor

unix command

hello all! I'd like to ask if there's a unix command that could delete parts of the file given a string. Example:
Given: File1 which contains
--testing--
test123
--testing--
--hello--
world
--hello--

If my string is hello, the output would be another file or the same file but without the hello block. So, the output would be
--testing--
test123
--testing--

if anybody knows please help! thanks!
19 REPLIES 19
Patrick Wallek
Honored Contributor

Re: unix command

The simplest, least elegant way would be:

# grep -v hello somefile > some_other_file

The above would look for all occurences of hello anywhere in a line in the file somefile and put all lines EXCEPT those in the file some_other_file.

I'm sure others will give you examples with Perl, awk, sed, etc. as there are lots of ways to do this.
curt larson_1
Honored Contributor

Re: unix command

lets see

cat file | sed '/^--hello--/d' > newFile

cat file | awk '{if ( $0 !~ "--hello--" ) print $0;}' > newFile
Caesar_3
Esteemed Contributor

Re: unix command

Hello!

Use perl, read the file and write to the
new one all what you want from the file.

Caesar
curt larson_1
Honored Contributor

Re: unix command

and

ex -s +"1,$ /^--hello--/ d|w!q" file1
Hai Nguyen_1
Honored Contributor

Re: unix command

Try this which will make a NEW File1:

# cat File1 | grep -v "--hello--" | tee File1 >/dev/null

Hai
James R. Ferguson
Acclaimed Contributor

Re: unix command

Hi:

# sed -n '/--hello--/,/--hello--/!p' filename > filename.new

Regards!

...JRF...
Pete Randall
Outstanding Contributor

Re: unix command

Do a search of the forums, using the "exact phrase" option, for "handy one liners for sed". I don't have the document available to me at the moment but I think it will provide what you seek.


Pete



Pete
mango_1
Frequent Advisor

Re: unix command

hello all! thanks for the info! what if there is additional text after hello. let's say.
--testing--
test123
--testing--
--hello--
hiya
--hello--
--world--
earth
--world--

is there a way to delete everything starting from hello to world. so the end output will be
--testing--
test123
--testing--

but without hard coding it. because I might not know what's the next block after hello. thanks so much! I'm relatively new with this.

curt larson_1
Honored Contributor

Re: unix command

can you provide a definition for what a "block" of output is. or a relationship between hello and world that is going to be deleted.
James R. Ferguson
Acclaimed Contributor

Re: unix command

Hi (again):

For your last question, examine the input you have. Now, you want only the block bounded, inclusively by "---testing--" and "---testing---". This is similar to the first solutipm I offered, but without negating (with the '!' flag)the 'p'rint:

# sed -n '/--testing/,/--testing/p' filein > fileout

This says, "dont print (output) anything, except, (p)rint that which begins (and ends) with the regular expression /--testing/.

Regards!

...JRF...
Fragon
Trusted Contributor

Re: unix command

Hi,
Do you mean from the first "hello" to last "world", all lines be deleted?

-ux
mango_1
Frequent Advisor

Re: unix command

hi thanks for the help everyone.

curt: a block is those lines enclosing the
--xxxxxx---
hello
world
--xxxxxx---

james: thanks for the help. I have another question to that. what if the file contains
--abc--
def
--abc--
--testing--
123
--testing--
--hello--
world
--hello--
--mango--
fruit
--mango--

then output the following
--abc--
def
--abc--
--testing--
123
--testing--
where you only know testing string and not the abc. could i use sed -n ...1,/--testing--/. hmmm...

thanks again everyone.
mango_1
Frequent Advisor

Re: unix command

hpux: I meant from the whole block of hello
just
--hello--
world
--hello--
mango_1
Frequent Advisor

Re: unix command

hi james! one last question please. what can I do to not include the --testing-- headers for example

I have this file format:
--abc--
def
--testing--
123
--hello--
world

then

sed -n '1,/--testing--/p' file19 > file23

will print out
--abc--
def
--testing--

is there a way to remove the --testing-- being printed out to the new file? eg
--abc--
def

or should I just reformat my file. hmmm

is there a way to manipulate the same file? in this case file19 without having to redirect the output to another file? just wondering. thanks!
James R. Ferguson
Acclaimed Contributor

Re: unix command

Hi (again):

In answer to your question about using:

# sed -n '1,/--testing--/p' filein > fileout

...that is legitimate syntax for "from record #1 through (inclusive) a record with the regular expression /--testing--/ print the output. Using your file, though as posted, you would get only this:

--abc--
def
--abc--
--testing--

If you want to extract the "blocks" /--abc--/ *and* /--testing--/ I suggest you do something like this:

# sed -n '/--abc--/,/--abc--/p' filein > fileout

# sed -n '/--testing--/,/--testing--/p' filein >> fileout

...that is, make two passes with the same input file, *appending* the second pass to the output of the first.

Regards!

...JRF...
James R. Ferguson
Acclaimed Contributor
Solution

Re: unix command

Hi Mango:

With regard to your last question..."to not include the --testing-- headers", here's one approach:

# V=--testing---
# sed -n "/${V}/,/${V}/p" filein|grep -v \\${V}

Note carefully the two backslashes before the ${V} variable in the 'grep' pipeline. Without these, 'grep' will not interpret the "--testing--" expression correctly.

Regards!

...JRF...
mango_1
Frequent Advisor

Re: unix command

thanks james! that really solved my problem. :) Is there a way to manipulate the same file. I don't know if its going to be inefficient to create another file when I'm just editing the same file. thanks!

mango_1
Frequent Advisor

Re: unix command

is this possible

test="==testin=="
sed -n "1,/==$test==/p" file | grep -v \\$test > file

thanks
James R. Ferguson
Acclaimed Contributor

Re: unix command

Hi Mango:

I think you wanted to write:

# test="==testin=="
# sed -n "1,/$test/p" file|grep -v \\$test > file

That is, from record-1 through a record with the string ==testin== ...

...and the answer is *NO* you can't. You need to use an intermediate file. In the above, you will end up with an empry file. You cannot edit inplace nor can you read and write to the same file (which is what the above pipeline does!

Regards!

...JRF...