My notes on Linux SED command.
Table of contents
Open Table of contents
Introduction
SED
stands for “Stream Editor”. If your data is unformatted or unstructured then you should use SED
.
sed
command acts on line-by-line basis.
SED Command Examples
Basic Query & Print
sed
requires expressions instead of curly brackets.
# SED logic is written inside ''.
sed '/INFO/p' app.log
Here, p
stands for print
. It will print the query’s result.
This will output all INFO
logs + all other logs where each individual character has its existence.
Use -n
option to find the exact pattern match. The following command will fetch logs having INFO
levels only.
sed -n '/INFO/p' app.log
Find & Replace
Let’s replace INFO
level with LOG
level.
sed 's/INFO/LOG/g' app.log
Here
s
means sub-replace. We are replacing a sub-string from a big string.g
global replace.
Query Line Numbers
Let’s find on which line numbers the INFO
level logs appear in our log file.
sed -n -e '/INFO/=' app.log
Here
-e
means the statement is an expression.-n
limits the scope by exact match.
Let’s print the line numbers and the line content itself for each INFO
log occurrence.
sed -n -e '/INFO/=' -e '/INFO/p' app.log
Range Replace
Let’s replace the INFO
logs only for lines from 1 and 10.
sed '1,10 s/INFO/LOG/g' app.log
Let’s replace as above but print only partial result.
sed '1,10 s/INFO/LOG/g; 1,10p; 11q' app.log
Here
;
marks end of 1 expression.p
means print.q
means quit.
Sample Log File
Source: IBM Sample Log File
03/22 08:53:22 TRACE :.....rsvp_event: received event from RAW-IP on interface 9.67.116.98
03/22 08:53:22 TRACE :......rsvp_explode_packet: v=1,flg=0,type=2,cksm=54875,ttl=255,rsv=0,len=84
03/22 08:53:22 TRACE :.......rsvp_parse_objects: STYLE is WF
03/22 08:53:22 INFO :.......rsvp_parse_objects: obj RSVP_HOP hop=9.67.116.99, lih=0
03/22 08:53:22 TRACE :......rsvp_event_mapSession: Session=9.67.116.99:1047:6 exists
03/22 08:53:22 INFO :.......rsvp_flow_stateMachine: state RESVED, event RESV
03/22 08:53:22 TRACE :........flow_timer_stop: Stop T4
03/22 08:53:22 TRACE :........flow_timer_start: Start T4
03/22 08:53:22 TRACE :.......rsvp_flow_stateMachine: reentering state RESVED
03/22 08:53:22 EVENT :..mailslot_sitter: process received signal SIGALRM
03/22 08:53:22 TRACE :.....event_timerT1_expire: T1 expired
03/22 08:53:22 INFO :......router_forward_getOI: Ioctl to query route entry successful
03/22 08:53:22 TRACE :......router_forward_getOI: source address: 9.67.116.98
03/22 08:53:22 TRACE :......router_forward_getOI: out inf: 9.67.116.98
03/22 08:53:22 TRACE :......router_forward_getOI: gateway: 0.0.0.0
03/22 08:53:22 TRACE :......router_forward_getOI: route handle: 7f5251c8
03/22 08:53:22 INFO :......rsvp_flow_stateMachine: state RESVED, event T1OUT
03/22 08:53:22 TRACE :.......rsvp_action_nHop: constructing a PATH
03/22 08:53:22 TRACE :.......flow_timer_start: started T1
03/22 08:53:22 TRACE :......rsvp_flow_stateMachine: reentering state RESVED
03/22 08:53:22 TRACE :.......mailslot_send: sending to (9.67.116.99:0)
03/22 08:53:38 EVENT :..mailslot_sitter: process received signal SIGALRM
03/22 08:53:38 TRACE :.....event_timerT1_expire: T1 expired
03/22 08:53:38 INFO :......router_forward_getOI: Ioctl to query route entry successful
03/22 08:53:38 TRACE :......router_forward_getOI: source address: 9.67.116.98
03/22 08:53:38 TRACE :......router_forward_getOI: out inf: 9.67.116.98
03/22 08:53:38 TRACE :......router_forward_getOI: gateway: 0.0.0.0
03/22 08:53:38 TRACE :......router_forward_getOI: route handle: 7f5251c8
03/22 08:53:38 INFO :......rsvp_flow_stateMachine: state RESVED, event T1OUT
03/22 08:53:38 TRACE :.......rsvp_action_nHop: constructing a PATH
03/22 08:53:38 TRACE :.......flow_timer_start: started T1
03/22 08:53:38 TRACE :......rsvp_flow_stateMachine: reentering state RESVED
03/22 08:53:38 TRACE :.......mailslot_send: sending to (9.67.116.99:0)
03/22 08:53:52 TRACE :.....rsvp_event: received event from RAW-IP on interface 9.67.116.98
03/22 08:53:52 TRACE :......rsvp_explode_packet: v=1,flg=0,type=2,cksm=54875,ttl=255,rsv=0,len=84
03/22 08:53:52 TRACE :.......rsvp_parse_objects: STYLE is WF
03/22 08:53:52 INFO :.......rsvp_parse_objects: obj RSVP_HOP hop=9.67.116.99, lih=0
03/22 08:53:52 TRACE :......rsvp_event_mapSession: Session=9.67.116.99:1047:6 exists
03/22 08:53:52 INFO :.......rsvp_flow_stateMachine: state RESVED, event RESV
03/22 08:53:52 TRACE :........flow_timer_stop: Stop T4
03/22 08:53:52 TRACE :........flow_timer_start: Start T4
03/22 08:53:52 TRACE :.......rsvp_flow_stateMachine: reentering state RESVED
03/22 08:53:53 EVENT :..mailslot_sitter: process received signal SIGALRM
03/22 08:53:53 TRACE :.....event_timerT1_expire: T1 expired
03/22 08:53:53 INFO :......router_forward_getOI: Ioctl to query route entry successful
03/22 08:53:53 TRACE :......router_forward_getOI: source address: 9.67.116.98
03/22 08:53:53 TRACE :......router_forward_getOI: out inf: 9.67.116.98
03/22 08:53:53 TRACE :......router_forward_getOI: gateway: 0.0.0.0
03/22 08:53:53 TRACE :......router_forward_getOI: route handle: 7f5251c8
03/22 08:53:53 INFO :......rsvp_flow_stateMachine: state RESVED, event T1OUT
03/22 08:53:53 TRACE :.......rsvp_action_nHop: constructing a PATH
03/22 08:53:53 TRACE :.......flow_timer_start: started T1
03/22 08:53:53 TRACE :......rsvp_flow_stateMachine: reentering state RESVED
03/22 08:53:53 TRACE :.......mailslot_send: sending to (9.67.116.99:0)
03/22 08:54:09 EVENT :..mailslot_sitter: process received signal SIGALRM
03/22 08:54:09 TRACE :.....event_timerT1_expire: T1 expired
03/22 08:54:09 INFO :......router_forward_getOI: Ioctl to query route entry successful
03/22 08:54:09 TRACE :......router_forward_getOI: source address: 9.67.116.98
03/22 08:54:09 TRACE :......router_forward_getOI: out inf: 9.67.116.98
03/22 08:54:09 TRACE :......router_forward_getOI: gateway: 0.0.0.0
03/22 08:54:09 TRACE :......router_forward_getOI: route handle: 7f5251c8
03/22 08:54:09 INFO :......rsvp_flow_stateMachine: state RESVED, event T1OUT
03/22 08:54:09 TRACE :.......rsvp_action_nHop: constructing a PATH
03/22 08:54:09 TRACE :.......flow_timer_start: started T1
03/22 08:54:09 TRACE :......rsvp_flow_stateMachine: reentering state RESVED
03/22 08:54:09 TRACE :.......mailslot_send: sending to (9.67.116.99:0)
03/22 08:54:22 TRACE :.....rsvp_event: received event from RAW-IP on interface 9.67.116.98
03/22 08:54:22 TRACE :......rsvp_explode_packet: v=1,flg=0,type=2,cksm=54875,ttl=255,rsv=0,len=84
03/22 08:54:22 TRACE :.......rsvp_parse_objects: STYLE is WF
03/22 08:54:22 INFO :.......rsvp_parse_objects: obj RSVP_HOP hop=9.67.116.99, lih=0
03/22 08:54:22 TRACE :......rsvp_event_mapSession: Session=9.67.116.99:1047:6 exists
03/22 08:54:22 INFO :.......rsvp_flow_stateMachine: state RESVED, event RESV
03/22 08:54:22 TRACE :........flow_timer_stop: Stop T4
03/22 08:54:22 TRACE :........flow_timer_start: Start T4
03/22 08:54:22 TRACE :.......rsvp_flow_stateMachine: reentering state RESVED
03/22 08:54:24 EVENT :..mailslot_sitter: process received signal SIGALRM
03/22 08:54:24 TRACE :.....event_timerT1_expire: T1 expired
03/22 08:54:24 INFO :......router_forward_getOI: Ioctl to query route entry successful
03/22 08:54:24 TRACE :......router_forward_getOI: source address: 9.67.116.98
03/22 08:54:24 TRACE :......router_forward_getOI: out inf: 9.67.116.98
03/22 08:54:24 TRACE :......router_forward_getOI: gateway: 0.0.0.0
03/22 08:54:24 TRACE :......router_forward_getOI: route handle: 7f5251c8
03/22 08:54:24 INFO :......rsvp_flow_stateMachine: state RESVED, event T1OUT
03/22 08:54:24 TRACE :.......rsvp_action_nHop: constructing a PATH
03/22 08:54:24 TRACE :.......flow_timer_start: started T1
03/22 08:54:24 TRACE :......rsvp_flow_stateMachine: reentering state RESVED
03/22 08:54:24 TRACE :.......mailslot_send: sending to (9.67.116.99:0)
03/22 08:54:35 TRACE :......rsvp_event_mapSession: Session=9.6