Search This Blog

sed: replace formatted numbers

Example remove date string:
s="1234 2007-12-21 11:22:30 1.txt"
s=$(echo $s | sed 's/[0-9]\{4\}-[0-9]\{2\}-[0-9]\{2\}\s*//')
echo $s
# 1234 1.txt

Shell script: execute a string as command

eval is what we need:
string="echo \"Shell scripting is cool.\""
eval $string

Shell script: set delimiter and read line from file

while IFS=$'\t' read col1 col2; do
    echo "column 1: ${col1}"
done < /path/to/input.txt
The above code read from tab delimited text file.

Shell script: loop through lines from file or command output

You can pipe command output to a while loop
echo /path/to/1.txt | while IFS=$'\t' read a b; do
   echo "${b}, ${a}"
done

Or loop through a text file:
while IFS=$'\t' read a b; do
   echo "${b}, ${a}"
done < /path/to/1.txt

awk: re-order columns using printf

echo 1.txt | awk 'BEGIN{FS="\t"} {printf "%s\t%s\n", $2, $1}'

Shell script: get file size

stat -c %s /path/to/file
or
#!/bin/bash

path=/path/to/file
size=$(stat -c %s ${path})
if [[ $size -eq 0 ]]; then
    echo "${path} is a zero-size file."
fi
Note: If path directs to a directory, it will not fail nor return 0. It returns the block size associated with the directory. See this for more details.

Shell script: remove leading and trailing character

s="\"Shell scripting is interesting.\""
echo $s

# ${s%\"} will remove the suffix " (escaped with a backslash to prevent shell interpretation)
s=${s%\"}

# ${s#\"} will remove the prefix " (escaped with a backslash to prevent shell interpretation)
s=${s#\"}
echo $s

Sed: multiple substitutes in one sed command

Delimit commands with semicolon:
echo "John like apples." | sed 's/apple/pear/;s/John/David/'
Another example, remove leading and trailing quotes:
echo "'Sed is cool.'" | sed 's/^'//;s/'$//'

A shell script to check files in SWIFT object storage