Help with sed code line


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Help with sed code line
# 1  
Old 05-04-2013
Help with sed code line

Can anyone tell me what the following line of code does?

Code:
sed 's/2\([0-2][0-9]\)\([0-5][0-9]\)/#\1#\2#/g' file1 > file2


Last edited by Scott; 05-04-2013 at 04:41 PM.. Reason: Please use code tags
# 2  
Old 05-04-2013
Quote:
Originally Posted by dee campbell
Can anyone tell me what the following line of code does?

Code:
sed 's/2\([0-2][0-9]\)\([0-5][0-9]\)/#\1#\2#/g' file1 > file2

This part :

Code:
sed 's/2\([0-2][0-9]\)\([0-5][0-9]\)/#\1#\2#/g' file1

uses the "sed" utility to search for a pattern in file "file1" and replace it by something, if it is found. If the pattern is not found, the line will be returned as it is.

This part:

Code:
> file2

redirects the output of the sed command above to another file called "file2". If it exists, the file "file2" will be overwritten, otherwise it will be created.

Now for the search/replace part:

Code:
s                               : search for a pattern
/
2                               : that starts with the character "2"
\([0-2][0-9]\)              : followed by a two-digit number between "00" and "29" both inclusive. Store this two-digit number in a temporary variable called "\1"
\([0-5][0-9]\)              : followed by a two-digit number between "00" and "59" both inclusive. Store this two-digit number in a temporary variable called "\2"
/                                : if such a pattern is found, replace it with
#\1                            : the character "#" followed by the value of the temporary variable "\1" derived above
#\2#                          : followed by the character "#" followed by the value of the temporary variable "\2" derived above
/g                              : and perform this search/replace process for each such pattern in each line of the file

Here's an example of the working of the sed command. I've omitted the redirection part at the end:

Code:
$
$ cat f20
blah 20000 blah 20059 blah
blah 23456 blah 22959 blah
blah 29988 blah 39988 blah
$
$ sed 's/2\([0-2][0-9]\)\([0-5][0-9]\)/#\1#\2#/g' f20
blah #00#00# blah #00#59# blah
blah 23456 blah #29#59# blah
blah 29988 blah 39988 blah
$
$

(a) Line 1
Both the numbers in the first line matched the pattern, hence they were replaced.

(b) Line 2
The first number in the second line did not match the pattern, because "2" was not followed by a number between "00" and "29" (both inclusive). It was followed by "34", so the first number was left as it is. The second number matched the pattern and was suitably replaced.

(c) Line 3
None of the two numbers matched the pattern, so the line was returned as it was.
# 3  
Old 05-04-2013
If you look at the examples closely, along with the explanation durden_tyler provided, you'll see what the code does.
Code:
$ echo 23344 | sed 's/2\([0-2][0-9]\)\([0-5][0-9]\)/#\1#\2#/g'
23344
$ echo 22344 | sed 's/2\([0-2][0-9]\)\([0-5][0-9]\)/#\1#\2#/g'
#23#44#
$ echo 72344 | sed 's/2\([0-2][0-9]\)\([0-5][0-9]\)/#\1#\2#/g'
72344

Code:
$ echo 22364 | sed 's/2\([0-2][0-9]\)\([0-5][0-9]\)/#\1#\2#/g'
22364
$ echo 22354 | sed 's/2\([0-2][0-9]\)\([0-5][0-9]\)/#\1#\2#/g'
#23#54#
$ echo 12354 | sed 's/2\([0-2][0-9]\)\([0-5][0-9]\)/#\1#\2#/g'
12354

 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Sed/grep: check if line exists, if not add line?

Hello, I'm trying to figure out how to speed up the following as I want to use multiple commands to search thousands of files. is there a way to speed things up? Example I want to search a bunch of files for a specific line, if this line already exists do nothing, if it doesn't exist add it... (4 Replies)
Discussion started by: f77hack
4 Replies

2. Shell Programming and Scripting

Replace values in script reading line by line using sed

Hi all, Let's say I have a script calling for the two variables PA_VALUE and PB_VALUE. for pa in PA_VALUE blah blah do for pb in PB_VALUE blah blah do I have a text file with two columns of values for PA and PB. 14.5 16.7 7.8 9.5 5.6 3.6 etc etc I would like to read this... (7 Replies)
Discussion started by: crimsonengineer
7 Replies

3. Shell Programming and Scripting

sed command to replace a line in a file using line number from the output of a pipe.

Sed command to replace a line in a file using line number from the output of a pipe. Is it possible to replace a whole line piped from someother command into a file at paritcular line... here is some basic execution flow.. the line number is 412 lineNo=412 Now i have a line... (1 Reply)
Discussion started by: vivek d r
1 Replies

4. Shell Programming and Scripting

sed and awk giving error ./sample.sh: line 13: sed: command not found

Hi, I am running a script sample.sh in bash environment .In the script i am using sed and awk commands which when executed individually from terminal they are getting executed normally but when i give these sed and awk commands in the script it is giving the below errors :- ./sample.sh: line... (12 Replies)
Discussion started by: satishmallidi
12 Replies

5. Shell Programming and Scripting

sed command to grep multiple pattern present in single line and delete that line

here is what i want to achieve.. i have a file with below contents cat fileName blah blah blah . .DROP this REJECT that . --sport 7800 -j REJECT --reject-with icmp-port-unreachable --dport 7800 -j REJECT --reject-with icmp-port-unreachable . . . more blah blah blah --dport 3306... (14 Replies)
Discussion started by: vivek d r
14 Replies

6. Shell Programming and Scripting

sed command to replace a line at a specific line number with some other line

my requirement is, consider a file output cat output blah sdjfhjkd jsdfhjksdh sdfs 23423 sdfsdf sdf"sdfsdf"sdfsdf"""""dsf hellow there this doesnt look good et cetc etc etcetera i want to replace a line of line number 4 ("this doesnt look good") with some other line ... (3 Replies)
Discussion started by: vivek d r
3 Replies

7. Shell Programming and Scripting

Sed Comparing Parenthesized Values In Previous Line To Current Line

I am trying to delete lines in archived Apache httpd logs Each line has the pattern: <ip-address> - - <date-time> <document-request-URL> <http-response> <size-of-req'd-doc> <referring-document-URL> This pattern is shown in the example of 6 lines from the log in the code box below. These 6... (1 Reply)
Discussion started by: Proteomist
1 Replies

8. Shell Programming and Scripting

Sed or Grep to delete line containing patter plus extra line

I'm new to using sed and grep commands, but have found them extremely useful. However I am having a hard time figuring this one out: Delete every line containing the word CEN and the next line as well. ie. test.txt blue 324 CEN green red blue 324 CEN green red blue to produce:... (2 Replies)
Discussion started by: rocketman88
2 Replies

9. Shell Programming and Scripting

single line input to multiple line output with sed

hey gents, I'm working on something that will use snmpwalk to query the devices on my network and retreive the device name, device IP, device model and device serial. I'm using Nmap for the enumeration and sed to clean up the results for use by snmpwalk. Once i get all the data organized I'm... (8 Replies)
Discussion started by: mitch
8 Replies

10. Shell Programming and Scripting

sed remove last 10 characters of a line start from 3rd line

hello experts, I need a sed command that remove last 10 characters of a line start from 3rd line. any suggestions? Thanks you (7 Replies)
Discussion started by: minifish
7 Replies
Login or Register to Ask a Question