dropdown menu

SED: stream oriented editor

sed 'instructions' filename
command | sed 'edit-instructions'
sed -f command.file filename
sed 's/...' <f1 >f2                      <--it will read from file f1 the input and will write to file f2 the output

.         Match any single character
*         Match any number of single characters
[…]       Matches any one of the class of characters enclosed between the brackets
          eg. [a-zA-Z0-9] match any numeric, lower or uppercase character
^         The beginning of a line
$         The end of a line
\         Escapes the special character that follows
\{n,m\}   at least m and no more than n occurrences of previous pattern ("quoted braces")
          eg. 'r\{2\}‘ two consecutive r’s
          eg. '[abc]\{2,4\}‘ two, three or four occurrences of any combination of the characters a, b and c consecutively
       it holds the valuse what was searched for

echo "a \nb\nc c c" > f1                 <--creates a file (f1) with the given content (\n is the new line character)
echo "one fine day" | sed 's/one/(&)/'   <--it will put parenthesis around the word 'one' (& is the result of the search pattern)


substitute ('s' character):

print "Alexander" | sed 's:e:E:g'               <--it is the same as in the picture (sometimes : is better to see it clearer)
sed s/logfile/logfile.txt/ sed.txt              <--it will replace logfile to logfile.txt (but only once in a line)
                                               (sed s/REGULAREXPRESSION/REPLACEMENTSTRING/flags INPUT_FILE)
sed s/logfile/logfile.txt/g sed.txt             <--it will replace all oocurences (if there are more in 1 line)
sed  /nodes/s/logfile/logfile_nodes/ sed.txt    <--it will grep for "nodes" and change in that line the "logfile" to "logfile_nodes"
sed s/[a-z]*:/new_\&/g sed.txt                  <--it will look for words which starts alphabetical characters and ends with ":" and
                                                what was found is stored in "&" and put a "new_" prefix


delete ('d' character):
sed '/^$/d' <f2                                 <--delete all blank lines (it is the same: cat f2 |sed '/^$/d')
cat f2 | sed 's/one//g' | sed '/^$/d'           <--deletes the word one and the empty lines from the file f2
cat f2 | sed '2,4 d'                            <--deletes lines 2 to 4


AWK (first letter of the 3 persons who wrote it)


awk '{print}' /etc/passwd                       <--prints the file /etc/passwd
awk '{print "hello"}' /etc/passwd               <--prints "hello" for every line of /etc/passwd (not the content of the lines)
awk -F":" '{ print $1 }' /etc/passwd            <--prints the first column of the file /etc/passwd (delimiter is ":")
awk -F":" '{ print $1 " " $3 }' /etc/passwd     <--prints the first and 3rd column between a space
ls -l | awk '{ print$3":"$9}'                   <--prints 3rd and 9th rows separated by the character :
ls -l | awk '/Screen/{print$3" "$9}'            <--it will grep for "Screen" in the output of ls -l and print...
ls -l | awk '$6 == "Sep"'                       <--it will list only those lines which has in the 6th column "Sep"

awk 'NR==2' /root/settitle 
                     <--displays the 2nd line in the file:/root/setitle
df -g | awk '{sum = sum + $3} END {print sum}'  <--sums up filesystems in GB (3rd column in df)

awk '/'"$i"'/ {print $0}'                       <--using a variable ($i) in awk for searching
awk '{print "'"$i"'"}'                          <--using a variable ($i) in awk for printing

5 comments:

  1. Awesome :) your article is very helpful...

    ReplyDelete
  2. High,I want to change the structure A-B-C to C-B-A with awk,could you help me? Thx!

    ReplyDelete
    Replies
    1. You want to interchange the first and third columns. Then try this
      awk 'BEGIN{FS="-"} {print $3"-"$2"-"$1}' filename.

      If you are interested in awk command, then check this Awk Command

      Delete