LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Home Forums Tutorials Articles Register
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 04-13-2013, 08:03 AM   #1
pampers
LQ Newbie
 
Registered: Sep 2012
Location: Chile
Distribution: Slackware Current - RedHat Enterprise
Posts: 16

Rep: Reputation: Disabled
Smile Help counting elements from an array in Bash Script.


Hello.

I am very newbie in bash coding, I have a file with the elements:

apple
pear
juice
orange

I need to read this file in bash script, and print those values and the number of elements line by line attached to those values, for example what I need to print as output is:

1 apple
2 pear
3 juice
4 orange

I know I can read the elements from a file with the command "wc -l" but i don,t want the total of elements printed on screen, I want line per line printed. In Perl I know I can do this with foreach:

Code:
foreach my $value (0..$#fruits) {
        print $value + 1 . " $fruits[$value]\n";
    }
will probably print on screen:

1 apple
2 pear
3 juice
4 orange

But since I don,t have foreach in bash, is kind of hard for me to translate from perl to bash. What I have done in bash for reading the file is:

Code:
file=($(cat myfruits.txt))

for a in ${file[*]}
do

 echo $a

done
Any help will be very appreciated.
 
Old 04-13-2013, 08:34 AM   #2
pampers
LQ Newbie
 
Registered: Sep 2012
Location: Chile
Distribution: Slackware Current - RedHat Enterprise
Posts: 16

Original Poster
Rep: Reputation: Disabled
Is ok.

I did this task with Perl instead of Bash, and now is working.

So this thread can be closed.

Thanks for any consideration to help me
 
Old 04-13-2013, 09:09 AM   #3
konsolebox
Senior Member
 
Registered: Oct 2005
Distribution: Gentoo, Slackware, LFS
Posts: 2,248
Blog Entries: 8

Rep: Reputation: 235Reputation: 235Reputation: 235
Please be patient for answers. You could try this.
Code:
ARRAY=(apple pear juice orange)
for I in "${!ARRAY[@]}"; do
    echo "$(( I + 1 )) ${ARRAY[I]}"
done
---- Edit ----

Sorry I actually took a look at your code first and based the solution from it without reading much..

It could be done this way:
Code:
LN=0
FILE=/path/to/file.ext
while read LINE; do
    echo "$(( ++LN )) ${LINE}"
done < "$FILE"
And this is yet another way if your version of bash is 4.0 or newer.
Code:
FILE=/path/to/file.ext
readarray -t -O 1 LINES < "$FILE"
for I in "${!LINES[@]}"; do
    echo "$I ${LINES[I]}"
done

Last edited by konsolebox; 04-13-2013 at 09:22 AM.
 
Old 04-15-2013, 08:08 AM   #4
danielbmartin
Senior Member
 
Registered: Apr 2010
Location: Apex, NC, USA
Distribution: Mint 17.3
Posts: 1,881

Rep: Reputation: 660Reputation: 660Reputation: 660Reputation: 660Reputation: 660Reputation: 660
With this InFile ...
Code:
apple
pear
juice
orange
... this code ...
Code:
nl -s" " -w1 $InFile >$OutFile
... produced this OutFile ...
Code:
1 apple
2 pear
3 juice
4 orange
Daniel B. Martin
 
  


Reply



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] Read correctly bash array with spaces in its elements cgcamal Programming 4 10-05-2011 10:46 PM
Bash - Searching strings for array elements... Phier Programming 18 05-09-2010 04:37 AM
Bash Script Array index value Kedelfor Programming 10 04-29-2009 04:37 AM
Array counting in C rubadub Programming 10 08-14-2007 04:35 PM
Renaming array elements in bash bryan.out.there Programming 2 05-31-2006 11:44 PM

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

All times are GMT -5. The time now is 11:01 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
Open Source Consulting | Domain Registration