LinuxQuestions.org
Latest LQ Deal: Complete CCNA, CCNP & Red Hat Certification Training Bundle
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 01-11-2018, 12:01 AM   #1
Abhayman
LQ Newbie
 
Registered: Jan 2018
Posts: 1

Rep: Reputation: Disabled
format-data-using-sed-or-awk


Hi,

I am having data in below format in a file

Code:
Section : A1234,
Name : ABCBDEDF,
Age : No,   
Name : Reporting,
Age : No
Section : XYSZA,
Name : Work,
Age : YES
I am trying to achieve data in below format :--

Code:
Section : A1234,Name : ABCBDEDF,Age : No
Section : A1234,Name : Reporting,Age : No
Section : XYSZA,Name : Work,Age : YES
I tried few sed and awk statements to but I am able to merge only all rows together.

Code:
awk '{key=$0; getline; print key "" $0;}' test.txt
Any help is appreciated.
 
Old 01-11-2018, 12:18 AM   #2
syg00
LQ Veteran
 
Registered: Aug 2003
Location: Australia
Distribution: Lots ...
Posts: 16,848

Rep: Reputation: 2512Reputation: 2512Reputation: 2512Reputation: 2512Reputation: 2512Reputation: 2512Reputation: 2512Reputation: 2512Reputation: 2512Reputation: 2512Reputation: 2512
You are merely merging each two lines. Simplest might be to save "Name" as well as key (presumably "Section") records, then print when an "Age" record is found.
 
Old 01-11-2018, 01:32 AM   #3
AnanthaP
Member
 
Registered: Jul 2004
Location: Chennai, India
Distribution: UBUNTU 5.10 since Jul-18,2006 on Intel 820 DC
Posts: 875

Rep: Reputation: 208Reputation: 208Reputation: 208
Look carefully at the data. One section can contain many names.
You need 2 variables, KEY and TEXT
When "Section" pattern is encountered, assign $0 to KEY.
When "Name" pattern is encountered, concatenate, KEY (contains SECTION) and this $o (NAME) to TEXT.
When "Age" pattern is encountered, append $0 to the variable TEXT and print ti out and initialize TEXT.

Finally (on END), if TEXT variable contains a value, then print it out.

OK

Last edited by AnanthaP; 01-11-2018 at 01:38 AM.
 
Old 01-11-2018, 06:19 AM   #4
MadeInGermany
Member
 
Registered: Dec 2011
Location: Simplicity
Posts: 832

Rep: Reputation: 364Reputation: 364Reputation: 364Reputation: 364
If you hard-code "Name" and "Age", then it is like this.
There is still two techniques, one with getline, and one with variables.
I always prefer the variables.

Here is an advanced solution that uses many variables.
It only depends on "Section" but does not need to know what follows.
If "Section" is met it sets state variable first and jumps to the next input cycle.
If first is set it must be on the next line; then it learns the first following key ffkey, "Name" in this case.
Because it does not know the ending key, it has to delay the printing of the final EOL ("\n" or ORS in awk). This is done when the next "Section" is met (I put in the first following printing), or in the END section.
Code:
awk '
  $1=="Section" { key=$0; first=1; next }
  first==1 { ffkey=$1; first=0 }
  { printf "%s", ($1==ffkey ? (ors key $0) : $0); ors=ORS }
  END { printf ors }
' test.txt
The last trick is to not print the ORS in the first section. I print variable ors instead that is empty first, and set later to ORS.

Last edited by MadeInGermany; 01-11-2018 at 06:26 AM.
 
Old 01-16-2018, 11:47 PM   #5
firstfire
Member
 
Registered: Mar 2006
Location: Ekaterinburg, Russia
Distribution: Debian, Ubuntu
Posts: 709

Rep: Reputation: 427Reputation: 427Reputation: 427Reputation: 427Reputation: 427
Hi.

How about
Code:
 $ awk -F' : ' '{f[$1] = $2;} $1=="Age" { print "Section :", f["Section"] "Name : " f["Name"], "Age : " f["Age"] }' /tmp/data.txt
Section : A1234,Name : ABCBDEDF, Age : No,
Section : A1234,Name : Reporting, Age : No
Section : XYSZA,Name : Work, Age : YES
This may be more suitable if there are more than 3 fields:
Code:
$ awk -F' : ' '{f[$1] = $2;} $1=="Age" {split("Section,Name,Age", keys, ","); for(i in keys) {k=keys[i]; printf("%s : %s", k, f[k])}; printf("\n") }' /tmp/data.txt
Section : A1234,Name : ABCBDEDF,Age : No,
Section : A1234,Name : Reporting,Age : No
Section : XYSZA,Name : Work,Age : YES
 
Old 01-17-2018, 11:15 AM   #6
keefaz
LQ Guru
 
Registered: Mar 2004
Distribution: Slackware
Posts: 6,226

Rep: Reputation: 709Reputation: 709Reputation: 709Reputation: 709Reputation: 709Reputation: 709Reputation: 709
Using perl
Code:
perl -lne '/^S/ and $s=$_ or $v.=$_; /^A/ and print($s,$v), $v=""' test.txt
Edit, converting to awk
Code:
awk '/^S/{s=$0; next} {v=v$0} /^A/{print s v; v=""}' test.txt

Last edited by keefaz; 01-17-2018 at 03:26 PM.
 
  


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
Format horizontal data vertically using grep, sed, awk, etc nooobeee Linux - Newbie 10 01-15-2018 04:32 PM
[SOLVED] Formatting data with Sed and Awk itachi8009 Programming 10 08-06-2013 10:32 AM
How to get the result in such log format using linux tools (awk,sed..) ? sylye Linux - Software 15 05-21-2012 08:29 AM
sed/awk/grep for multiple line data Coiby Programming 4 09-20-2011 06:16 AM
sed/awk/grep for multiple line data hotrodmacman Programming 8 10-18-2007 11:06 AM

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

All times are GMT -5. The time now is 08:17 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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration