Skip to main content
deleted 180 characters in body
Source Link
rexkogitans
  • 1.4k
  • 11
  • 17

The problem with your solution is that you invoke grep for each line. In fact, grep parses each line, too. So, for a file with n lines, these lines are parsed n^2 times, and loading grep is quite an expensive call.

Use a one-line buffer, in this example called PrevLine, like this:

#!/bin/bash
PrevLine=''CurLine=''
isFirstLine=true
while IFS='' read -r Line;LineBelow; do
  echo "Current line: ${Line}"
  if $isFirstLine; then
    echo "This is the first line, so no previous."
  else
    echo "Previous"Current line read: ${PrevLineCurLine}"
    echo "Line below: ${LineBelow}"
  fi
  PrevLine="$CurLine="${LineLineBelow}"
  isFirstLine=false
done <"$1"

In fact, assigning true to isFirstLine is a string assignment, and just mentioning $isFirstLine (in the if-condition) is the execution of the command of this string. Since true and false are bash-builtins, they can be used directly without significant speed impact, but with a high increase of readability.

If you want to swap the lines, just move the line echo "Current line: ${Line}" below the if-block:

  fi
  echo "Current line: ${Line}"
  PrevLine="${Line}"

The last line mentions $1 as input file name, so invoke with:

./test.sh inputfile.txt

The problem with your solution is that you invoke grep for each line. In fact, grep parses each line, too. So, for a file with n lines, these lines are parsed n^2 times, and loading grep is quite an expensive call.

Use a one-line buffer, in this example called PrevLine, like this:

#!/bin/bash
PrevLine=''
isFirstLine=true
while IFS='' read -r Line; do
  echo "Current line: ${Line}"
  if $isFirstLine; then
    echo "This is the first line, so no previous."
  else
    echo "Previous line: ${PrevLine}"
  fi
  PrevLine="${Line}"
  isFirstLine=false
done <"$1"

In fact, assigning true to isFirstLine is a string assignment, and just mentioning $isFirstLine (in the if-condition) is the execution of the command of this string. Since true and false are bash-builtins, they can be used directly without significant speed impact, but with a high increase of readability.

If you want to swap the lines, just move the line echo "Current line: ${Line}" below the if-block:

  fi
  echo "Current line: ${Line}"
  PrevLine="${Line}"

The last line mentions $1 as input file name, so invoke with:

./test.sh inputfile.txt

The problem with your solution is that you invoke grep for each line. In fact, grep parses each line, too. So, for a file with n lines, these lines are parsed n^2 times, and loading grep is quite an expensive call.

Use a one-line buffer, in this example called PrevLine, like this:

#!/bin/bash
CurLine=''
isFirstLine=true
while IFS='' read -r LineBelow; do
  if $isFirstLine; then
    echo "This is the first line, so no previous."
  else
    echo "Current line read: ${CurLine}"
    echo "Line below: ${LineBelow}"
  fi
  CurLine="${LineBelow}"
  isFirstLine=false
done <"$1"

In fact, assigning true to isFirstLine is a string assignment, and just mentioning $isFirstLine (in the if-condition) is the execution of the command of this string. Since true and false are bash-builtins, they can be used directly without significant speed impact, but with a high increase of readability.

The last line mentions $1 as input file name, so invoke with:

./test.sh inputfile.txt
added 180 characters in body
Source Link
rexkogitans
  • 1.4k
  • 11
  • 17

The problem with your solution is that you invoke grep for each line. In fact, grep parses each line, too. So, for a file with n lines, these lines are parsed n^2 times, and loading grep is quite an expensive call.

Use a one-line buffer, in this example called PrevLine, like this:

#!/bin/bash
PrevLine=''
isFirstLine=true
while IFS='' read -r Line; do
  echo "Current line: ${Line}"
  if $isFirstLine; then
    echo "This is the first line, so no previous."
  else
    echo "Previous line: ${PrevLine}"
  fi
  PrevLine="${Line}"
  isFirstLine=false
done <"$1"

In fact, assigning true to isFirstLine is a string assignment, and just mentioning $isFirstLine (in the if-condition) is the execution of the command of this string. Since true and false are bash-builtins, they can be used directly without significant speed impact, but with a high increase of readability.

If you want to swap the lines, just move the line echo "Current line: ${Line}" below the if-block:

  fi
  echo "Current line: ${Line}"
  PrevLine="${Line}"

The last line mentions $1 as input file name, so invoke with:

./test.sh inputfile.txt

The problem with your solution is that you invoke grep for each line. In fact, grep parses each line, too. So, for a file with n lines, these lines are parsed n^2 times, and loading grep is quite an expensive call.

