Go Back   The UNIX and Linux Forums > Top Forums > UNIX for Dummies Questions & Answers


UNIX for Dummies Questions & Answers If you're not sure where to post a UNIX or Linux question, post it here. All UNIX and Linux newbies welcome !!

Closed Thread    
 
Thread Tools Search this Thread Display Modes
    #1  
Old 07-02-2012
Registered User
 
Join Date: May 2011
Posts: 94
Thanks: 25
Thanked 0 Times in 0 Posts
append following lines to 1st line, every 3 lines

I have output like this:

Code:
USER_ID
12/31/69 19:00:00
12/31/69 19:00:00
USER_ID
12/31/69 19:00:00
12/31/69 19:00:00
USER_ID
12/31/69 19:00:00
12/31/69 19:00:00
USER_ID
12/31/69 19:00:00
12/31/69 19:00:00
...

where USER_ID is a unique user login followed by their login timestamp and logout timestamp. How can I append the login and logout timestamps to the end of the user_id line? It would look like this:

Code:
USER_ID 12/31/69 19:00:00 12/31/69 19:00:00

I've been experimenting with this from the sed one-liners:
Quote:
Originally Posted by http://sed.sourceforge.net/sed1line.txt
# if a line ends with a backslash, append the next line to it
sed -e :a -e '/\\$/N; s/\\\n//; ta'
using this:
Code:
sed -e :a -e '/:[0-9][0-9]$/N; s/[0-9][0-9]$\n//; ta'

but it's producing:
Code:
USER_ID
12/31/69 19:00:12/31/69 19:00:USER_ID
12/31/69 19:00:12/31/69 19:00:USER_ID
12/31/69 19:00:12/31/69 19:00:USER_ID
12/31/69 19:00:12/31/69 19:00:USER_ID

Can someone advise?
Sponsored Links
    #2  
Old 07-03-2012
jayan_jay's Avatar
Forum Advisor
 
Join Date: Jul 2008
Posts: 831
Thanks: 9
Thanked 185 Times in 176 Posts

Code:
$ paste - - - < infile
USER_ID 12/31/69 19:00:00       12/31/69 19:00:00
USER_ID 12/31/69 19:00:00       12/31/69 19:00:00
USER_ID 12/31/69 19:00:00       12/31/69 19:00:00
USER_ID 12/31/69 19:00:00       12/31/69 19:00:00
$

The Following 3 Users Say Thank You to jayan_jay For This Useful Post:
MaindotC (07-06-2012), ni2 (07-03-2012), PikK45 (07-06-2012)
Sponsored Links
    #3  
Old 07-03-2012
bakunin bakunin is offline Forum Staff  
Bughunter Extraordinaire
 
Join Date: May 2005
Location: In the leftmost byte of /dev/kmem
Posts: 3,301
Thanks: 27
Thanked 457 Times in 356 Posts
Quote:
Originally Posted by MaindotC View Post
I've been experimenting with this from the sed one-liners:
using this:
Code:
sed -e :a -e '/:[0-9][0-9]$/N; s/[0-9][0-9]$\n//; ta'

but it's producing
This is easy to explain:


Code:
/:[0-9][0-9]$/N

This is saying apply the command (which is "N") to all lines of the form: a ":" followed by two digits at lines end. "N" combines a line with the following one, so it is easy to see, why your lines start with the dates and not the user-id.

What you want is to search for the user-id lines and then combine this with the next two lines. User-id lines are the ones without a date/time at the end, therefore we negate your search ("!" is a logical NOT):


Code:
/:[0-9][0-9]$/! {...}

Now lets see what we have to do with such a line: get the next two lines into our pattern space, then remove the newlines to combine the lines to one:


Code:
/:[0-9][0-9]$/! {N;N;s/\n//g;}

Now put that into a complete command:


Code:
sed '/:[0-9][0-9]$/! {N;N;s/\n/ /g;}' /path/to/input > path/to/output

Because your input file has a fixed structure (user-id, login timestamp on next line, logout timestamp on next) this will work and there is no need to take additional precautions, like in the oneliner example you found. It will fail, though, when this structure is disrupted.

I hope this helps.

bakunin
The Following 2 Users Say Thank You to bakunin For This Useful Post:
jayan_jay (07-03-2012), MaindotC (07-06-2012)
    #4  
Old 07-06-2012
Registered User
 
Join Date: May 2011
Posts: 94
Thanks: 25
Thanked 0 Times in 0 Posts
Quote:
Originally Posted by jayan_jay View Post
Code:
$ paste - - - < infile
USER_ID 12/31/69 19:00:00       12/31/69 19:00:00
USER_ID 12/31/69 19:00:00       12/31/69 19:00:00
USER_ID 12/31/69 19:00:00       12/31/69 19:00:00
USER_ID 12/31/69 19:00:00       12/31/69 19:00:00
$

Thanks, jay. I was unaware of the paste command. I was man -k "append" but if I had used the keyword "merge" I would have found it. I'm still dissecting bakunin's explanation.
Sponsored Links
    #5  
Old 07-06-2012
ctsgnb ctsgnb is offline Forum Advisor  
Registered User
 
Join Date: Oct 2010
Location: France
Posts: 2,763
Thanks: 72
Thanked 587 Times in 561 Posts
Just another sed statement if your whole file is formatted the same way than in your example :


Code:
sed 'N;N;s/\n/ /g' yourfile

The Following 2 Users Say Thank You to ctsgnb For This Useful Post:
MaindotC (07-06-2012), PikK45 (07-06-2012)
Sponsored Links
    #6  
Old 07-06-2012
Registered User
 
Join Date: May 2011
Posts: 94
Thanks: 25
Thanked 0 Times in 0 Posts
Quote:
Originally Posted by ctsgnb View Post
Just another sed statement if your whole file is formatted the same way than in your example :


Code:
sed 'N;N;s/\n/ /g' yourfile

This one's even better because it doesn't add the extra-large space (perhaps a tab?) before the last set. Either way I have to read up on this "N" business in sed.
Sponsored Links
    #7  
Old 07-06-2012
ctsgnb ctsgnb is offline Forum Advisor  
Registered User
 
Join Date: Oct 2010
Location: France
Posts: 2,763
Thanks: 72
Thanked 587 Times in 561 Posts
You can also specify a delimiter (here, a space) with the paste alternative (to avoid the tabs that are used by default):


Code:
paste -d" " - - - <yourfile

Sponsored Links
Closed 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
how to append multiple lines to the last line of a file smarones Shell Programming and Scripting 8 11-06-2011 02:39 AM
Append text to end of line on all lines giles.cardew Shell Programming and Scripting 5 11-26-2010 09:10 AM
Append transaction header lines to same transaction's detail lines rookie12 Shell Programming and Scripting 3 06-18-2010 02:01 AM
Append specific lines to a previous line based on sequential search criteria jesse Shell Programming and Scripting 3 08-21-2009 02:01 AM
Joining lines in reverse. append line 1 to line 2. dwalley Shell Programming and Scripting 7 08-04-2008 07:11 AM



All times are GMT -4. The time now is 10:24 AM.