sed – 20 examples to remove / delete characters from a file

In this article, we will see the examples of how to remove or delete characters from a file. The syntax of sed command replacement is:

$ sed 's/find/replace/' file

This sed command finds the pattern and replaces with another pattern. When the replace is left empty, the pattern/element found gets deleted.

Let us consider a sample file as below:

$ cat file
Linux
Solaris
Ubuntu
Fedora
RedHat

1. To remove a specific character, say ‘a’

$ sed 's/a//' file
Linux
Solris
Ubuntu
Fedor
RedHt

This will remove the first occurence of ‘a’ in every line of the file. To remove all occurences of ‘a’ in every line,

$ sed 's/a//g' file

2. To remove 1st character in every line:

$ sed 's/^.//' file
inux
olaris
buntu
edora
edHat

.(dot) tries to match a single character. The  ^ tries to match a pattern(any character) in the beginning of the line.   Another way to write the same:

$ sed 's/.//' file

This tells to replace a character with nothing. Since by default, sed starts from beginning, it replaces only the 1st character since ‘g’ is not passed.

3. To remove last character of every line :

$ sed 's/.$//' file
Linu
Solari
Ubunt
Fedor
RedHa

The $ tries to match a pattern in the end of the line.

4. To remove the 1st and last character of every line in the same command:

$ sed 's/.//;s/.$//' file
inu
olari
bunt
edor
edHa

Two commands can be given together with a semi-colon separated in between.

5. To remove first character only if it is a specific character:

$ sed 's/^F//' file
Linux
Solaris
Ubuntu
edora
RedHat

This removes the 1st character only if it is ‘F’.

6. To remove last character only if it is a specific character:

$ sed 's/x$//' file
Linu
Solaris
Ubuntu
Fedora
RedHat

This removed the last character only if it s ‘x’.

7. To remove 1st 3 characters of every line:

$ sed 's/...//' file
ux
aris
ntu
ora
Hat

A single dot(.) removes 1st character, 3 dots remove 1st three characters.

8. To remove 1st n characters of every line:

$ sed -r 's/.{4}//' file
x
ris
tu
ra
at

.{n} -> matches any character n times, and hence the above expression matches 4 characters and deletes it.

9. To remove last n characters of every line:

$ sed -r 's/.{3}$//' file
Li
Sola
Ubu
Fed
Red

10. To remove everything except the 1st n characters in every line:

$ sed -r 's/(.{3}).*/\1/' file
Lin
Sol
Ubu
Fed
Red

.* -> matches any number of characters, and the first 3 characters matched are grouped using parantheses. In the replacement, by having \1 only the group is retained, leaving out the remaining part.

11. To remove everything except the last n characters in a file:

$ sed -r 's/.*(.{3})/\1/' file
nux
ris
ntu
ora
Hat

Same as last example, except that from the end.

12. To remove multiple characters present in a file:

$ sed 's/[aoe]//g' file
Linux
Slris
Ubuntu
Fdr
RdHt

To delete multiple characters, [] is used by specifying the characters to be removed. This will remove all occurences of the characters a, o and e.

13. To remove a pattern  :

$ sed 's/lari//g' file
Linux
Sos
Ubuntu
Fedora
RedHat

Not just a character, even a pattern can be removed. Here, ‘lari’ got removed from ‘Solaris’.

14. To delete only nth occurrence of a character in every line:

$ sed 's/u//2' file
Linux
Solaris
Ubunt
Fedora
RedHat

By default, sed performs an activity only on the 1st occurence. If n is specifed, sed performs only on the nth occurence of the pattern. The 2nd ‘u’ of ‘Ubuntu’ got deleted.

15. To delete everything in a line followed by a character:

$ sed 's/a.*//' file
Linux
Sol
Ubuntu
Fedor
RedH

16. To remove all digits present in every line of a file:

$ sed 's/[0-9]//g' file

[0-9] stands for all characters between 0 to 9 meaning all digits, and hence all digits get removed.

17. To remove all lower case alphabets present in every line:

$ sed 's/[a-z]//g' file
L
S
U
F
RH

[a-z] represents lower case alphabets range and hence all lower-case characters get removed.

18. To remove everything other than the lower case alphabets:

$ sed 's/[^a-z]//g' file
inux
olaris
buntu
edora
edat

^ inside square brackets negates the condition. Here, all characters except lower case alphabets get removed.

19. To remove all alpha-numeric characters present in every line:

$ sed 's/[a-zA-Z0-9]//g' file

All alpha-numeric characters get removed.

20. To remove a character irrespective of the case:

$ sed 's/[uU]//g' file
Linx
Solaris
bnt
Fedora
RedHat

By specifying both the lower and upper case character in brackets is equivalent to removing a character irrespective of the case.

Original article here, from The UNIX School.

Linux Increase Networking Performance Tuning Network Stack (Buffers Size)

