Linux - NewbieThis 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
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
Hi,
I'm trying to write a bash script that takes a file and passes each line from the file into an array with elements separated by column.
For example:
Sample file "file1.txt":
1 name1 a first
2 name2 b second
3 name3 c third
and have arrays such as:
line1 = ( "1" "name1" "a" "first" )
line2 = ( "2" "name2" "b" "second" )
line1 = ( "3" "name3" "c" "third" )
I'd also like to know if it's possible to pass each column of values into its own array, such as:
column1 = ( "1" "2" "3" )
column2 = ( "name1" "name2" "name3" )
column3 = ( "a" "b" "c" )
column4 = ( "first" "second" "third" )
Yes, all these things are possible. Post what you've written/tried so far, and tell us where you're stuck, and we'll be happy to help. However, this sounds very much like a homework question, and as a rule, you're not going to get someone here to do it for you.
If you show you've put some effort into things, and tell us where you're stuck, you're much more likely to get a favorable response.
This isn't a homework question, I'm doing this for work and this is for the sake of assigning values from the file to arrays so I can more easily reference them later when I need to compare values with each other. Bash and perl are available to me, but I'm slightly more familiar with bash, so I'm starting there. I'm just trying to get this done as quickly as possible so we can move on to the next task. My main problem is not knowing the syntax, so I guess I could list my problem as a series of questions.
1. Assuming the file name is the first argument when running the program, $1 will reference the file name, I know that much. I'm assuming I need a loop of some kind to pass each line of the file into arrays so something along the lines of:
Code:
counter = 1
while [ $counter -le $(wc -l $1) ]; do
read line$counter < #line $counter of $1
let counter = counter + 1
end
How do I write that line "line $counter of $1", where it references line number "counter" from the file?
2. I have no idea how to pass the file values as columns, is there a specific command for this?
Thanks!
This isn't a homework question, I'm doing this for work and this is for the sake of assigning values from the file to arrays so I can more easily reference them later when I need to compare values with each other. Bash and perl are available to me, but I'm slightly more familiar with bash, so I'm starting there. I'm just trying to get this done as quickly as possible so we can move on to the next task. My main problem is not knowing the syntax, so I guess I could list my problem as a series of questions.
Ok...again, as with your other thread, post what you've written/tried. And if it's not homework...it sure sounds like it. If you need it done quickly and its for work, why not let someone else at your company who knows scripting handle it?
Quote:
1. Assuming the file name is the first argument when running the program, $1 will reference the file name, I know that much. I'm assuming I need a loop of some kind to pass each line of the file into arrays so something along the lines of:
Code:
counter = 1
while [ $counter -le $(wc -l $1) ]; do
read line$counter < #line $counter of $1
let counter = counter + 1
end
How do I write that line "line $counter of $1", where it references line number "counter" from the file?
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.