Thursday, May 9, 2019

sed, awk command line

1. Insert text from begining and special line (from line >= 57) with awk
  • awk 'FNR>=57,$0="allow "$0{print}' /etc/nginx/akamai_https.conf > test2.txt
  • sed -i -e 's/^/text\ to \ insert/g' filename
2. Replace all IP "xxx.xxx.xxx.xxx;" => "xxx.xxx.xxx.0/24;"
  • sed -e 's/\.[0-9]\+;/\.0\/24;/g' /tmp/ips.txt > /tmp/new_ips.txt
    Here is content in file:
    access_log /var/log/test.log;
    I want to insert before the ; character
  • sed -i -e "s/\(access_log.*.log\)\(;\)/\1\ combined\ if\=\$loggable\2/g" filename.txt
3. Replace string in all file in dir
  • ls -1 <dir> | xargs -n1 -i%f sed -i -e 's/old_string/new_string/g' '%f'
4. Sed with variable with forward slash in shell script
  • vari1=/Users/huytn/test
  • sed -i -e "s/old_string/${vari1//\//\\/}/g" /path/to/file







No comments: