Including Hash / in sed command filter


 
Thread Tools Search this Thread
Top Forums UNIX for Beginners Questions & Answers Including Hash / in sed command filter
# 1  
Old 02-13-2019
Including Hash / in sed command filter

Hello All,

I want to print data in between two lines in a file sample.txt through more or cat command on the screen. For that I am using below sed command to give the BEGIN and END text.

Content of sample.txt

Code:
server01:~ # cat /proc/mdstat

Hello this is a text message 1
Hello this is a text message 2
Hello this is a text message 3
Hello this is a text message 4
Hello this is a text message 5

Code:
server01:~ # df -k

TESTING 1
TESTING 2
TESTING 3
TESTING 4
TESTING 5

Code:
server02:~ # df -k

Command and its output:

Code:
server01:~ # more sample.txt | sed -n -e '/server01:~ # cat/,$p' | sed -e '/server01:~ # df -k/,$d'

Hello this is a text message 1
Hello this is a text message 2
Hello this is a text message 3
Hello this is a text message 4
Hello this is a text message 5

The above method is working fine but since I may have more cat text for server01 in sample.txt so I want to include complete text 'server01:~ # cat /proc/mdstat' in the BEGIN or END in between the sed / / for exact matching. But it gives error when I put like below because the matching string itself contains Hash (/) :

Code:
server01:~ # more sample.txt | sed -n -e '/server01:~ # cat/proc/mdstat/,$p' | sed -e '/server01:~ # df -k/,$d'


Please suggest the way forward
# 2  
Old 02-13-2019
I'm sorry I don't understand your request at all.


If you're after a solution to include a slash / in the address, you can
- escape it (\/)
- use another character for the address delimiters. man sed:c
Quote:
Addresses
\cregexpc
Match lines matching the regular expression regexp. The c may be any character.
# 3  
Old 02-13-2019
When you try to match strings with sed you need to distinguish between characters and metacharacters. Characters only match themselves, i.e.

Code:
sed -n '/a/p' /some/file

will print every (line containing an) "a" because /a/ matches only an "a" and nothing else.

But there are characters that do not match these same characters directly but influence the way other characters match something. These are called metacharacters and a "regular expression" usually consists of a mixture of characters and metacharacters. Here:

Code:
sed -n '/aa*z/p' /some/file

the expression searched for to print the line is /aa*z/ and the asterisk makes the "a" in front of it optional: it makes that any number (including zero) of the character before is matched. Here are some strings which would be matched by this expression:

Code:
az
aaz
aaaz
aaaaz
...

The one string NOT matched by the expression is this: aa*z, because the asterisk - unlike the "a" - doesn't match itself, only the way other characters (in this case the "a") match something.

