Advertisement
metalx1000

BASH read line before and after current line

Jan 21st, 2022
1,913
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 0.36 KB | None | 0 0
  1. #!/bin/bash
  2.  
  3. echo "john
  4. jeff
  5. jerry
  6. tom
  7. tim
  8. sam
  9. andrew
  10. kris" > /tmp/names
  11.  
  12.  
  13. let x=0
  14.  
  15. cat /tmp/names |while read line
  16. do
  17.   if [[ $x == 0 ]]
  18.   then
  19.     name1="$line"
  20.   elif [[ $x == 1 ]]
  21.   then
  22.     name2="$line"
  23.   else
  24.     echo "$name1 is before $name2 and $line is after $name2"
  25.     name1="$name2"
  26.     name2="$line"
  27.   fi
  28.   let x++
  29.  
  30. done
  31.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement