Add a Column of Numbers in Bash

Satish Kumar
Updated on 03-Jan-2023 11:00:17

2K+ Views

Overview This article examines how to total up numeric columns of data in a bash shell, looking at the bash tools available for the task and comparing their speed. Using The awk Tool We’ll start by calculating the sum of the values in a particular column using the awk (awk) program. $ awk '{Total=Total+$1} END{print "Total is: " Total}' numbers.csv Total is: 49471228 Let’s now take a look at the timing using the “time” command − $ time awk '{Total=Total+$1} END{print "Total is: " Total}' numbers.csv Total is: 49471228   real 0m0.228s user 0m0.141s sys 0m0.047s When The ... Read More

Extract Substring Using Linux Bash

Satish Kumar
Updated on 03-Jan-2023 10:58:15

6K+ Views

Overview Extracting a substring from a string is a basic and common operation of text processing in Linux. We're looking at different ways to extract substrings from strings using the Linux command line here. Extracting an Index-Based Substring Let’s first take a quick glance at how to extract index-based substring using four different methods. Using the cut command Using the awk command Using Bash’s substring expansion Using the expr command Next, we’ll see them in action. Using The cut Command We can extract characters starting at position N through position M from the input string using "cut" command. ... Read More

Change Install Directory with Make Install

Satish Kumar
Updated on 03-Jan-2023 10:55:46

5K+ Views

Overview Generally, packages are installed in the default directory. Depending on the system's Linux version, it might be necessary to use a different directory such as /usr or /user/local. We might also want to install a particular software application for a single user rather than for the entire system. We'll take a look at how to change where packages get installed by running make uninstall. Using ./configure Parameters When using autoconf/automake to build software packages, a configure script with several standard parameters and (sometimes) additional custom parameters is provided. Some packages don't use autoconf but they usually offer a compatible ... Read More

Move All Files Except One on Linux

Satish Kumar
Updated on 03-Jan-2023 10:52:58

5K+ Views

Introduction If you're working with Linux, there might be times when you'll want to copy several files at once and then remove some of them later. We're going to take a closer at several different methods for achieving such results. Renaming The Unwanted File You can rename the unwanted file so that it becomes a “.” (dot) file means hidden files, which means that mv won't be able to see it. After renaming the unwanted file using the asterisks, we’ll then use the regular expression to remove the rest of the files. /source_dir$ mv file5 .file5 /source_dir$ mv * ~/target_dir/ ... Read More

Load Environment Variables in a Cron Job

Satish Kumar
Updated on 03-Jan-2023 10:51:02

6K+ Views

Overview When crontab runs a command, it doesn't read any environment variables from files like ~/.bashrc, ~/.bash_profile, etc. Because cron runs tasks from a non-interactive, non-login shell, it doesn't need an interactive user Some applications require environmental variables to function correctly. We’ll discuss different methods for loading environmental variables from a crontab. Setting the BASH_ENV Variable We can set environment variables for our shell scripts by using the BASH_ENV variable. We set it up so that when we run our jobs, they're executed by a shell script. We can set up our shell so that when we open a new ... Read More

Use tmux on Linux

Satish Kumar
Updated on 03-Jan-2023 10:48:43

512 Views

Overview Tmux is a terminal multiplexing utility for Unix systems. It provides an interface between several programs running simultaneously on one computer. Tmux allows us to detach from any terminal sessions without killing them. We can then reattached to any of the terminal sessions later on. We’ll learn the tmux terminal emulator in Linux. Specifically, we’ll examine some of its features and commands. Installation You can install tmui on Debian-based Linux systems using the apt-get package manager. $ sudo apt-get update -qq $ sudo apt-get install -y tmux We can also use the yum command line tool to download ... Read More

Precision String Format Specifier in Swift

Nitin Aggarwal
Updated on 03-Jan-2023 10:32:41

2K+ Views

It is sometimes necessary to format strings in a custom format in order to use them in an application. For example, you can format product prices, decimal-point numbers, etc. In Swift, you can use precision specifiers to specify the number of decimal places or the number of characters to be displayed in a string. Example 1 To specify the number of decimal places for a floating-point number, you can use the %.nf format specifier, where n is the number of decimal places., import Foundation let productPrice = 300.3456789 let formattedPrice = String(format: "%.2f", productPrice) print("The formatted price is: ", formattedPrice) ... Read More

Use a Background Thread in Swift

Nitin Aggarwal
Updated on 03-Jan-2023 10:26:51

8K+ Views

In this article, you will learn how you can perform a task in the background using a background thread in the Swift language. Swift provides us with several ways to perform background tasks. One popular option of theirs is GCD (generally called Grand Central Dispatch), which is a low-level API for managing concurrently in the Swift language. The GCD provides us with a global queue for creating background threads. You can invoke the DispatchQueue.global() method to get an instance of a global dispatch queue. Using the same instance, you can use the async() method to execute a block of code ... Read More

Define Optional Methods in Swift Protocol

Nitin Aggarwal
Updated on 03-Jan-2023 10:25:17

1K+ Views

This article will explain to you how to define optional methods in the protocol. Before diving into making optional methods in the protocol, you will first learn what a protocol is and how to declare one in Swift. What is The Protocol? A protocol is a type that defines a group of methods or properties. Basically, you can define a blueprint of methods to specify behavior. A protocol is similar to interfaces in other programming languages. Syntax Here is the syntax of a simple protocol in Swift − protocol { // Properties // ... Read More

Get Version and Build Number of an Application using Swift

Nitin Aggarwal
Updated on 03-Jan-2023 10:22:21

6K+ Views

You often need to deal with the version and build number of the iOS application. It helps you to perform debugging operations on the server side. You can check which version or build the client (IOS app user) has. Based on that, you can debug the issues coming from the client side. Version and build numbers might be needed to display to the user to verify what version and build are currently running by the end user. To get the app version and build number in Swift, you can use the Bundle class, which provides access to the app's bundle ... Read More

Advertisements