Use a one-line buffer, in this example called PrevLine, like this:

#!/bin/bash
PrevLine=''
isFirstLine=true
while IFS='' read -r Line; do
  echo "Current line: ${Line}"
  if $isFirstLine; then
    echo "This is the first line, so no previous."
  else
    echo "Previous line: ${PrevLine}"
  fi
  PrevLine="${Line}"
  isFirstLine=false
done <"$1"

In fact, assigning true to isFirstLine is a string assignment, and just mentioning $isFirstLine (in the if-condition) is the execution of the command of this string. Since true and false are bash-builtins, they can be used directly without significant speed impact, but with a high increase of readability.

The last line mentions $1 as input file name, so invoke with:

./test.sh inputfile.txt

The problem with your solution is that you invoke grep for each line. In fact, grep parses each line, too. So, for a file with n lines, these lines are parsed n^2 times, and loading grep is quite an expensive call.

Use a one-line buffer, in this example called PrevLine, like this:

#!/bin/bash
PrevLine=''
isFirstLine=true
while IFS='' read -r Line; do
  echo "Current line: ${Line}"
  if $isFirstLine; then
    echo "This is the first line, so no previous."
  else
    echo "Previous line: ${PrevLine}"
  fi
  PrevLine="${Line}"
  isFirstLine=false
done <"$1"

In fact, assigning true to isFirstLine is a string assignment, and just mentioning $isFirstLine (in the if-condition) is the execution of the command of this string. Since true and false are bash-builtins, they can be used directly without significant speed impact, but with a high increase of readability.

If you want to swap the lines, just move the line echo "Current line: ${Line}" below the if-block:

  fi
  echo "Current line: ${Line}"
  PrevLine="${Line}"

The last line mentions $1 as input file name, so invoke with:

./test.sh inputfile.txt
edited body
Source Link
rexkogitans
  • 1.4k
  • 11
  • 17

The problem with your solution is that you invoke grep for each line. In fact, grep parses each line, too. So, for a file with n lines, these lines are parsed n^2 times, and loading grep is quite an expensive call.

Use a one-line buffer, in this example called PrevLine, like this:

#!/bin/bash
PrevLine=''
isFirstLine=true
while IFS='' read -r Line; do
  echo "Current line: ${Line}"
  if $isFirstLine; then
    echo "This is the first line, so no previous."
  else
    echo "Previous line: ${PrevLine}"
  fi
  PrevLine="${Line}"
  isFirstLine=false
done <"$1"

In fact, assigning true to isFirstLine is a string assignment, and just mentioning $isFirstLine (in the if-condition) is the execution of the command of this string. Since true and false andare bash-builtins, they can be used directly without significant speed impact, but with a high increase of readability.

The last line mentions $1 as input file name, so invoke with:

./test.sh inputfile.txt

The problem with your solution is that you invoke grep for each line. In fact, grep parses each line, too. So, for a file with n lines, these lines are parsed n^2 times, and loading grep is quite an expensive call.

Use a one-line buffer, in this example called PrevLine, like this:

#!/bin/bash
PrevLine=''
isFirstLine=true
while IFS='' read -r Line; do
  echo "Current line: ${Line}"
  if $isFirstLine; then
    echo "This is the first line, so no previous."
  else
    echo "Previous line: ${PrevLine}"
  fi
  PrevLine="${Line}"
  isFirstLine=false
done <"$1"

In fact, assigning true to isFirstLine is a string assignment, and just mentioning $isFirstLine (in the if-condition) is the execution of the command of this string. Since true and false and bash-builtins, they can be used directly without significant speed impact, but with a high increase of readability.

The last line mentions $1 as input file name, so invoke with:

./test.sh inputfile.txt

The problem with your solution is that you invoke grep for each line. In fact, grep parses each line, too. So, for a file with n lines, these lines are parsed n^2 times, and loading grep is quite an expensive call.

Use a one-line buffer, in this example called PrevLine, like this:

#!/bin/bash
PrevLine=''
isFirstLine=true
while IFS='' read -r Line; do
  echo "Current line: ${Line}"
  if $isFirstLine; then
    echo "This is the first line, so no previous."
  else
    echo "Previous line: ${PrevLine}"
  fi
  PrevLine="${Line}"
  isFirstLine=false
done <"$1"

In fact, assigning true to isFirstLine is a string assignment, and just mentioning $isFirstLine (in the if-condition) is the execution of the command of this string. Since true and false are bash-builtins, they can be used directly without significant speed impact, but with a high increase of readability.

The last line mentions $1 as input file name, so invoke with:

./test.sh inputfile.txt
Source Link
rexkogitans
  • 1.4k
  • 11
  • 17
Loading