john pfeiffer
  • Home
  • Categories
  • Tags
  • Archives

File read from a file count lines

!/bin/bash

if [ $# -ne 1 ] then echo "$0 filename" exit 0 fi

file=$1 while IFS= read -r line do echo "$line" done <"$file"


!/bin/bash

if [ $# -ne 1 ] then echo "$0 filename" exit 0 fi

file=$1 count=0 while IFS= read -r line do ((count++)) done <"$file" echo $count

((count+=2)) , ((count-=5))


!/bin/bash

file="/etc/passwd" while IFS=: read -r f1 f2 f3 f4 f5 f6 f7 do echo "Username: $f1, Shell: $f7, Home Dir: $f6" done <"$file"


!/bin/sh

FOR LOOP VERSION

for line in $(cat test.txt) do echo $line"nospace" "space between words" done


!/bin/bash

Read file lines into an array

declare -a ARRAY

Link filedescriptor 10 with stdin

exec 10<&0

stdin replaced with a file supplied as a first argument

exec < $1 let count=0

while read LINE; do

ARRAY[$count]=$LINE
((count++))

done

echo Number of elements: ${#ARRAY[@]} echo ${ARRAY[@]}

restore stdin from filedescriptor 10

and close filedescriptor 10

exec 0<&10 10<&-


  • « css table
  • Ldaps slapd ubuntu 11 »

Published

Dec 16, 2011

Category

linux

~146 words

Tags

  • a 23
  • count 6
  • file 92
  • from 24
  • lines 4
  • read 14
  • scripts 63