sed examples

eg. 1. we have lots of records like:

5173230|1947953|Łódz
5317879|2207629|Łomza

we want only the last column:

execute:
cat file | sed ‘s/.*|//g’ (the same as cat file | sed s/.*|//g)

result:
Łódz
Łomza

e.g. 2, delete all files that are not added to svn.
before:
svn st
? provider/mapdb.script
? provider/mysql-clean.script

execute:
svn st | awk ‘{print $2}’ | xargs rm

result:
svn st

(no files)

e.g. 3.
A pom.xml file has a line with contents:
<version>0.9<\version>
We want to get the value between the version tags:

execute:
cat pom.xml | grep version | head -1 | sed ‘s/.*<version>\(.*\)<\version>.*/\1/’

result:
0.9

e.g. 4
Parse ip address from the ifconfig output

before:
/sbin/ifconfig
eth0 Link encap:Ethernet HWaddr 00:15:C5:F2:55:D8
inet addr:192.168.49.140 Bcast:192.168.49.255 Mask:255.255.255.0
inet6 addr: fe80::215:c5ff:fef2:55d8/64 Scope:Link

execute:
/sbin/ifconfig | grep Bcast | sed ‘s/.*addr:\([0-9\.]*\).*/\1/’

result:
192.168.49.140

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s