LinuxQuestions.org
Review your favorite Linux distribution.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 08-26-2019, 03:06 AM   #1
elfoozo
Member
 
Registered: Feb 2004
Location: Washington, USA
Distribution: Debian
Posts: 263

Rep: Reputation: 32
Question bash scripting, getting a counter to align with output?


I'm trying to write something that retrieves total lines of "myfile.txt" then prefixes each iteration of "myfile.txt" with a number as it counts down processing each line of "myfile.txt". I don't know what this programming concept is called so my challenge is figuring out how to search for what this is even called. So I tried nested for loops but it isn't what I'm looking for.

I've tried in my script:
Code:
#!/bin/bash
totallines="$(cat myfile.txt | wc -l)"
for ((eachline="${totallines}"; eachline>=1; eachline--))
   do echo -n "${eachline}: " 
   for line in "$(cat myfile.txt)"    
      do echo "${line}" 
   done 
done
Contents of myfile.txt:
Code:
Line one
Line two
Line three
Line four
Line five
What I'm producing: (wrong)
Code:
5: Line one
Line two
Line three
Line four
Line five
4: Line one
Line two
Line three
Line four
Line five
3: Line one
Line two
Line three
Line four
Line five
2: Line one
Line two
Line three
Line four
Line five
1: Line one
Line two
Line three
Line four
Line five
What I'm trying to produce:
Code:
5: Line one
4: Line two
3: Line three
2: Line four
1: Line one
Is there an official name for this kind of coding logic?
This is not a schoolwork assignment. Any pointers are greatly appreciated.
 
Old 08-26-2019, 04:55 AM   #2
GazL
LQ Guru
 
Registered: May 2008
Posts: 5,156
Blog Entries: 18

Rep: Reputation: 2849Reputation: 2849Reputation: 2849Reputation: 2849Reputation: 2849Reputation: 2849Reputation: 2849Reputation: 2849Reputation: 2849Reputation: 2849Reputation: 2849
Quote:
Is there an official name for this kind of coding logic?
"wrong" springs to mind.


If you're looking for a bash solution,
Code:
count=$( wc -l < inputfile.txt )
while read line
do
 printf "%4d:%s\n" $(( count-- )) "$line"
done < inputfile.txt
If you don't mind using GNUisms, you could also do something like
tac inputfile.txt | cat -n | tac

The 'nl' utility from coreutils is also an option.
 
3 members found this post helpful.
Old 08-26-2019, 05:48 AM   #3
Firerat
Senior Member
 
Registered: Oct 2008
Distribution: Debian sid
Posts: 1,992

Rep: Reputation: 549Reputation: 549Reputation: 549Reputation: 549Reputation: 549Reputation: 549
you can also do

Code:
printf "%4d:%s\r" $(( count-- )) "$line"
which would 're-write' the same line
you would want to pad the end if the length varries
 
1 members found this post helpful.
Old 08-27-2019, 12:28 AM   #4
elfoozo
Member
 
Registered: Feb 2004
Location: Washington, USA
Distribution: Debian
Posts: 263

Original Poster
Rep: Reputation: 32
Thank you for the replies. Hopefully my original ask wasn't too vague. I wasn't trying to be cagey and of course I realize what I was doing was wrong. I just don't have the experience to know better (yet). I wasn't looking for someone to write the solution but instead was trying to understand the mechanics of how one could count down as a script works through a list, and give me the observer a visual indicator how many iterations were left to go from that inputfile.txt list.

Based on your code example, if I were not wanting to just display (printf) contents of inputfile.txt but do some kind of processing on what is within inputfile.txt... Say as example I wanted to do something like dig a list of hostnames provided from inputfile.txt or do some simplistic housekeeping based off of what's in inputfile.txt? Is it likely to still be a while loop for that leg of a processing.
 
Old 08-27-2019, 04:31 AM   #5
GazL
LQ Guru
 
Registered: May 2008
Posts: 5,156
Blog Entries: 18

Rep: Reputation: 2849Reputation: 2849Reputation: 2849Reputation: 2849Reputation: 2849Reputation: 2849Reputation: 2849Reputation: 2849Reputation: 2849Reputation: 2849Reputation: 2849
There's almost always more than one way to do something in shell-script. If you really wanted to, you could put the read in the body of a for loop, but when sequentially processing an input file line by line, while read; do ... seems like the more natural fit.

If the input file is relatively small, one might even want to do a Perlesque slurp into an array (see bash mapfile command).

Short answer: pick what feels right.
 
1 members found this post helpful.
Old 08-27-2019, 11:24 AM   #6
Sefyir
Member
 
Registered: Mar 2015
Distribution: Linux Mint
Posts: 602

Rep: Reputation: 300Reputation: 300Reputation: 300Reputation: 300
This will read strange to anyone using bash. a while loop will go over every line
Code:
for ((eachline="${totallines}"; eachline>=1; eachline--))
You prepend the line count (as expected)
The next line you put out will have this value. Since you put out multiple lines after one iteration of this, this gives you your undesired behavior
Code:
   do echo -n "${eachline}: "
You iterate through every line in the whole file, echoing out the line. Probably not what you want
Code:
   for line in "$(cat myfile.txt)"    
      do echo "${line}"
Quote:
Based on your code example, if I were not wanting to just display (printf) contents of inputfile.txt but do some kind of processing on what is within inputfile.txt... Say as example I wanted to do something like dig a list of hostnames provided from inputfile.txt or do some simplistic housekeeping based off of what's in inputfile.txt? Is it likely to still be a while loop for that leg of a processing.
GazL gave you a good answer for this but to simplify it slightly,
Code:
count=$( wc -l < inputfile.txt )
while read line
do
 other_processing_commands
 dig +short "$line"
 echo "$count": "$line"
 (( count-- ))
done < inputfile.txt
28: Line
27: Line
26: Line
25: Line
24: Line
...
 
1 members found this post helpful.
  


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
LXer: Shell Scripting Part I: Getting started with bash scripting LXer Syndicated Linux News 0 04-29-2015 08:03 AM
PHP - POST variables named with counter: $_POST[${"variable$counter"} winairmvs Programming 1 08-06-2010 04:56 PM
Shell Scripting: using <printf> to align text need HELP!!! naomi Programming 2 01-06-2005 01:06 PM
Counter-strike,[color=orange]H L F - L I F E [/color],Counter-strike: Source. Dennisgoop Linux - Games 2 08-10-2004 03:03 AM
Red Hat 8, HP842C, Align the print heads? itsjustme Red Hat 0 10-21-2003 03:03 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 06:53 PM.

Main Menu
Advertisement
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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration