Important: This
page will be continually updated as I find new work-around or ideas while
working on scripts (any script - shell, windows, or others).
One of my teammates today seemed pretty frustrated while trying to run 'dos2unix' command in batch mode. His script (see below) was almost doing the thing, however instead of updating the file, the content were displayed on screen (stdout).
His script (with issue) that sent output to stdout:
Here is the corrected script, which correctly updates each file under
current directory by converting end of line from Windows format to Unix
format.
As you have noticed, the only thing missing was the last set of '{}', which
basically tells dos2unix to use the same filename for output as per input.
Below is example using 'exec' instead of 'xargs' to achieve the same.
Command reference links: find, xargs,dos2unix
OR
Note: in above example, any occurrence of 'iamold' in 'myfile.txt' will be
replace by 'iamnew' and written in 'myfile.txt.new'. Important thing here is
the variable $replaceme should be in double quote. Below variant does not
work. The variable '$replaceme' will not be expanded.
Command reference links: sed
Here, I'm finding which process/process ID is listening on port 9080. Here is how I can find out.
Note: the following has been tested on CentOS Linux.
1) Using 'netstat -lnp'
2) Using 'lsof -i :<port>'
3) Using ss -ntlp
Run file as:
Running dos2unix in batch mode:
One of my teammates today seemed pretty frustrated while trying to run 'dos2unix' command in batch mode. His script (see below) was almost doing the thing, however instead of updating the file, the content were displayed on screen (stdout).
His script (with issue) that sent output to stdout:
find . -type f -name "*.sh" |
xargs -i dos2unix {}; |
find . -type f -name "*.sh" |
xargs -i dos2unix {} {}; |
find . -type f -name ".sh" -exec
dos2unix {} {} \; |
Using variable in SED:
file="myfile.txt";
|
file="myfile.txt";
|
file="myfile.txt";
|
Finding which process owns/listens on which port
Here, I'm finding which process/process ID is listening on port 9080. Here is how I can find out.
Note: the following has been tested on CentOS Linux.
1) Using 'netstat -lnp'
$> netstat -lnp | grep 9080
|
2) Using 'lsof -i :<port>'
$> lsof -i :9080
|
3) Using ss -ntlp
$> ss -ntlp | grep 9080
|
Retrieving Certificate and Updating kestore file.
Following file show example of retrieving Google certificate from www.google.com and adding it to local key.jks file. script file: retrieveAndUpdateCert.sh
#! /bin/bash
|
Run file as:
$> ./retrieveAndUpdateCert.sh <Your keystore password>
|
AWK numerical processing tricks
1. If you have number with 1000 separator (,) like 84,959, AWK fails to process the number correctly unless you remove the separator (,) from input.
for example:
As seen from the above result, AWK only took the input values prefixed by comma. Fix is simple, just remove the "," from input value. The following line gives the correct result:
$> echo "84,959|34,600" | awk 'BEGIN{FS=OFS="|";}{print $1/1000,$2/1000}'
|
$> echo "84,959|34,600" | awk 'BEGIN{FS=OFS="|";}{gsub(",","",$1);gsub(",","",$2); print $1/1000,$2/1000}'
84.959|34.6
2. If you get some weird result while doing AWK numeric comparison, make sure the value is presented as number not string literal. For example:
$> echo "Is 99 ( ninety nine) higher than 100?" | awk 'BEGIN{FS="(";}{num=substr($1,4,2);if(num >= 100){ print num" is greater than 100"}else{print num" is less than 100"}}'
|
$> echo "Is 99 ( ninety nine) higher than 100?" | awk 'BEGIN{FS="(";}{num=substr($1,4,2)*1;if(num >= 100){ print num" is greater than 100"}else{print num" is less than 100"}}'
|
$> echo "Is 99 ( ninety nine) higher than 100?" | awk 'BEGIN{FS="(";}{num=substr($1,4,2)+0;if(num >= 100){ print num" is greater than 100"}else{print num" is less than 100"}}'
|
AWK printing from specific column/field to the end
In the following example, matrix.csv (comma delimited file) data is piped to awk which processes one row at a time (excluding first header row), first column is a time in milliseconds, so it converts into displayable date and prints, but rest of the columns (starting from 2nd column) require no processing, so it prints as it is.
cat matrix.csv | awk 'BEGIN{FS=OFS=","}{if(NR > 1) {print strftime("%c", ($1 + 500)/1000), substr($0, index($0,$2))}}'
|
Using comma as a delimiter in for loop
By default 'for loop' expects input delimited by space (or tab or newline) character. However, if you need to use ',' (comma), one of the easiest way is to override Internal Field Separator (IFS) value. However, make sure to set it back to the original value. See the script below, it opens a set of firewall ports delimited by comma ','. Before the for loop, we set IFS="," and after the for loop, we set value back to space " ".
#!/bin/sh
|
No comments:
Post a Comment