Concatenate lines in a single paragraph

Tags
shell scripts

Login to Reply

 
Thread Tools Search this Thread
# 1  
Old 05-16-2009
Concatenate lines in a single paragraph

Hi all,

May you help me with this?

After extract only some lines that I need from a file, I would like to concatenate the remaining lines in a single line (paragraph).

Source file pattern:

Code:
Event Type: Warning
Event Source: Sorting
Event Category: None
Event ID: 1188
Date:  9/10/2008
Time:  9:46:50 PM
User:  Adminis\P1
Computer: MyComp-KKL
Description:
User profile MyComp-KKL(S-1-5-21-3230802392-3390281410-1560515013-1307).

After extracting text of my interest I get:

Code:
Warning
9/10/2008
9:46:50 PM
User profile MyComp-KKL(S-1-5-21-3230802392-3390281410-1560515013-1307).

But Im cannot follow with the next step, I would like the next result.
(Join lines like a continuous line)

Code:
In date 9/10/2008, 9:46:50 PM. This is a warning from User profile MyComp-KKL(S-1-5-21-3230802392-3390281410-1560515013-1307).

Any help would be very appreciated.

Best regards
# 2  
Old 05-16-2009
hi, if you mean line break then try this:

cat youfile.txt | paste -s -d',' | sed s'/,/, /g'

otherwise if you have allready createed a script then please post it!
# 3  
Old 05-16-2009
if you have Python
Code:
d={}
for line in open("file"):
    line=line.strip().split(":")
    d[line[0].strip()]=line[-1].strip()
for k in d.keys():    
    if "User profile" in k: 
        user=k
        break    
print "In date %s, %s. This is a warning from %s." %(d['Date'],d['Time'],user)

output
Code:
# ./test.py
In date 9/10/2008, 50 PM. This is a warning from User profile MyComp-KKL(S-1-5-21-3230802392-3390281410-1560515013-1307)..

# 4  
Old 05-16-2009
Alternatively, if you have perl then:

Code:
$
$ cat input.txt
Event Type: Warning
Event Source: Sorting
Event Category: None
Event ID: 1188
Date:  9/10/2008
Time:  9:46:50 PM
User:  Adminis\P1
Computer: MyComp-KKL
Description:
User profile MyComp-KKL(S-1-5-21-3230802392-3390281410-1560515013-1307).
$
$ perl -ne '{if (/^(.*):[ ]+(.*)$/) {$x{$1}=$2}
>            elsif (/^User profile/){$p=$_}
> } END {print "In date $x{Date}, $x{Time}. This is a warning from $p"}' input.txt
In date 9/10/2008, 9:46:50 PM. This is a warning from User profile MyComp-KKL(S-1-5-21-3230802392-3390281410-1560515013-1307).
$
$

tyler_durden
# 5  
Old 05-17-2009
Hey guys,

Really thanks to all for your help, for your different solutions to my question; this part of code would help me a lot to continue in my script.

Smilie
Login to Reply

|
Thread Tools Search this Thread
Search this Thread:
Advanced Search

Similar Threads More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Join Lines every paragraph in a file.txt lxdorney Shell Programming and Scripting 7 10-09-2016 05:00 PM
Concatenate three lines into one FredAtArrow UNIX for Dummies Questions & Answers 7 08-14-2014 05:03 PM
How to concatenate lines with specific pattern? nixtime Shell Programming and Scripting 8 06-25-2013 06:39 PM
Concatenate lines in file shell script systemoper UNIX for Advanced & Expert Users 3 01-14-2012 06:40 PM
Need to concatenate spuriously separated lines lemele Shell Programming and Scripting 16 12-28-2011 02:21 AM
Concatenate two lines in a fIle uday.sena.m Programming 3 09-22-2011 12:50 AM
Multiple lines in a single column to be merged as a single line for a record Bhuvaneswari Shell Programming and Scripting 1 08-11-2011 04:16 AM
How to search for multiple lines and put them into one paragraph? liuzhencc Shell Programming and Scripting 11 11-28-2010 02:01 PM
Concatenate lines between lines starting with a specific pattern s052866 Shell Programming and Scripting 9 10-24-2010 01:09 PM
Concatenate multiple lines based. indrajit_u Shell Programming and Scripting 8 06-30-2010 06:50 PM
concatenate lines in pairs kerl Shell Programming and Scripting 5 06-17-2010 12:26 PM
Concatenate two lines in one Orbix Shell Programming and Scripting 5 04-29-2010 05:40 PM
concatenate lines using shell scripting dtdt Shell Programming and Scripting 13 09-07-2009 01:14 AM
How to concatenate consecutive lines shivi707 UNIX Desktop Questions & Answers 1 01-12-2009 09:08 AM
Convert a paragraph to single line rimss Shell Programming and Scripting 6 06-07-2006 04:17 PM
All times are GMT -4. The time now is 11:18 AM.

Unix & Linux Forums Content Copyright 1993-2018. All Rights Reserved.