In general, if you want to use a metacharacter as normal character you need to [i]escape[/icode] it. To escape it means you put a backslash ("\") in front of it:

Code:
sed -n '/aa\*z/p' /some/file

This changes the "*" to be a normal character and the only string this expression will match now is:

Code:
aa*z

and nothing else. The slash ("/") is also a metacharacter because it is used to mark beginning and end of a regular expression. In the above /aa*z/ it is not encluded in the string we searched for but acts only as a separator, similar to quotes like "this", which don't belong to the word they surround either. And therefore it needs to be escaped the same way as the asterisk:

Code:
sed -n -e '/server01:~ # cat/proc/mdstat/,$p'      # wrong
sed -n -e '/server01:~ # cat\/proc\/mdstat/,$p'                # correct

You need to do this with all metacharacters in your search expression. The most used ones are:

Code:
/
*
[
]
.

Btw., you should NOT use more for this purpose and you do not need to use separate sed calls for this:

Code:
server01:~ # more sample.txt | sed -n -e '/server01:~ # cat/proc/mdstat/,$p' | sed -e '/server01:~ # df -k/,$d'

write it like this instead:
Code:
server01:~ # sed -n -e '/server01:~ # cat/proc/mdstat/,$p' -e '/server01:~ # df -k/,$d' sample.txt

or even like this - ";" is used as separator between consecutive commands:
Code:
server01:~ # sed -n -e '/server01:~ # cat/proc/mdstat/,$p;/server01:~ # df -k/,$d' sample.txt

I hope this helps.

bakunin

Last edited by RudiC; 02-13-2019 at 06:39 AM..
# 4  
Old 02-13-2019
Quote:
Originally Posted by bakunin
When you try to match strings with sed you need to distinguish between characters and metacharacters. Characters only match themselves, i.e.

Code:
sed -n '/a/p' /some/file

will print every (line containing an) "a" because /a/ matches only an "a" and nothing else.

But there are characters that do not match these same characters directly but influence the way other characters match something. These are called metacharacters and a "regular expression" usually consists of a mixture of characters and metacharacters. Here:

Code:
sed -n '/aa*z/p' /some/file

the expression searched for to print the line is /aa*z/ and the asterisk makes the "a" in front of it optional: it makes that any number (including zero) of the character before is matched. Here are some strings which would be matched by this expression:

Code:
az
aaz
aaaz
aaaaz
...

The one string NOT matched by the expression is this: aa*z, because the asterisk - unlike the "a" - doesn't match itself, only the way other characters (in this case the "a") match something.

In general, if you want to use a metacharacter as normal character you need to [i]escape[/icode] it. To escape it means you put a backslash ("\") in front of it:

Code:
sed -n '/aa\*z/p' /some/file

This changes the "*" to be a normal character and the only string this expression will match now is:

Code:
aa*z

and nothing else. The slash ("/") is also a metacharacter because it is used to mark beginning and end of a regular expression. In the above /aa*z/ it is not encluded in the string we searched for but acts only as a separator, similar to quotes like "this", which don't belong to the word they surround either. And therefore it needs to be escaped the same way as the asterisk:

Code:
sed -n -e '/server01:~ # cat/proc/mdstat/,$p'      # wrong
sed -n -e '/server01:~ # cat\/proc\/mdstat/,$p'                # correct

You need to do this with all metacharacters in your search expression. The most used ones are:

Code:
/
*
[
]
.

Btw., you should NOT use more for this purpose and you do not need to use separate sed calls for this:

Code:
server01:~ # more sample.txt | sed -n -e '/server01:~ # cat/proc/mdstat/,$p' | sed -e '/server01:~ # df -k/,$d'

write it like this instead:
Code:
server01:~ # sed -n -e '/server01:~ # cat/proc/mdstat/,$p' -e '/server01:~ # df -k/,$d' sample.txt

or even like this - ";" is used as separator between consecutive commands:
Code:
server01:~ # sed -n -e '/server01:~ # cat/proc/mdstat/,$p;/server01:~ # df -k/,$d' sample.txt

I hope this helps.

bakunin



Hello,

Thanks for your valuable and detailed input. This has worked for me !! Thanks mate

Code:
sed -n -e '/server01:~ # cat \/proc\/mdstat/,$p'                # correct

--- Post updated at 11:58 AM ---

Quote:
Originally Posted by RudiC
I'm sorry I don't understand your request at all.


If you're after a solution to include a slash / in the address, you can
- escape it (\/)
- use another character for the address delimiters. man sed:c
You understood correctly and your suggestion has worked for me. Thanks dear

Last edited by Xtreme; 02-13-2019 at 07:57 AM..
# 5  
Old 02-13-2019
Quote:
Originally Posted by Xtreme
Thanks for your valuable and detailed input. I have tried both of the above options you mentioned but its not working.
WHAT is not working? Sorry, but i cannot see you monitor and if i should explain to you something i need to know what it is. Post your screen output (surrounded by CODE-tags) so that i can see what you have done and what the result was.

Quote:
Originally Posted by Xtreme
The reason I want to use more is
more is a paginator program, nothing more, nothing less. That means: if you have a long file and you type:

Code:
cat /this/file

it would rush all the lines through you terminal window at a high speed (far too fast to read along) and only the last lines would finally be visible, all the lines before would have scrolled off the screen. Instead using

Code:
more /this/file

will pause after each screen full of text and only continue (for exactly one other screen full) after you press a key. This is what more does. I can't see how that relates to your problem at all.

I hope this helps.

bakunin
# 6  
Old 02-13-2019
Hello Bakunin,

Sorry it was my bad. The command didn't work first due to some extra space needed which I added and now it is working. So your solution was perfect for my problem. Thanks.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Issue with user input including * (glob) and sed

Hello All, I have created a script that searches for different things and "sanitizes" the findings from files. Currently the user is required to put in a hostname (server.serverfarm.abc) one at a time to replace. I would like the user be able to use *.*.abc in grep and then pipe into sed to... (1 Reply)
Discussion started by: jvezinat
1 Replies

2. Shell Programming and Scripting

Remove bracket including text inside with sed

Hello, I could not remove brackets with text contents myfile: Please remove the bracket with text I wish to remove: I tried: sed 's/\//' myfile It gives: Please remove the bracket with text A1 I expect: Please remove the bracket with text Many thanks Boris (2 Replies)
Discussion started by: baris35
2 Replies

3. Shell Programming and Scripting

sed - remove begin of line up to the third and including occurence of character

hello. How to remove all characters in a line from first character ( a $ ) until and including the third occurrence of that character ( $ ). Any help is welcome. (10 Replies)
Discussion started by: jcdole
10 Replies

4. Shell Programming and Scripting

Problem with filter data using sed command

Hi, I am using the following command(sed) to get the key/value pair from the string String="{ "test":"test message", "testmessage":"subscription is active, charge successfully} " }" status=$( echo $String | sed -e 's/^.*\("testmessage":*\).*$/\1/') echo $status i am getting this... (2 Replies)
Discussion started by: nanthagopal
2 Replies

5. Shell Programming and Scripting

Filter date and time form a file using sed command

I want to filter out the date and time from this line in a file. How to do this using sed command. on Tue Apr 19 00:48:29 2011 (12 Replies)
Discussion started by: vineet.dhingra
12 Replies

6. Shell Programming and Scripting

sed: remove characters between and including 2 strings

I have the following line: 4/23/2010 0:00:38.000: Copying $$3MSYDDC02$I would like to use sed (or similiar) to remove everthing between and including $ that appears in the line so it ends up like this. 4/23/2010 0:00:38.000: Copying 3MSYDDC02I have been trying these but i'm really just... (5 Replies)
Discussion started by: jelloir
5 Replies

7. Shell Programming and Scripting

How to filter only comments while reading a file including line break characters.

How do I filter only comments and still keep Line breaks at the end of the line!? This is one of the common tasks we all do,, How can we do this in a right way..!? I try to ignore empty lines and commented lines using following approach. test.sh # \040 --> SPACE character octal... (17 Replies)
Discussion started by: kchinnam
17 Replies

8. Shell Programming and Scripting

Insert a line including Variable & Carriage Return / sed command as Variable

I want to instert Category:XXXXX into the 2. line something like this should work, but I have somewhere the wrong sytanx. something with the linebreak goes wrong: sed "2i\\${n}Category:$cat\n" Sample: Titel Blahh Blahh abllk sdhsd sjdhf Blahh Blah Blahh Blahh Should look like... (2 Replies)
Discussion started by: lowmaster
2 Replies

9. Shell Programming and Scripting

How to use sed to remove html tags including text between them

How to use sed to remove html tags including text between them? Example: User <b> rolvak </b> is stupid. It does not using <b>OOP</b>! and should output: User is stupid. It does not using ! Thank you.. (2 Replies)
Discussion started by: alphagon
2 Replies

10. Shell Programming and Scripting

sed or other tool to manipulate data, including email addresses

I have a list of names and email addresses, like this. The <tab> markers are actually tabs. joe.blow <tab> joe.blow@wherever.com tom.t.hall <tab> tom.t.hall@wherever.com john.r.smith <tab> john.r.smith@wherever.com sally.jones <tab> sally.jones@state.or.us I want to parse the data so that... (3 Replies)
Discussion started by: manouche
3 Replies
Login or Register to Ask a Question