Ana Sayfa / Linux / Bash Script / Cut Command

Cut Command

Option Description
-b LIST, –bytes=LISTPrint the bytes listed in the LIST parameter
-c LIST, –characters=LISTLIST Print characters in positions specified in LIST parameter
-f LIST, –fields=LISTPrint fields or columns
-d DELIMITERUsed to separate columns or fields

In Bash, the cut command is useful for dividing a file into several smaller parts.

Show the first column of a file

Suppose you have a file that looks like this

John Smith 31
Robert Jones 27
...

This file has 3 columns separated by spaces. To select only the first column, do the following.

cut -d ' ' -f1 filename

Here the -d flag, specifies the delimiter, or what separates the records. The -f flag specifies the field or column number. This will display the following output

John
Robert

Show columns x to y of a file

Sometimes, it’s useful to display a range of columns in a file. Suppose you have this file


Apple California 2017 1.00 47
Mango Oregon 2015 2.30 33


To select the first 3 columns do


cut -d ‘ ‘ -f1-3 filename


This will display the following output


Apple California 2017
Mango Oregon 2015

Global and local variables

By default, every variable in bash is global to every function, script and even the outside shell if you are declaringyour variables inside a script.


If you want your variable to be local to a function, you can use local to have that variable a new variable that is independent to the global scope and whose value will only be accessible inside that function.

Global variables

var="hello"
function foo(){
echo $var
}
foo

Will obviously output “hello”, but this works the other way around too:

function foo() {
var="hello"
}
foo
echo $var

Local variables

function foo() {
local var
var="hello"
}
foo
echo $var

Will output nothing, as var is a variable local to the function foo, and its value is not visible from outside of it.

Mixing the two together

var="hello"
function foo(){
local var="sup?"
echo "inside function, var=$var"
}
foo
echo "outside function, var=$var"

Will output:

inside function, var=sup?
outside function, var=hello

Bunada Göz Atın

PHP Types

Type Comparison There are two types of comparison: loose comparison with == and strict comparison …

Bir yanıt yazın

E-posta adresiniz yayınlanmayacak. Gerekli alanlar * ile işaretlenmişlerdir