LinuxQuestions.org
Share your knowledge at the LQ Wiki.
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 05-13-2014, 07:56 AM   #1
sandeep_hello
Member
 
Registered: Feb 2008
Posts: 62

Rep: Reputation: 1
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.
 
Old 05-13-2014, 08:37 AM   #2
szboardstretcher
Senior Member
 
Registered: Aug 2006
Location: Detroit, MI
Distribution: GNU/Linux systemd
Posts: 4,278

Rep: Reputation: 1694Reputation: 1694Reputation: 1694Reputation: 1694Reputation: 1694Reputation: 1694Reputation: 1694Reputation: 1694Reputation: 1694Reputation: 1694Reputation: 1694
Parse it with what? What have you tried?
 
Old 05-13-2014, 08:38 AM   #3
TenTenths
Senior Member
 
Registered: Aug 2011
Location: Dublin
Distribution: Centos 5 / 6 / 7 / 8
Posts: 3,572

Rep: Reputation: 1612Reputation: 1612Reputation: 1612Reputation: 1612Reputation: 1612Reputation: 1612Reputation: 1612Reputation: 1612Reputation: 1612Reputation: 1612Reputation: 1612
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.
 
Old 05-13-2014, 08:55 AM   #4
sandeep_hello
Member
 
Registered: Feb 2008
Posts: 62

Original Poster
Rep: Reputation: 1
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.
 
Old 05-13-2014, 08:58 AM   #5
TenTenths
Senior Member
 
Registered: Aug 2011
Location: Dublin
Distribution: Centos 5 / 6 / 7 / 8
Posts: 3,572

Rep: Reputation: 1612Reputation: 1612Reputation: 1612Reputation: 1612Reputation: 1612Reputation: 1612Reputation: 1612Reputation: 1612Reputation: 1612Reputation: 1612Reputation: 1612
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.
 
Old 05-13-2014, 09:02 AM   #6
sandeep_hello
Member
 
Registered: Feb 2008
Posts: 62

Original Poster
Rep: Reputation: 1
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.
 
Old 05-13-2014, 09:42 AM   #7
TB0ne
LQ Guru
 
Registered: Jul 2003
Location: Birmingham, Alabama
Distribution: SuSE, RedHat, Slack,CentOS
Posts: 27,790

Rep: Reputation: 8201Reputation: 8201Reputation: 8201Reputation: 8201Reputation: 8201Reputation: 8201Reputation: 8201Reputation: 8201Reputation: 8201Reputation: 8201Reputation: 8201
Quote:
Originally Posted by sandeep_hello View Post
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.
 
Old 05-13-2014, 10:48 AM   #8
sandeep_hello
Member
 
Registered: Feb 2008
Posts: 62

Original Poster
Rep: Reputation: 1
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?
 
Old 05-13-2014, 10:58 AM   #9
szboardstretcher
Senior Member
 
Registered: Aug 2006
Location: Detroit, MI
Distribution: GNU/Linux systemd
Posts: 4,278

Rep: Reputation: 1694Reputation: 1694Reputation: 1694Reputation: 1694Reputation: 1694Reputation: 1694Reputation: 1694Reputation: 1694Reputation: 1694Reputation: 1694Reputation: 1694
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
 
Old 05-13-2014, 11:07 AM   #10
TB0ne
LQ Guru
 
Registered: Jul 2003
Location: Birmingham, Alabama
Distribution: SuSE, RedHat, Slack,CentOS
Posts: 27,790

Rep: Reputation: 8201Reputation: 8201Reputation: 8201Reputation: 8201Reputation: 8201Reputation: 8201Reputation: 8201Reputation: 8201Reputation: 8201Reputation: 8201Reputation: 8201
Quote:
Originally Posted by sandeep_hello View Post
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.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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 in Parsing data saurabhmehan Linux - Newbie 9 02-15-2011 09:50 AM
Parsing Email Data Geneset Linux - General 1 07-23-2007 05:31 PM
parsing data - better way of doing that kshkid Programming 10 01-08-2007 06:05 AM
Parsing text file line by line armandino101 Linux - Newbie 3 12-14-2006 02:43 PM
Parsing a string line-by-line in PHP enigma_0Z Programming 3 04-21-2006 08:07 AM

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

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