Latest LQ Deal: Complete CCNA, CCNP & Red Hat Certification Training Bundle
Go Back > Forums > Non-*NIX Forums > Programming
User Name
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.


  Search this Thread
Old 01-11-2018, 01:01 AM   #1
LQ Newbie
Registered: Jan 2018
Posts: 1

Rep: Reputation: Disabled


I am having data in below format in a file

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

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.

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

Rep: Reputation: 2628Reputation: 2628Reputation: 2628Reputation: 2628Reputation: 2628Reputation: 2628Reputation: 2628Reputation: 2628Reputation: 2628Reputation: 2628Reputation: 2628
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, 02:32 AM   #3
Registered: Jul 2004
Location: Chennai, India
Distribution: UBUNTU 5.10 since Jul-18,2006 on Intel 820 DC
Posts: 878

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.


Last edited by AnanthaP; 01-11-2018 at 02:38 AM.
Old 01-11-2018, 07:19 AM   #4
Registered: Dec 2011
Location: Simplicity
Posts: 934

Rep: Reputation: 403Reputation: 403Reputation: 403Reputation: 403Reputation: 403
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.
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 07:26 AM.
Old 01-17-2018, 12:47 AM   #5
Registered: Mar 2006
Location: Ekaterinburg, Russia
Distribution: Debian, Ubuntu
Posts: 709

Rep: Reputation: 427Reputation: 427Reputation: 427Reputation: 427Reputation: 427

How about
 $ 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:
$ 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, 12:15 PM   #6
LQ Guru
Registered: Mar 2004
Distribution: Slackware
Posts: 6,230

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

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


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

All times are GMT -5. The time now is 04:23 PM.

Main Menu
Write for LQ is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration