LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   Line count as 0000nn (https://www.linuxquestions.org/questions/linux-newbie-8/line-count-as-0000nn-4175451145/)

nwalsh88 02-21-2013 12:34 PM

Line count as 0000nn
 
This is going to sound like a stupid question but is it possible to carry out a line count of a file and display the count as 0000nn with 6 characters altogether.

eg.
if line count is 1 -> 000001
if line count is 10 -> 000010
if line count is 100 -> 000100
etc.

I need it in this format to place it in another file

Thanks :)

shivaa 02-21-2013 12:48 PM

Try a script like this:
Code:

#!/bin/bash
echo -n "Enter filename: "; read filename
lc=$(cat $filename | wc -l)
if [ $lc -eq 1 ]; then
echo "000001"
elif [ $lc -eq 10 ]; then
echo "000010"
elif [ $lc -eq 100 ]; then
echo "000100"
else
echo $lc
fi


TB0ne 02-21-2013 12:50 PM

Quote:

Originally Posted by nwalsh88 (Post 4896840)
This is going to sound like a stupid question but is it possible to carry out a line count of a file and display the count as 0000nn with 6 characters altogether.

eg.
if line count is 1 -> 000001
if line count is 10 -> 000010
if line count is 100 -> 000100
etc.

I need it in this format to place it in another file

No idea what you're asking for here...you don't give any context/details. If you mean that you want to write a program to do this, you certainly can...it's your program, so output whatever you'd like. If so, you don't say what language you want to use, so we can't give any details. If you mean you want to do it from a command line using Linux commands, then you need to look into the wc and awk commands. For example:
Code:

wc -l filename | awk {'print $1'} | awk '{printf "%06s\n", $0}'
Can probably be cleaner/neater, but this is an example to work from.

nwalsh88 02-21-2013 01:00 PM

Thanks Guys, sorry for not making it clearer.

I meant to do it from the command line.

Thanks for the help

shivaa 02-21-2013 04:27 PM

Quote:

Originally Posted by nwalsh88 (Post 4896857)
I meant to do it from the command line.

Both will work on command line only :)

Please Mark the thread as solved (option is under Thread Tools on top menu), if you think it has so.

danielbmartin 02-21-2013 05:05 PM

Quote:

Originally Posted by nwalsh88 (Post 4896840)
... is it possible to carry out a line count of a file and display the count as 0000nn with 6 characters altogether.

Code:

nl -nrz $InFile > $OutFile
Daniel B. Martin

chrism01 02-22-2013 01:34 AM

A couple of alternatives
Code:

wc -l file1.txt |  awk '{printf "%06s\n", $1}'
022770

Shortened from TB0ne's example

Also, printf is a bash cmd as well, so in a bash script
Code:

s=2270
printf "%06d\n" "$s"



All times are GMT -5. The time now is 09:15 PM.