LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Software
User Name
Password
Linux - Software This forum is for Software issues.
Having a problem installing a new program? Want to know which application is best for the job? Post your question in this forum.

Notices


Reply
  Search this Thread
Old 03-20-2020, 10:22 AM   #1
flukeyLinux
Member
 
Registered: Sep 2012
Location: Ireland
Distribution: Ubuntu redhat, wsl
Posts: 48

Rep: Reputation: Disabled
Creating an array from a txt file in bash


Hi all,
I'm looking for some help to build an array from a text file that looks like this:

Project1
ansible
Project2
Project3
Project4
repo1
Project5
repo1
repo2
repo3
repo4
repo5
repo6
repo7
Project6
repo1
repo2
repo3
repo4
repo5
repo6
repo7
repo8
repo9
repo10
Project7
repo1
repo2
repo3
repo4
Project8
ops
Project9
delete_this_repo
Project10
model_service
c2r_service
puppetcode


So it looks like this:

Project1=(ansible)
Project2=()
Project3=()
Project4=(repo1)
Project5=(repo1 repo2 repo3 repo4 repo5 repo6 repo7)
... and so on.

Anything that does not have a "repo" can just be deleted. That could be done before or after. I tried looking into "declare" but I am not sure how to use it. Maybe there is something with awk I could use?

This data was in jq but I removed all of the double quotes and commas to get the data I needed to look like it does above. But maybe there is an easier way to do this using jq?

Any suggestions welcome!


Thanks in advance,
Fluke

Last edited by flukeyLinux; 03-20-2020 at 10:30 AM.
 
Old 03-20-2020, 10:55 AM   #2
boughtonp
Senior Member
 
Registered: Feb 2007
Location: UK
Distribution: Debian
Posts: 3,627

Rep: Reputation: 2556Reputation: 2556Reputation: 2556Reputation: 2556Reputation: 2556Reputation: 2556Reputation: 2556Reputation: 2556Reputation: 2556Reputation: 2556Reputation: 2556
Quote:
Originally Posted by flukeyLinux View Post
This data was in jq but I removed all of the double quotes and commas to get the data I needed to look like it does above. But maybe there is an easier way to do this using jq?
That's very likely - I've not yet used jq, but it looks like you could use recurse to filter the data, then map to reformat each item.
https://stedolan.github.io/jq/manual...),recurse_down
https://stedolan.github.io/jq/manual...,map_values(x)


Last edited by boughtonp; 03-20-2020 at 10:56 AM.
 
Old 03-20-2020, 11:02 AM   #3
Turbocapitalist
LQ Guru
 
Registered: Apr 2005
Distribution: Linux Mint, Devuan, OpenBSD
Posts: 7,350
Blog Entries: 3

Rep: Reputation: 3766Reputation: 3766Reputation: 3766Reputation: 3766Reputation: 3766Reputation: 3766Reputation: 3766Reputation: 3766Reputation: 3766Reputation: 3766Reputation: 3766
What does the above look like in the original JSON encoded format?
 
Old 03-20-2020, 12:57 PM   #4
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 purely dynamically being done. so you're going to have to come up with a way to keep track of each array created dynamically and keep track of there names in another array perhaps.

just using that "file" you posted.

Code:
#!/bin/bash

arr=''

while read a ; 
do
#echo $a
if [[ "${a,,}" =~ "project" ]] ;
then
#keep which project it is
	arr=$a
	#to add in the print out of each
	#array created, but not kept.
	#because it is being over written by
	#the next projectX in line within 
	#the 'data' file.
	echo "$arr"
	
	#if projects hits then the next 
	#step is to get the repo's if any.
elif [[ "${a,,}" =~ "repo" ]] ;
then
	
	echo "$a"
	arr+=("$a")
	
	
fi

done < data
output
Code:
$ ./sortme
Project1
Project2
Project3
Project4
repo1
Project5
repo1
repo2
repo3
repo4
repo5
repo6
repo7
Project6
repo1
repo2
repo3
repo4
repo5
repo6
repo7
repo8
repo9
repo10
Project7
repo1
repo2
repo3
repo4
Project8
Project9
delete_this_repo
Project10
Project10
it is just an idea, work in progress. but I'd think it is still all being done dynamically regardless of what tools you use.

Last edited by BW-userx; 03-20-2020 at 01:06 PM.
 
Old 03-23-2020, 07:18 AM   #5
flukeyLinux
Member
 
Registered: Sep 2012
Location: Ireland
Distribution: Ubuntu redhat, wsl
Posts: 48

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by BW-userx View Post
this is purely dynamically being done. so you're going to have to come up with a way to keep track of each array created dynamically and keep track of there names in another array perhaps.

