Bash: Working with files

From Joe's Boredom

Remove duplicate lines in file

<source lang="bash">

  1. Will sort file out-put

sort -u file.in > file.out

  1. Works same as above, sort file out-put

sort file.in | uniq > file.out

  1. Will remove duplicates and keep file in same order

awk ' !x[$0]++' file.in > file.out</source>


Remove blank lines in file

<source lang="bash"> sed '/^$/d' input.txt > output.txt

  1. or

grep -v '^$' input.txt > output.txt

  1. Loop dir, remove blank lines from all files

files="/home/me/data/*.txt" for i in $files do

 sed '/^$/d' $i > $i.out
 mv  $i.out $i

done </source>


Read file line by line

<source lang="bash"> while read line do echo $line; done < "file.lst" </source>


Count lines in file

<source lang="bash">countTotal=$(awk 'END { print NR }' file.name); echo "$countTotal"; </source>


Check if file exists

<source lang="bash"> if [ -s "$file_name" ]; then

  echo 'yes';

fi </source>


Check if dir exists

<source lang="bash"> if [ -d "$folder_path" ]; then

  echo 'yes';

fi </source>