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 05-16-2016, 11:25 AM   #1
Andy_Crowd
Member
 
Registered: Jan 2014
Posts: 62

Rep: Reputation: Disabled
Question How to make Array into string in AWK and split


Hello!

I need all part of array get as an string, split and show new with separated parts.

Code:
grep ^Categories -h -R /usr/share/applications/ | awk -F= '//{
A[C++]=$2
}END{
B=A[@];
split(B,D,";");
for( I in D)print D[I];
}'

Thanks for any help!
 
Old 05-16-2016, 01:13 PM   #2
Turbocapitalist
LQ Guru
 
Registered: Apr 2005
Distribution: Linux Mint, Devuan, OpenBSD
Posts: 7,309
Blog Entries: 3

Rep: Reputation: 3721Reputation: 3721Reputation: 3721Reputation: 3721Reputation: 3721Reputation: 3721Reputation: 3721Reputation: 3721Reputation: 3721Reputation: 3721Reputation: 3721
It might help if you post some sample data. Also, grep is redundant if you have awk. Try / /

Code:
awk '/somepattern/ { someactions }' somefile
 
1 members found this post helpful.
Old 05-16-2016, 01:21 PM   #3
dunne
Member
 
Registered: May 2014
Distribution: OpenBSD
Posts: 67

Rep: Reputation: 36
Quote:
Originally Posted by Turbocapitalist View Post
It might help if you post some sample data.

Yes, I was just about to say that. I would add: post what you expect to see as output. Then the problem should be trivial.
 
Old 05-16-2016, 01:23 PM   #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
To expand on the previous post by Turbocapitalist ...
Help us to help you. Provide a sample input file (10-15 lines will do). Construct a sample output file which corresponds to your sample input and post both samples here. With "Before and After" examples we can better understand your needs and also judge if our proposed solution fills those needs.

Daniel B. Martin
 
Old 05-16-2016, 10:19 PM   #5
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,007

Rep: Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192
ditto to all of the above
 
Old 05-17-2016, 10:06 AM   #6
Andy_Crowd
Member
 
Registered: Jan 2014
Posts: 62

Original Poster
Rep: Reputation: Disabled
Here is a simply explanation as I want make it work in awk:

Code:
A=A+B[I]

split(A,C,";")

***loop***
print C[II]
**********
 
Old 05-17-2016, 10:16 AM   #7
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
Quote:
Originally Posted by Andy_Crowd View Post
Here is a simply explanation as I want make it work in awk:

Code:
A=A+B[I]

split(A,C,";")

***loop***
print C[II]
**********
Help us to help you. Provide a sample input file (10-15 lines will do). Construct a sample output file which corresponds to your sample input and post both samples here. With "Before and After" examples we can better understand your needs and also judge if our proposed solution fills those needs.

Daniel B. Martin
 
Old 05-17-2016, 10:37 AM   #8
Andy_Crowd
Member
 
Registered: Jan 2014
Posts: 62

Original Poster
Rep: Reputation: Disabled
This is working script. If it will be possible to have all in the same variable then I could skip using "sort -u"

Code:
grep ^Categories -h -R "/usr/share/applications/" "$HOME/.local/share/applications/" | \
awk -F= '//{A[$2]=1
}END{
for(E in A){gsub(";","\n",E);printf E}
}' | sort -u
 
Old 05-17-2016, 12:10 PM   #9
Andy_Crowd
Member
 
Registered: Jan 2014
Posts: 62

Original Poster
Rep: Reputation: Disabled
Evrika !!!!!

I solved it! Pretty simply but I couldn't give correct question to get right answer. Sometimes it just coming as an apple on head under tree.

Code:
grep ^Categories -h -R "/usr/share/applications/" | \ 
awk -F= '//{
 A[$2]=1
}END{
 for(E in A)ZZ=ZZ E;
 split(ZZ,YY,";");
 asort(YY);
for(I in YY)
 HH[YY[I]];
for(QQ in HH)
 print QQ}'
Thank you for your support and your time!!!
 
Old 05-17-2016, 09:16 PM   #10
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,007

Rep: Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192
Guess we will have to take your word for it seeing as you did not want any of our help ... not really sure why you asked the question in the first place.
 
Old 05-17-2016, 11:28 PM   #11
Andy_Crowd
Member
 
Registered: Jan 2014
Posts: 62

Original Poster
Rep: Reputation: Disabled
I didn't know how to do it. I tested A=A+Array[I], A+=Array[I], A[Array[I]] and even split(Array,B,";") but got bad results and I wanted to split into a new array after I collected all in a same variable. I got really lost, I am still a newbie to use "awk". After I asking questions I still continue to look around if it is not a really complex, but sometimes even simple things can be complex. I found out that in awk it works a little different, to add a variable to another for splitting is: "A=A Array[I]", I isn't necessary to use + as I thought, impossible to split in whole array, it is just enough to use space between variables.
 
Old 05-18-2016, 12:06 AM   #12
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,007

Rep: Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192
Well I am glad you found a solution, it may not be the most expedient and you have not given anyone here a chance to show you an alternative because you refuse to answer any of the questions asked
of you. No one is knocking you learning, but your question was made moot by the fact you would not tell anyone the information requested ... which is why I thought it was kind of a waste
to ask the question in the first place if you didn't really want help.

Hopefully your future endeavours will come to you as well Good luck
 
1 members found this post helpful.
  


Reply

Tags
array, awk, split



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
how can I split a file into many files using a string in awk or sed atjurhs Linux - Newbie 15 06-11-2013 11:45 PM
[SOLVED] split a string into array in bash return wrong size xeon123 Linux - Newbie 2 03-21-2011 07:16 AM
[SOLVED] split a string into array in bash xeon123 Linux - Newbie 8 03-17-2011 11:16 AM
[SOLVED] how to split string into an array/list of lines in C++? rohedin Programming 11 06-06-2010 10:54 AM
awk: Using split to divide string to array. How do I find out the number of elements? vxc69 Programming 9 02-09-2008 12:49 PM

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

All times are GMT -5. The time now is 09:11 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