just using that "file" you posted.

Code:
#!/bin/bash

arr=''

while read a ; 
do
#echo $a
if [[ "${a,,}" =~ "project" ]] ;
then
#keep which project it is
	arr=$a
	#to add in the print out of each
	#array created, but not kept.
	#because it is being over written by
	#the next projectX in line within 
	#the 'data' file.
	echo "$arr"
	
	#if projects hits then the next 
	#step is to get the repo's if any.
elif [[ "${a,,}" =~ "repo" ]] ;
then
	
	echo "$a"
	arr+=("$a")
	
	
fi

done < data
output
Code:
$ ./sortme
Project1
Project2
Project3
Project4
repo1
Project5
repo1
repo2
repo3
repo4
repo5
repo6
repo7
Project6
repo1
repo2
repo3
repo4
repo5
repo6
repo7
repo8
repo9
repo10
Project7
repo1
repo2
repo3
repo4
Project8
Project9
delete_this_repo
Project10
Project10
it is just an idea, work in progress. but I'd think it is still all being done dynamically regardless of what tools you use.
This would do actually but most repo's don't have the word repo in it. I am going to try going the JQ route but if I don't get what I need, I will be coming back to this.

thanks very much.
 
Old 03-23-2020, 07:27 AM   #6
flukeyLinux
Member
 
Registered: Sep 2012
Location: Ireland
Distribution: Ubuntu redhat, wsl
Posts: 48

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by Turbocapitalist View Post
What does the above look like in the original JSON encoded format?
The projects look like this:

"key": "Project1",
"key": "Project2",
"key": "Project3",
"key": "Project4",
"key": "Project5",
"key": "Project6",
"key": "Project7",
"key": "Project8",
"key": "Project9",
"key": "Project10",

I strip out the 1t column with awk and remove any white space with tr -d.

I then use a for loop to get the repo's under every project with an API call

# Get repos for every project
for i in `cat projectkeys.txt`;
do echo $i;
curl -s -u $LOGIN "https://$SERVER/rest/api/1.0/projects/$i/repos?limit=10" -k | jq . | grep "slug";
done > projectkeys2.txt

I couldn't do this with quotes and commas so that is why I removed them.

regards,
fluke
 
Old 03-23-2020, 07:35 AM   #7
Turbocapitalist
LQ Guru
 
Registered: Apr 2005
Distribution: Linux Mint, Devuan, OpenBSD
Posts: 7,350
Blog Entries: 3

Rep: Reputation: 3766Reputation: 3766Reputation: 3766Reputation: 3766Reputation: 3766Reputation: 3766Reputation: 3766Reputation: 3766Reputation: 3766Reputation: 3766Reputation: 3766
Quote:
Originally Posted by flukeyLinux View Post
The projects look like this:
That's not JSON though. Is that really how the source data looks? Please provide an exact sample of the format, sanitized if necessary but retaining the format nonetheless.
 
Old 03-23-2020, 07:43 AM   #8
flukeyLinux
Member
 
Registered: Sep 2012
Location: Ireland
Distribution: Ubuntu redhat, wsl
Posts: 48

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by Turbocapitalist View Post
That's not JSON though. Is that really how the source data looks? Please provide an exact sample of the format, sanitized if necessary but retaining the format nonetheless.
true, I use grep to get what I need. Here is the full output of the API call

Code:
{
  "size": 1,
  "limit": 1,
  "isLastPage": false,
  "values": [
    {
      "key": "Project4",
      "id": 15785,
      "name": "repo1",
      "public": false,
      "type": "NORMAL",
      "links": {
        "self": [
          {
            "href": "https://10.196.20.79:9443/projects/Project4"
          }
        ]
      }
    }
  ],
  "start": 0,
  "nextPageStart": 1
}

Last edited by flukeyLinux; 03-23-2020 at 07:45 AM.
 
  


Reply

Tags
array, bash, for loop, jq



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 txt file into an array and make a second txt file zimbot Linux - General 12 09-05-2015 01:39 PM
BASH-Adding array element: Naming issue using array[${#array[*]}]=5 calvarado777 Programming 8 07-26-2013 09:48 PM
Copy the contents of a txt file to other txt files (with similar names) by cp command Aquarius_Girl Linux - Newbie 7 07-03-2010 12:54 AM
cat onelinefile.txt >> newfile.txt; cat twofile.txt >> newfile.txt keep newline? tmcguinness Programming 4 02-12-2009 06:38 AM
How can read from file.txt C++ where can save this file(file.txt) to start reading sam_22 Programming 1 01-11-2007 05:11 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Software

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