sed cut awk

[root@ansible1 ~]# vim example
I want to learn java. Learn java. Learn java
java is the best
java forever

Replacing string

[root@ansible1 ~]# sed ‘s/java/guava/’ example.txt
I want to learn guava. Learn java. Learn java
guava is the best
guava forever
Replacing the nth occurrence of a pattern in a line

[root@ansible1 ~]# sed ‘s/java/guava/2’ example.txt
I want to learn java. Learn guava. Learn java
java is the best
java forever

Replacing all the occurrences of a pattern in a line

root@ansible1 ~]# sed ‘s/java/guava/g’ example.txt
I want to learn guava. Learn guava. Learn guava
guava is the best
guava forever

Replacing from nth occurrence to all occurrences in a line

[root@ansible1 ~]# sed ‘s/java/guava/2g’ example.txt
I want to learn java. Learn guava. Learn guava
java is the best
java forever
Duplicating the replaced line with /p flag

[root@ansible1 ~]# sed ‘s/java/guava/p’ example.txt
I want to learn guava. Learn java. Learn java
I want to learn guava. Learn java. Learn java
guava is the best
guava is the best
guava forever
guava forever
Replacing string on a specific line number

[root@ansible1 ~]# sed ‘2 s/java/guava/’ example.txt
I want to learn java. Learn java. Learn java
guava is the best
java forever

Replacing on a line which matches a pattern

[root@ansible1 ~]# sed ‘/java/ s/java/guava/’ example.txt
I want to learn guava. Learn java. Learn java
guava is the best
guava forever

Deleting lines

[root@ansible1 ~]# sed ‘2 d’ example.txt
I want to learn java. Learn java. Learn java
java forever
Duplicating lines

[root@ansible1 ~]# sed ‘p’ example.txt
I want to learn java. Learn java. Learn java
I want to learn java. Learn java. Learn java
java is the best
java is the best
java forever
java forever

Changing the slash (/) delimiter

[root@ansible1 ~]# sed ‘s_https://_www_’ example.txt
I want to learn java. Learn java. Learn java
java is the best
java forever
Using & as the matched string

[root@ansible1 ~]# sed ‘s/java/{&}/’ example.txt
I want to learn {java}. Learn java. Learn java
{java} is the best
{java} forever
Using 1,2 and so on to 9

[root@ansible1 ~]# sed ‘s/\(java\)/\1\1/’ example.txt
I want to learn javajava. Learn java. Learn java
javajava is the best
javajava forever

========================
Printing only the replaced lines

[root@ansible1 ~]# sed -n ‘s/java/guava/p’ example.txt
I want to learn guava. Learn java. Learn java
guava is the best
guava forever

Using sed as grep

[root@ansible1 ~]# grep ‘java’ example.txt
I want to learn java. Learn java. Learn java
java is the best
java forever

Adding a line after a match is found.

[root@ansible1 ~]# sed ‘/java/ a “Add a new line”‘ example.txt
I want to learn java. Learn java. Learn java
“Add a new line”
java is the best
“Add a new line”
java forever
“Add a new line”

Adding a line before a match

[root@ansible1 ~]# sed ‘/java/ i “New line”‘ example.txt
“New line”
I want to learn java. Learn java. Learn java
“New line”
java is the best
“New line”
java forever
Changing a line

[root@ansible1 ~]# sed ‘/java/ c “Change line”‘ example.txt
“Change line”
“Change line”
“Change line”

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