LinuxQuestions.org
Latest LQ Deal: Linux Power User Bundle
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 10-21-2016, 11:43 AM   #1
SafetyMark
LQ Newbie
 
Registered: Oct 2016
Posts: 7

Rep: Reputation: Disabled
Reading file with "cat" and creating array


Hi,

I'm beginner at bash so I decided to ask you about my problem.

I was trying to sort some numbers which are stored in text. I want to read file using cat and than store it to array so I can sort those numbers by using bubble sort (I know there is sort already implemented in bash, but I prefer bubble sort).

I was trying something like this
Code:
input=$1
myText=`cat $1`
myArray=${#myText[*]}
#I was trying also
#myArray=( $(cat "$1") )
When I try to echo $myText its working, but when using bubble sort it is not working.

Thanks for your help.
 
Old 10-21-2016, 12:10 PM   #2
HMW
Member
 
Registered: Aug 2013
Location: Sweden
Distribution: Debian, Arch, Xubuntu, Manjaro
Posts: 754
Blog Entries: 3

Rep: Reputation: 348Reputation: 348Reputation: 348Reputation: 348
$1 is the first argument passed to a program in bash, I don't really understand the usage here.

If you want to read from a file into an array, you can do something like this:
Code:
HMW@debian-HP:~/Slask$ echo "foo bar baz" >> myFile
HMW@debian-HP:~/Slask$ myarr=($(cat myFile))
HMW@debian-HP:~/Slask$ echo ${myarr[@]} 
foo bar baz
 
Old 10-21-2016, 12:39 PM   #3
SafetyMark
LQ Newbie
 
Registered: Oct 2016
Posts: 7

Original Poster
Rep: Reputation: Disabled
Yes in $1 is stored "argument" which I pass to program. For example
Code:
bash myscript.sh myfile.txt
. I'm using it inside if so I can check if user passed file to script or not.

Text file is already saved on Desktop because I'm trying project everytime on the same file (to check if it is working).
 
Old 10-21-2016, 01:20 PM   #4
ondoho
Senior Member
 
Registered: Dec 2013
Posts: 4,620

Rep: Reputation: 984Reputation: 984Reputation: 984Reputation: 984Reputation: 984Reputation: 984Reputation: 984Reputation: 984
what output do you get for
Code:
echo "$MyArray"
???
 
Old 10-21-2016, 01:38 PM   #5
SafetyMark
LQ Newbie
 
Registered: Oct 2016
Posts: 7

Original Poster
Rep: Reputation: Disabled
If I write
Code:
echo "$myText" 
I got
5 1 2 3 4
but If I write
Code:
echo "$myArray"
I got
1
 
Old 10-21-2016, 01:46 PM   #6
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,424

Rep: Reputation: 2823Reputation: 2823Reputation: 2823Reputation: 2823Reputation: 2823Reputation: 2823Reputation: 2823Reputation: 2823Reputation: 2823Reputation: 2823Reputation: 2823
Without () around your data you will never create an array in bash, unless you individually assign each value to a specific index.

So let us look at what you have done:

input=$1 :- here you assign the value of the first parameter to a variable. Great idea except then you never use the variable

myText=`cat $1` :- here you assign the contents of the file passed as first parameter to a variable. Note I said contents, hence the variable contains the entire file, newlines and all.

myArray=${#myText[*]} :- here you assign the length of an "array" to a variable. I quoted "array" as in fact it is only a string but bash will let you refer to it in the same way, however, here it will
return the value 1 as by definition there is only 1 index value (this being the zero'th value) which has all of your data stored in it.


If you want more proof that you have not created an array, try the following:
Code:
echo ${#myText[0]}
You will find the output to not be at all what you expected.

As shown by HMW, your commented out version, #myArray=( $(cat "$1") ), was actually the correct way to create an array.
My own preference would be (although the exact same outcome):
Code:
input="$1"

my_array=($(< "$input"))
 
1 members found this post helpful.
Old 10-21-2016, 01:49 PM   #7
szboardstretcher
Senior Member
 
Registered: Aug 2006
Location: Detroit, MI
Distribution: GNU/Linux systemd
Posts: 4,112

Rep: Reputation: 1528Reputation: 1528Reputation: 1528Reputation: 1528Reputation: 1528Reputation: 1528Reputation: 1528Reputation: 1528Reputation: 1528Reputation: 1528Reputation: 1528
Quote:
I want to read file using cat and than store it to array so I can sort those numbers
This is as small as i could make it (using redirection instead of cat):

Code:
shuf -i 1-999 -n 20 > numbers
mapfile -t unsorted < numbers
declare -p unsorted > /dev/null 2>&1
IFS=$'\n' sorted=($(sort -n <<< "${unsorted[*]}"))
printf "%s\n" "${sorted[@]}"

Last edited by szboardstretcher; 10-21-2016 at 01:50 PM. Reason: typo
 
1 members found this post helpful.
Old 10-21-2016, 02:16 PM   #8
SafetyMark
LQ Newbie
 
Registered: Oct 2016
Posts: 7

Original Poster
Rep: Reputation: Disabled
So I guess is easier to use other ways to solve this problem than use "cat"?

I'm still curious how would the cat version look like.
 
Old 10-21-2016, 02:25 PM   #9
szboardstretcher
Senior Member
 
Registered: Aug 2006
Location: Detroit, MI
Distribution: GNU/Linux systemd
Posts: 4,112

Rep: Reputation: 1528Reputation: 1528Reputation: 1528Reputation: 1528Reputation: 1528Reputation: 1528Reputation: 1528Reputation: 1528Reputation: 1528Reputation: 1528Reputation: 1528
using cat?

Code:
shuf -i 1-999 -n 20 > numbers
cat numbers | mapfile -t unsorted
declare -p unsorted > /dev/null 2>&1
IFS=$'\n' sorted=($(sort -n <<< "${unsorted[*]}"))
printf "%s\n" "${sorted[@]}"
Not sure if this is what you meant.
 
Old 11-11-2016, 10:49 AM   #10
linux.bash255
LQ Newbie
 
Registered: Nov 2016
Posts: 2

Rep: Reputation: Disabled
Try:
Quote:
IFS=$'\n'
arr=(`cat $1`)
echo ${arr[@]}
Few examples: masteringunixshell.net/qa36/bash-how-to-add-to-array.html
 
  


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
Diff File contents while reading using "cat" and "more" maitree@123 AIX 3 07-31-2014 09:59 AM
how can I "cat" or "grep" a file to ignore lines starting with "#" ??? callagga Linux - Newbie 7 08-16-2013 07:58 AM
script using "/usr/bin/cat error" produces "cannot open" in cron Dcrusoe Programming 6 07-22-2009 04:30 PM
bash redirection "$ cat << EOF > file" (how does this work) ninmonkeys Linux - General 1 11-09-2004 04:37 PM
An existing file in the PWD isn't displayed with "cat".. linuxharsha Linux - General 2 02-17-2004 08:11 AM


All times are GMT -5. The time now is 06:58 AM.

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