|
|||||||
| Forums | Search Forums | Register | Forum Rules | Man Pages | Albums | FAQ | Members | Calendar | Search | Today's Posts | Mark Forums Read |
| 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 !! |
|
|
|
Thread Tools | Search this Thread | Display Modes |
|
#1
|
|||
|
|||
|
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:
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
|
||||
|
||||
|
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 $ |
| Sponsored Links | ||
|
|
#3
|
|||
|
|||
|
Quote:
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/outputBecause 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 |
|
#4
|
|||
|
|||
|
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
|
|||
|
|||
|
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 |
| Sponsored Links | |
|
|
#6
|
|||
|
|||
|
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
|
|||
|
|||
|
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 | ||
|
![]() |
| Thread Tools | Search this Thread |
| 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 |
|
|