Printing a specific line in a file using sed:
sed -n '1258p' file.txt
Printing a range of lines in a file using sed:
sed -n '1258,1262p' file.txt
Deleting a single line from a file using sed:
sed -e '1258d' file.txt > newfile.txt
Get length of string in Korn Shell (ksh) script:
printf $str_variable | wc -c
Check if variable is null or empty in Korn Shell (ksh) script:
if [[ -z "$variable_name" ]] then echo "Very null." fi
Check if a variable is not equal to a numeric value in a Korn Shell (ksh) script:
if [[ $numeric_variable -ne 1 ]] then echo "Not equal to 1." fi
Using the OR operator in a Korn Shell (ksh) if statement:
if [[ -z "$variable_name" || $numeric_variable -ne 1 ]] then echo "variable_name is null or numeric_variable is not equal to 1." fi
Check if last command was successful in Korn Shell (ksh):
if [[ $? = 0 ]] then echo "Success." else echo "Failed." fi
Find and replace all occurrences of a string using sed (replace "dog" with "cat" and "paw" with "claw"):
sed -e 's/dog/cat/g' -e 's/paw/claw/g' input_file.txt > output_file.txt
How to accept user input in a Korn Shell (ksh) script:
echo "Enter your pet's name (on the line below):" read pet_name printf "Enter your pet's name (on this line): " read pet_name printf "Enter your pet's name (on this line and in a single statement): " && read pet_name