So I have a range, say 2-4. And I have three lines:
first
second
third
I need my output to be:
2 first
3 second
4 third
I'm trying this on BSD (Mac) awk/sed, which seems to be making it harder.
nl
is ideally suited:
nl -v2 -p -ba
will start counting from 2 (-v2
), ignoring page changes (-p
) and numbering all lines (-ba
).
POSIXLY:
awk '{printf("%s %s\n", FNR+1, $0)}' file
If you want to pass parameter:
awk -vn=2 '{printf "%s %s\n", n++, $0}' <file
If you want only the range is produced in case the file is longer than the range:
awk -v s=2 -v e=4 'BEGIN{for(n=s;n<=e;n++)print n}' | paste -d' ' - file
With pure bash script?
i=2; cat output.txt | while IFS= read -r line; do
echo "$i $line"
i=$((i+1))
done
N
and that's really trivial... there's a tool that was designed specifically ton
umberl
ines. – don_crissti May 25 '16 at 18:16