LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
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 10-19-2018, 05:54 PM   #1
cogiz
LQ Newbie
 
Registered: Oct 2018
Location: Halifax
Distribution: Peppermint 10
Posts: 15

Rep: Reputation: Disabled
printing bash array elements inside text file


I am using Linux Mint 18.3 Mate 64 bit

I have 4 Bash arrays as follows:

[CODE]name=(Bob Bill Steve Richard Janet)
hair=(brown grey grey brunette blonde)
eyes=(blue brown brown green hazel)
build=(average slim average big average)[CODE]

I need it printed out in the following format (each sentence has its own line but double spaced and only one space between each word):

${name[@]} has ${hair[@]} hair and ${eyes[@]} eyes and ${build[@]} build.

Output example needed:

Bob has brown hair and blue eyes and average build.

Bill has grey hair and brown eyes and slim build.

Steve has grey hair and brown eyes and average build.

Richard has brunette hair and green eyes and big build.

Janet has blonde hair and hazel eyes and average build.


I would like this to be executed in one command in Bash script if possible.

I have tried this :

Code:
paste <(printf "%s\n" "${name[@]} has") <(printf "%s\n" "${hair[@]} hair and" ) <(printf "%s\n" "${eyes[@]} eyes and") <(printf "%s\n" "${build[@]} build.") > profile.txt
awk {$1=$1}, OFS=" "  profile.txt
but can't seem to get it right.
Any and all suggestions are greatly appreciated.
Thank you
cogiz
 
Old 10-19-2018, 06:12 PM   #2
BW-userx
LQ Guru
 
Registered: Sep 2013
Location: Somewhere in my head.
Distribution: Slackware (15 current), Slack15, Ubuntu studio, MX Linux, FreeBSD 13.1, WIn10
Posts: 10,342

Rep: Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242
I need it printed out in the following format (each sentence has its own line but double spaced and only one space between each word):

echo, redirection and a loop can get you this,
Code:
$ cat MyFormatedFile
Bob has brown hair and blue eyes and average build.

Bill has grey hair and brown eyes and slim build.

Steve has grey hair and brown eyes and average build.

Richard has brunette hair and green eyes and big build.

Janet has blonde hair and hazel eyes and average build.
that is what you are looking for, yes?
hint: all of your array's are the same length, so use that for your loop count.

Last edited by BW-userx; 10-19-2018 at 06:40 PM.
 
1 members found this post helpful.
Old 10-19-2018, 06:26 PM   #3
astrogeek
Moderator
 
Registered: Oct 2008
Distribution: Slackware [64]-X.{0|1|2|37|-current} ::12<=X<=15, FreeBSD_12{.0|.1}
Posts: 6,263
Blog Entries: 24

Rep: Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194
Welcome to LQ!

That is a lot of printf where a simple loop and echo would do what you want!

Wrap this in a while or for loop using array size as the index limit and see how it works:

Code:
echo -e "${name[$indx]} has ${hair[$indx]} hair and ${eyes[$indx]} eyes and ${build[$indx]} build.\n"
A quick implementation produces this...

Code:
Bob has brown hair and blue eyes and average build.

Bill has grey hair and brown eyes and slim build.

Steve has grey hair and brown eyes and average build.

Richard has brunette hair and green eyes and big build.

Janet has blonde hair and hazel eyes and average build.
I'll leave the loop details as an exercise!

Last edited by astrogeek; 10-19-2018 at 06:28 PM.
 
1 members found this post helpful.
Old 10-19-2018, 06:28 PM   #4
syg00
LQ Veteran
 
Registered: Aug 2003
Location: Australia
Distribution: Lots ...
Posts: 21,126

Rep: Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120
Quote:
Originally Posted by cogiz View Post
I would like this to be executed in one command in Bash script if possible.
Why ? Rube Golberg advocate ?.
See suggestion above.
 
1 members found this post helpful.
Old 10-20-2018, 09:25 AM   #5
cogiz
LQ Newbie
 
Registered: Oct 2018
Location: Halifax
Distribution: Peppermint 10
Posts: 15

Original Poster
Rep: Reputation: Disabled
the following worked like a charm:

for (( indx=0 ; indx < ${#name[@]} ; indx++ )); do
echo
echo -e " "${name[$indx]}" has "${hair[$indx]}" hair and "${eyes[$indx]}" eyes and ${build[$indx]} build.\n"
done

Thank you very much for all suggestions
cogiz
 
2 members found this post helpful.
Old 10-20-2018, 09:33 AM   #6
BW-userx
LQ Guru
 
Registered: Sep 2013
Location: Somewhere in my head.
Distribution: Slackware (15 current), Slack15, Ubuntu studio, MX Linux, FreeBSD 13.1, WIn10
Posts: 10,342

Rep: Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242
this is what I came up with.

Code:
#!/bin/bash

name=(Bob Bill Steve Richard Janet)
hair=(brown grey grey brunette blonde)
eyes=(blue brown brown green hazel)
build=(average slim average big average)
file=MyFormatedFile 

for i in "${!name[@]}"; do

#sending it to a file

#echo "${name[$i]} has ${hair[$i]} hair and ${eyes[$i]} eyes and ${build[$i]} build." >> $file

#adding two spaces between lines.
#echo;echo >> $file

done
astrogeek used one '\n' for new line, whereas I used the echo;echo to get double spaced between each line, adding an extra '\n' with give you another space between the line as well.


so now you've got two ways to set up your for loop.


Q:
if yo can get code blocks for your one liner you tied, why are you having trouble using code blocks for all of your code?

Last edited by BW-userx; 10-20-2018 at 09:43 AM.
 
1 members found this post helpful.
Old 10-20-2018, 08:08 PM   #7
astrogeek
Moderator
 
Registered: Oct 2008
Distribution: Slackware [64]-X.{0|1|2|37|-current} ::12<=X<=15, FreeBSD_12{.0|.1}
Posts: 6,263
Blog Entries: 24

Rep: Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194
Quote:
Originally Posted by cogiz View Post
the following worked like a charm:

for (( indx=0 ; indx < ${#name[@]} ; indx++ )); do
echo
echo -e " "${name[$indx]}" has "${hair[$indx]}" hair and "${eyes[$indx]}" eyes and ${build[$indx]} build.\n"
done

Thank you very much for all suggestions
cogiz
You are welcome, glad that helped!

Thank you for the follow-through, and again, welcome to LQ!
 
1 members found this post helpful.
  


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] Help counting elements from an array in Bash Script. pampers Programming 3 04-15-2013 08:08 AM
How to assign column of a text file to an array with bash GianniC Programming 9 11-14-2012 12:45 PM
[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
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 08:35 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