LinuxQuestions.org
Review your favorite Linux distribution.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices

Reply
 
Search this Thread
Old 02-21-2013, 11:34 AM   #1
nwalsh88
LQ Newbie
 
Registered: Dec 2012
Posts: 8

Rep: Reputation: Disabled
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
 
Old 02-21-2013, 11:48 AM   #2
shivaa
Senior Member
 
Registered: Jul 2012
Location: Grenoble, Fr.
Distribution: Sun Solaris, RHEL, Ubuntu, Debian 6.0
Posts: 1,797
Blog Entries: 4

Rep: Reputation: 285Reputation: 285Reputation: 285
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
 
Old 02-21-2013, 11:50 AM   #3
TB0ne
Guru
 
Registered: Jul 2003
Location: Birmingham, Alabama
Distribution: SuSE, RedHat, Slack,CentOS
Posts: 14,132

Rep: Reputation: 2456Reputation: 2456Reputation: 2456Reputation: 2456Reputation: 2456Reputation: 2456Reputation: 2456Reputation: 2456Reputation: 2456Reputation: 2456Reputation: 2456
Quote:
Originally Posted by nwalsh88 View Post
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.
 
2 members found this post helpful.
Old 02-21-2013, 12:00 PM   #4
nwalsh88
LQ Newbie
 
Registered: Dec 2012
Posts: 8

Original Poster
Rep: Reputation: Disabled
Thanks Guys, sorry for not making it clearer.

I meant to do it from the command line.

Thanks for the help
 
Old 02-21-2013, 03:27 PM   #5
shivaa
Senior Member
 
Registered: Jul 2012
Location: Grenoble, Fr.
Distribution: Sun Solaris, RHEL, Ubuntu, Debian 6.0
Posts: 1,797
Blog Entries: 4

Rep: Reputation: 285Reputation: 285Reputation: 285
Quote:
Originally Posted by nwalsh88 View Post
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.
 
Old 02-21-2013, 04:05 PM   #6
danielbmartin
Senior Member
 
Registered: Apr 2010
Location: Apex, NC, USA
Distribution: Ubuntu
Posts: 1,057

Rep: Reputation: 284Reputation: 284Reputation: 284
Quote:
Originally Posted by nwalsh88 View Post
... 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
 
Old 02-22-2013, 12:34 AM   #7
chrism01
Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Centos 6.5, Centos 5.10
Posts: 16,225

Rep: Reputation: 2021Reputation: 2021Reputation: 2021Reputation: 2021Reputation: 2021Reputation: 2021Reputation: 2021Reputation: 2021Reputation: 2021Reputation: 2021Reputation: 2021
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"
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] word and line count nishushah93 Linux - Newbie 1 10-06-2012 11:01 PM
Count the line frater Programming 20 02-06-2011 08:51 PM
Script to count # of chars per line (if line meets certain criteria) and get avg #? kmkocot Linux - Newbie 3 09-13-2009 11:05 AM
write line count to a variable? tatoosh Linux - Newbie 9 07-30-2009 03:44 PM
word count in a line pantera Programming 4 08-25-2004 01:14 PM


All times are GMT -5. The time now is 04:24 AM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration