Tag: line

Iterate a Text File in Bash

This is how you iterate thru a text file outputting a line at a time in Bash shell scripting.

Add Line Numbers

Use this command to add line numbers to a text file… sed = filename.txt | sed ‘N;s/\n/\t/’ > filename_number.txt

Delete Leading Whitespace

Delete leading whitespace (spaces/tabs/etc) from beginning of each line ruby -pe ‘gsub(/^\s+/, “”)’ < file.txt

Sort by Line Length

Sort file by line length. Original Article… cat /etc/passwd | awk ‘{print length, 0}’ | sort -n | awk ‘{1=””;…

Delete all \n chars in a file…

Delete all \n chars in a file… tr -d “\n”

Text type convert

Convert a text file format from MSDOS to UNIX dos2unix input_file output_file

Find Duplicate Lines

Sort contents of two files by viewing only duplicate line sort file1 file2 | uniq -d

Sort and Unique

Sort contents of a file and remove repeated lines sort file.txt | uniq

Remove Even Lines

This command removes all the even lines from the file given as the argument cat example.txt|awk ‘NR%2==1’

View some lines in a file

View from 1th to 5th line sed -n ‘1,5p;5q’ example.txt