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 |
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.
Are you new to LinuxQuestions.org? Visit the following links:
Site Howto |
Site FAQ |
Sitemap |
Register Now
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.
|
 |
05-13-2014, 07:56 AM
|
#1
|
Member
Registered: Feb 2008
Posts: 62
Rep:
|
Data parsing for each line
I am trying to parse below line in different order but it is not working.
a="1" b="2" c="3" d="4" e="5" f="6" g="7" h="8"
output should be
5,1,3,4,2,7,6,8
Please suggest me the best way to do this.
|
|
|
05-13-2014, 08:37 AM
|
#2
|
Senior Member
Registered: Aug 2006
Location: Detroit, MI
Distribution: GNU/Linux systemd
Posts: 4,278
|
Parse it with what? What have you tried?
|
|
|
05-13-2014, 08:38 AM
|
#3
|
Senior Member
Registered: Aug 2011
Location: Dublin
Distribution: Centos 5 / 6 / 7 / 8
Posts: 3,572
|
Code:
echo "5,1,3,4,2,7,6,8"
and if you want a serious answer, try telling us the logic behind the re-ordering of the output.
|
|
|
05-13-2014, 08:55 AM
|
#4
|
Member
Registered: Feb 2008
Posts: 62
Original Poster
Rep:
|
I want to parse field by taking values
like
first value of e
second value of a
third value of c
so on..
each value should be separated with "," comma.
|
|
|
05-13-2014, 08:58 AM
|
#5
|
Senior Member
Registered: Aug 2011
Location: Dublin
Distribution: Centos 5 / 6 / 7 / 8
Posts: 3,572
|
And how are we supposed to know what this sequence is?
Or is this another input that you're going to have?
Also post what you've written already.
|
|
|
05-13-2014, 09:02 AM
|
#6
|
Member
Registered: Feb 2008
Posts: 62
Original Poster
Rep:
|
sequence is
e,a,c,d,b,g,h
each character will have its value
I was trying to do with awk and sed but not getting success.
|
|
|
05-13-2014, 09:42 AM
|
#7
|
LQ Guru
Registered: Jul 2003
Location: Birmingham, Alabama
Distribution: SuSE, RedHat, Slack,CentOS
Posts: 27,790
|
Quote:
Originally Posted by sandeep_hello
sequence is
e,a,c,d,b,g,h
each character will have its value I was trying to do with awk and sed but not getting success.
|
Ok...so as we asked before, post what you have written/tried on your own. You were asked in post #2 and #5. We will be glad to help, but we're not going to write your homework scripts for you.
|
|
|
05-13-2014, 10:48 AM
|
#8
|
Member
Registered: Feb 2008
Posts: 62
Original Poster
Rep:
|
ok fine
I am doing parsing in this way.
# cat test.txt
a="1" b="2" c="3" d="4" e="5" f="6" g="7" h="8"
# cat test.txt |awk -F " " '{print $5 "," $1 "," $3 "," $4 "," $2 "," $6 "," $7 "," $8 }' |awk -F "\"" '{print $2 "," $4 "," $6 "," $8 "," $10 "," $12 "," $14 "," $16}'
Output is
5,1,3,4,2,6,7,8
Is it right way or can make more easy?
|
|
|
05-13-2014, 10:58 AM
|
#9
|
Senior Member
Registered: Aug 2006
Location: Detroit, MI
Distribution: GNU/Linux systemd
Posts: 4,278
|
To keep it more 'simple', first thing that came to mind was:
Code:
vars=$(cat test.txt | sed 's/\ /;/g'); eval $vars; echo "$e,$a,$c,$d,$b,$f,$g,$h"
output:
5,1,3,4,2,6,7,8
|
|
|
05-13-2014, 11:07 AM
|
#10
|
LQ Guru
Registered: Jul 2003
Location: Birmingham, Alabama
Distribution: SuSE, RedHat, Slack,CentOS
Posts: 27,790
|
Quote:
Originally Posted by sandeep_hello
ok fine I am doing parsing in this way.
# cat test.txt
a="1" b="2" c="3" d="4" e="5" f="6" g="7" h="8"
# cat test.txt |awk -F " " '{print $5 "," $1 "," $3 "," $4 "," $2 "," $6 "," $7 "," $8 }' |awk -F "\"" '{print $2 "," $4 "," $6 "," $8 "," $10 "," $12 "," $14 "," $16}'
Output is
5,1,3,4,2,6,7,8
Is it right way or can make more easy?
|
There are MANY ways to do what you're after, depending on the language and your requirements. You don't really say what you're after, though, and what you're doing is VERY simplistic.
If your goal is to have a script that actually reads the values of a, b, c, etc., and print them out, then this isn't the way to do it; szboardstretcher has given the solution for that.
|
|
|
All times are GMT -5. The time now is 03:08 PM.
|
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.
|
Latest Threads
LQ News
|
|