Delete lines from file with SED or AWK

前端 未结 3 839
旧时难觅i
旧时难觅i 2020-12-09 04:57

Ive seen many variations, very confused on how to solve these 3 problems.

  1. deleting all rows except the first from a file
  2. deleting a row from file with
3条回答
  •  自闭症患者
    2020-12-09 05:25

    you can just use bash if your system has it. The basic idea behind is to set a count and incrementing this count while iterating the file.

    1) deleting all rows except the first from a file

    read -r line < file; echo "$line" > temp && mv temp file
    

    2) deleting a row from file with a line number

    declare -i count=0
    while read -r line
    do
      ((count++))
      case "$count" in
        10) continue;;
        * ) echo "$line";;
      esac
    done < file > temp && mv temp file
    

    3) deleting rows from a file with a range of line numbers eg from 10 to 20

    declare -i count=0
    while read -r line
    do
      ((count++))
      if (( $c < 10 && $c > 20 ));then
        echo "$line";;
      fi
    done < file > temp && mv temp file
    

提交回复
热议问题