Home Man
Search
Today's Posts
Register

BSD, Linux, and UNIX shell scripting — Post awk, bash, csh, ksh, perl, php, python, sed, sh, shell scripts, and other shell scripting languages questions here.

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 I´m 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.

Login to Reply

« Previous Thread | Next Thread »
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

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 04:00 PM
Concatenate three lines into one FredAtArrow UNIX for Dummies Questions & Answers 7 08-14-2014 04:03 PM
Concatenate two lines in a fIle uday.sena.m Programming 3 09-21-2011 11:50 PM
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 03:16 AM
How to search for multiple lines and put them into one paragraph? liuzhencc Shell Programming and Scripting 11 11-28-2010 01:01 PM
Concatenate lines between lines starting with a specific pattern s052866 Shell Programming and Scripting 9 10-24-2010 12:09 PM
concatenate lines in pairs kerl Shell Programming and Scripting 5 06-17-2010 11:26 AM
Concatenate two lines in one Orbix Shell Programming and Scripting 5 04-29-2010 04:40 PM
How to concatenate consecutive lines shivi707 UNIX Desktop Questions & Answers 1 01-12-2009 08:08 AM
Convert a paragraph to single line rimss Shell Programming and Scripting 6 06-07-2006 03:17 PM


All times are GMT -4. The time now is 04:32 AM.

Unix & Linux Forums Content Copyright©1993-2018. All Rights Reserved.
UNIX.COM Login
Username:
Password:  
Show Password