Posts (page 187)
-
3 min readTo get only the first occurrence of a pattern in a bash script, you can use the grep command with the -m option followed by the number 1. This option tells grep to stop reading the input file after the first match is found. For example, you can use the following command: grep -m 1 "pattern" filename This command will search for the pattern in the specified file and output only the first occurrence of the pattern. You can then use this output in further processing within your bash script.
-
3 min readTo concatenate a string to a comma-separated element in bash, you can use the following syntax: myString="hello" myElement="apple,banana,orange" myElement="$myElement,$myString" echo $myElement In this example, we first have a string "hello" stored in the variable myString and a comma-separated element "apple,banana,orange" stored in the variable myElement.
-
3 min readIn Bash, the asterisk (*) is known as a wildcard character that can be used for pattern matching in file and directory names. When the asterisk is used in a command, it represents any sequence of characters or none at all.For example, if you want to list all files in a directory that start with "test", you can use the command "ls test*". This will match all files that start with "test" followed by any characters.
-
4 min readTo replace * with #* in Bash, you can use the following command: echo "original string" | sed 's/\*/#*/g' This command uses the sed command to search for instances of * and replace them with #* in a given string. The s command in sed stands for substitute, and the g at the end of the command tells sed to make the substitution globally (i.e., for all instances of * in the string).
-
7 min readIn bash, you can use a combination of commands such as awk or grep to print a line when a certain text pattern changes. One way to achieve this is by using the awk command with the print function to output the lines that match the desired text pattern.For example, if you have a file with lines containing different text patterns, you can use the following command to print a line when the text pattern changes: awk 'a!=$0{print}{a=$0}' filename.
-
6 min readTo get memory usage for a specific process ID (PID) in a variable using bash, you can use the following command: mem_usage=$(ps -p $PID -o %mem | awk 'NR==2 {print $1}') Replace $PID with the process ID for which you want to get the memory usage.
-
5 min readTo split a string into multiple lines in bash, you can use the tr command to replace a delimiter with a newline character. For example, if you have a string hello|world|foo|bar where | is the delimiter, you can use the following command: echo "hello|world|foo|bar" | tr '|' '\n' This will output: hello world foo bar You can also use the awk command to achieve the same result.
-
5 min readTo add a new field in a yaml file using a bash script, you can use tools like yq or sed to manipulate the yaml file. For example, using yq, you can add a new key-value pair to the yaml file like this: yq eval '.newField: "value"' file.yaml > newFile.yaml This will add a new field named "newField" with the value "value" to the yaml file.
-
3 min readTo delete added lines between two files in bash, you can use the diff command to compare the two files and then use grep to filter out the added lines. You can do this by running the following command: diff file1.txt file2.txt | grep '^> ' | cut -c3- | comm -23 - <(sort -u file1.txt) | sed '/^>/d' > deleted_lines.txt This command will compare file1.txt and file2.
-
4 min readIn Bash, you can assign different outputs to different variables by using command substitution along with the assignment operator. Command substitution allows you to capture the output generated by a command and assign it to a variable.To assign different outputs to different variables, you can use command substitution within a variable assignment statement. For example, you can assign the output of a command to a variable by enclosing the command within $() or backticks (`).
-
4 min readTo grep all keywords from an array in a bash script, you can iterate through the array and use the grep command to search for each keyword. You can do this by looping through the array elements and using grep -w to match whole words. Here is an example script that demonstrates this: #.
-
5 min readTo properly assign a null value from a bash script to MySQL, you can use the following method. When inserting data into a MySQL database and you want to assign a null value to a column, you can do so by using the keyword "NULL" (in uppercase) without any quotes.