LinuxQuestions.org
Review your favorite Linux distribution.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Linux User Groups (LUG)
User Name
Password
Linux User Groups (LUG) This forum allows Linux User Groups (LUG) to gain exposure. It also allows people looking for a LUG in their area to find one.

Notices


Reply
  Search this Thread
Old 08-08-2017, 12:37 PM   #1
say_hi_ravi
Member
 
Registered: Jan 2008
Posts: 75

Rep: Reputation: 15
Convert fields in one column to single line using common field separator in the column


I have a file

Code:
---
_id:abc
config:qa
---
_id:lmn
config:dev
---
_id:xyz
config:xyz
---
I want following o/p

Code:
--- _id:abc config:qa
--- _id:lmn config:dev
--- _id:xyz config:xyz
---
I tried using it with awk with no luck

Code:
awk '{BEGIN OFS = "---"; ORS =" " } {print}'
 
Old 08-08-2017, 01:04 PM   #2
MensaWater
LQ Guru
 
Registered: May 2005
Location: Atlanta Georgia USA
Distribution: Redhat (RHEL), CentOS, Fedora, CoreOS, Debian, FreeBSD, HP-UX, Solaris, SCO
Posts: 7,831
Blog Entries: 15

Rep: Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669
Code:
awk '/---/ {printf $0" "}
/_id/ {printf $0" "}
/config/ {printf $0 "\n"}' <filename>
echo ""
You can have have awk look for multiple lines based on separate search patterns and output on each based on that line. Using printf rather than print it will output the combined 3 lines on one line.

So in the above we search for the line that contains literal "---" and print that followed by a space, we then look for the line that contains the literal "_id" and print that followed by a space and finally look for the line that contains literal "config" and print that followed by a newline.
At end we do the echo to put in a blank line.

Notice on the third line you specify the file you're using where I put <filename>.

The above of course assumes that the lines are all as you show in your sample. You'd have to play with regex for other differences or similarities.

Last edited by MensaWater; 08-08-2017 at 03:20 PM.
 
Old 08-08-2017, 05:24 PM   #3
Sefyir
Member
 
Registered: Mar 2015
Distribution: Linux Mint
Posts: 634

Rep: Reputation: 316Reputation: 316Reputation: 316Reputation: 316
Python3 Program

This is a good use of positive lookup in regular expressions.
This starts with ---, then continues until the next character matches --- and repeats. Interestingly, This does not read line by line, but as a single stream. Because it iterates the results as it reads it, it'll begin printing out results immediately and uses minimal memory.

Code:
$ ./converter.py -d'---' -c'\n' myfile # Default action
--- _id:abc config:qa 
--- _id:lmn config:dev 
--- _id:xyz config:xyz
Code:
#!/usr/bin/env python3

import re
import sys
import argparse
import codecs

def unescaped_str(arg_str):
    return codecs.decode(str(arg_str), 'unicode_escape')

parser = argparse.ArgumentParser(description='Convert delimiter to singlelines')
parser.add_argument('-d', '--row-delimiter',
        type=unescaped_str,
        default='---')
parser.add_argument('-c', '--column-delimiter',
        type=unescaped_str,
        action='store',
        default='\n')
args, other_args = parser.parse_known_args()

dash_column_regex = re.compile('{d}.*?(?={d})'.format(d=args.row_delimiter), re.DOTALL)
data = (other_args if other_args else sys.stdin)

def column_to_rows(regex_object, file_object):
    file_object = file_object.read()
    return (column.group().replace(args.column_delimiter, ' ')
            for column in regex_object.finditer(file_object))

if data is sys.stdin:
    results = [column_to_rows(dash_column_regex, data)]
else:
    results = list()
    for _file in data:
        with open(_file) as f:
            results.append(column_to_rows(dash_column_regex, f))

for _file in results:
    for line in _file:
        print(line)

Last edited by Sefyir; 08-09-2017 at 11:32 AM.
 
Old 08-09-2017, 06:56 AM   #4
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 21,830

Rep: Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308
Code:
awk 'BEGIN{RS="---\n"; FS="\n"; ORS="\n"; OFS=" "} { print "---",$1,$2,$3 }'
 
  


Reply



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
Split single line into multiple lines with 3 column each udiubu Programming 7 11-26-2017 09:41 AM
[SOLVED] grep single column only in line keif Programming 4 12-04-2013 01:45 PM
[SOLVED] AWK fill column from previuos line column akeka Programming 4 01-30-2013 07:16 PM
How to display 2 different column field values as one column value in mysql VijayaRaghavanLakshman Linux - General 2 04-16-2012 09:56 AM
awk multiple column into single column ilukacevic Programming 49 07-19-2010 07:23 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Linux User Groups (LUG)

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