- Community Home
- >
- Servers and Operating Systems
- >
- Operating Systems
- >
- Operating System - HP-UX
- >
- Re: unix command
Categories
Company
Local Language
Forums
Discussions
Forums
- Data Protection and Retention
- Entry Storage Systems
- Legacy
- Midrange and Enterprise Storage
- Storage Networking
- HPE Nimble Storage
Discussions
Forums
Discussions
Discussions
Discussions
Forums
Discussions
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
- BladeSystem Infrastructure and Application Solutions
- Appliance Servers
- Alpha Servers
- BackOffice Products
- Internet Products
- HPE 9000 and HPE e3000 Servers
- Networking
- Netservers
- Secure OS Software for Linux
- Server Management (Insight Manager 7)
- Windows Server 2003
- Operating System - Tru64 Unix
- ProLiant Deployment and Provisioning
- Linux-Based Community / Regional
- Microsoft System Center Integration
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Community
Resources
Forums
Blogs
- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Printer Friendly Page
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-07-2003 12:53 PM
07-07-2003 12:53 PM
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!
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-07-2003 01:01 PM
07-07-2003 01:01 PM
Re: unix command
# 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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-07-2003 01:02 PM
07-07-2003 01:02 PM
Re: unix command
cat file | sed '/^--hello--/d' > newFile
cat file | awk '{if ( $0 !~ "--hello--" ) print $0;}' > newFile
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-07-2003 01:04 PM
07-07-2003 01:04 PM
Re: unix command
Use perl, read the file and write to the
new one all what you want from the file.
Caesar
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-07-2003 01:08 PM
07-07-2003 01:08 PM
Re: unix command
ex -s +"1,$ /^--hello--/ d|w!q" file1
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-07-2003 01:08 PM
07-07-2003 01:08 PM
Re: unix command
# cat File1 | grep -v "--hello--" | tee File1 >/dev/null
Hai
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-07-2003 01:12 PM
07-07-2003 01:12 PM
Re: unix command
# sed -n '/--hello--/,/--hello--/!p' filename > filename.new
Regards!
...JRF...
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-07-2003 02:04 PM
07-07-2003 02:04 PM
Re: unix command
Pete
Pete
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-07-2003 02:19 PM
07-07-2003 02:19 PM
Re: unix command
--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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-07-2003 02:32 PM
07-07-2003 02:32 PM
Re: unix command
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-07-2003 04:27 PM
07-07-2003 04:27 PM
Re: unix command
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...
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-07-2003 04:29 PM
07-07-2003 04:29 PM
Re: unix command
Do you mean from the first "hello" to last "world", all lines be deleted?
-ux
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-09-2003 05:04 AM
07-09-2003 05:04 AM
Re: unix command
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-09-2003 05:07 AM
07-09-2003 05:07 AM
Re: unix command
just
--hello--
world
--hello--
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-09-2003 05:17 AM
07-09-2003 05:17 AM
Re: unix command
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!
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-09-2003 05:28 AM
07-09-2003 05:28 AM
Re: unix command
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...
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-09-2003 06:06 AM
07-09-2003 06:06 AM
SolutionWith 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...
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-10-2003 08:28 AM
07-10-2003 08:28 AM
Re: unix command
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-10-2003 12:11 PM
07-10-2003 12:11 PM
Re: unix command
test="==testin=="
sed -n "1,/==$test==/p" file | grep -v \\$test > file
thanks
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-10-2003 01:26 PM
07-10-2003 01:26 PM
Re: unix command
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...