Unix/Linux Go Back    


Shell Programming and Scripting 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

Shell Programming and Scripting


Closed    
 
Thread Tools Search this Thread Display Modes
    #1  
Old Unix and Linux 05-16-2009   -   Original Discussion by cgkmal
cgkmal's Unix or Linux Image
cgkmal cgkmal is offline
Registered User
 
Join Date: Jan 2009
Last Activity: 7 November 2015, 3:54 PM EST
Location: Tegucigalpa, Honduras
Posts: 290
Thanks: 8
Thanked 37 Times in 36 Posts
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
Sponsored Links
    #2  
Old Unix and Linux 05-16-2009   -   Original Discussion by cgkmal
research3's Unix or Linux Image
research3 research3 is offline
Registered User
 
Join Date: Nov 2008
Last Activity: 13 December 2011, 4:30 PM EST
Location: Germany
Posts: 123
Thanks: 3
Thanked 3 Times in 3 Posts
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!
Sponsored Links
    #3  
Old Unix and Linux 05-16-2009   -   Original Discussion by cgkmal
ghostdog74's Unix or Linux Image
ghostdog74 ghostdog74 is offline
Registered User
 
Join Date: Sep 2006
Last Activity: 28 January 2015, 8:30 AM EST
Posts: 2,669
Thanks: 0
Thanked 18 Times in 18 Posts
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 Unix and Linux 05-16-2009   -   Original Discussion by cgkmal
durden_tyler's Unix or Linux Image
durden_tyler durden_tyler is offline Forum Advisor  
Registered User
 
Join Date: Apr 2009
Last Activity: 20 April 2018, 12:42 PM EDT
Posts: 2,086
Thanks: 22
Thanked 384 Times in 347 Posts
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
Sponsored Links
    #5  
Old Unix and Linux 05-17-2009   -   Original Discussion by cgkmal
cgkmal's Unix or Linux Image
cgkmal cgkmal is offline
Registered User
 
Join Date: Jan 2009
Last Activity: 7 November 2015, 3:54 PM EST
Location: Tegucigalpa, Honduras
Posts: 290
Thanks: 8
Thanked 37 Times in 36 Posts
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.

Linux
Sponsored Links
Closed

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Linux More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
sed / awk to concatenate lines until blank line dunstonrocks Shell Programming and Scripting 11 03-30-2009 11:11 PM
Logfile - extracting certain lines to concatenate into 1 line BecTech Shell Programming and Scripting 3 03-30-2009 07:28 PM
How to concatenate consecutive lines shivi707 UNIX Desktop Questions & Answers 1 01-12-2009 08:08 AM
concatenate and display 2 lines as 1 with a condition for 2 line ? vithala Shell Programming and Scripting 7 07-11-2008 01:01 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 06:08 PM.