Convert fields in one column to single line using common field separator in the column
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
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.
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.
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.
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.
#!/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)
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.