Quantcast
Channel: Write a program that read from a file and print the line with line number - Unix & Linux Stack Exchange
Viewing all articles
Browse latest Browse all 4

Answer by heemayl for Write a program that read from a file and print the line with line number

$
0
0

Using a short while construct:

% i=1; while IFS= read -r line; do printf '%s. %s\n' "$i" "$line"; ((i++)); done <file.txt

1. This is the first line
2. This is the second line
3. This is the third line
4. 
5. This was a blank line

Expanded:

#!/usr/bin/env bash
i=1
while IFS= read -r line; do 
    printf '%s. %s\n' "$i" "$line"
    ((i++))
done <file.txt

Viewing all articles
Browse latest Browse all 4

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>