Starting a Stress Test to improve performance, I reach some limits when the system was under intense fire up. By default the Linux network stack is not configured for high speed large file transfer across WAN links. This is done to save memory resources. You can easily tune Linux network stack by increasing network buffers size for high-speed networks that connect server systems to handle more network packets.

The default maximum Linux TCP buffer sizes are way too small. TCP memory is calculated automatically based on system memory; you can find the actual values by typing the following commands:

$ cat /proc/sys/net/ipv4/tcp_mem

The default and maximum amount for the receive socket memory:

$ cat /proc/sys/net/core/rmem_default
$ cat /proc/sys/net/core/rmem_max

The default and maximum amount for the send socket memory:

$ cat /proc/sys/net/core/wmem_default
$ cat /proc/sys/net/core/wmem_max

The maximum amount of option memory buffers:

$ cat /proc/sys/net/core/optmem_max

 Tuning the Values

Set the max OS send buffer size (wmem) and receive buffer size (rmem) to 12 MB for queues on all protocols. In other words set the amount of memory that is allocated for each TCP socket when it is opened or created while transferring files:

WARNING! The default value of rmem_max and wmem_max is about 128 KB in most Linux distributions, which may be enough for a low-latency general purpose network environment or for apps such as DNS / Web server. However, if the latency is large, the default size might be too small. Please note that the following settings going to increase memory usage on your server.

Now, as root user…

# echo 'net.core.wmem_max=12582912' >> /etc/sysctl.conf
# echo 'net.core.rmem_max=12582912' >> /etc/sysctl.conf

You also need to set minimum size, initial size, and maximum size in bytes:

# echo 'net.ipv4.tcp_rmem= 10240 87380 12582912' >> /etc/sysctl.conf
# echo 'net.ipv4.tcp_wmem= 10240 87380 12582912' >> /etc/sysctl.conf

Turn on window scaling which can be an option to enlarge the transfer window:

# echo 'net.ipv4.tcp_window_scaling = 1' >> /etc/sysctl.conf

Enable timestamps as defined in RFC1323:

# echo 'net.ipv4.tcp_timestamps = 1' >> /etc/sysctl.conf

Enable select acknowledgments:

# echo 'net.ipv4.tcp_sack = 1' >> /etc/sysctl.conf

By default, TCP saves various connection metrics in the route cache when the connection closes, so that connections established in the near future can use these to set initial conditions. Usually, this increases overall performance, but may sometimes cause performance degradation. If set, TCP will not cache metrics on closing connections.

# echo 'net.ipv4.tcp_no_metrics_save = 1' >> /etc/sysctl.conf

Set maximum number of packets, queued on the INPUT side, when the interface receives packets faster than kernel can process them.

# echo 'net.core.netdev_max_backlog = 5000' >> /etc/sysctl.conf

Now reload the changes:

# sysctl -p

Use tcpdump to view changes for eth0, eth1 or wlan0, or…

# tcpdump -ni eth0

See the results, enjoy it!

 

 

 

 

Add Proxy Exception In Ubuntu 12.04

Ubuntu 12.04 comes with shiny graphical tool to setup system wide global proxy. But as seen in the picture below there is no way to add exception or ignored host.

To overcome this limitation we have to install dconf Editor. You can install by clicking the previous link or by typing sudo apt-get install dconf-tools in terminal. Please note that though the software is call dconf editor its packaged as dconf-tools.

Once installed open dconf editor and navigate to system -> proxy. From here you can add ignore-hosts as string and as seen in the picture below.

You may need to log out and log in for the changes to take effect… just to avoid any noise!

increase ulimit in CentOS

By default the ulimit is set to 1024 only, first you need to increase system wise with adding this one in /etc/sysctl.conf

“fs.file-max = 65536″

and then…

# sysctl -p

switch to /etc/security/limits.conf and add the following lines:

*     hard      nofile     65536
*     soft      nofile     16384

switch to user for which you need to increase the file-max for, with this the default for all users will be 16384, you can increase with ulimit -n XXXX now.

 

nautilus-open-terminal, a terminal quick launch

Tonite it’s getting late but I wanted to post something that is useful for quickly getting to the shell from any GUI location. The package nautilus-open-terminal does just what you might guess it does. It allows you to launch a gnome-terminal from a right-click within nautilus.

You might remember I blogged about something similar long-long ago with nautilus scripts.  This is based on the same idea, but now wrapped in a nice shiny deb package.  From the package description:

"Nautilus plugin for opening terminals in arbitrary local paths nautilus-open-terminal is a proof-of-concept Nautilus extension which allows you to open a terminal in arbitrary local folders."

To install this quick-launch to the terminal simply run:

sudo apt-get install nautilus-open-terminal

You may need to restart gnome / nautilus for the change to take effect, but afterwards you’ll have a “open terminal” button on your right-click menu anywhere within nautilus or gnome-desktop area.  Enjoy.