append following lines to 1st line, every 3 lines


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers append following lines to 1st line, every 3 lines
# 1  
Old 07-02-2012
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?
# 2  
Old 07-03-2012
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
$

These 3 Users Gave Thanks to jayan_jay For This Post:
# 3  
Old 07-03-2012
Quote:
Originally Posted by MaindotC
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
These 2 Users Gave Thanks to bakunin For This Post:
# 4  
Old 07-06-2012
Quote:
Originally Posted by jayan_jay
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.
# 5  
Old 07-06-2012
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

These 2 Users Gave Thanks to ctsgnb For This Post:
# 6  
Old 07-06-2012
Quote:
Originally Posted by ctsgnb
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.
# 7  
Old 07-06-2012
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

 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Get an output of lines in pattern 1st line then 10th line then 11th line then 20th line and so on.

Input file: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 (6 Replies)
Discussion started by: Sagar Singh
6 Replies

2. Shell Programming and Scripting

awk to remove lines that do not start with digit and combine line or lines

I have been searching and trying to come up with an awk that will perform the following on a converted text file (original is a pdf). 1. Since the first two lines are (begin with) text they are removed 2. if $1 is a number then all text is merged (combined) into one line until the next... (3 Replies)
Discussion started by: cmccabe
3 Replies

3. Shell Programming and Scripting

Append next line to previous lines when NF is less than 0

Hi All, This is very urgent, I've a data file with 1.7 millions rows in the file and the delimiter is cedilla and I need to format the data in such a way that if the NF in the next row is less than 1, it will append that value to previous line. Any help will be appricated. Thanks,... (17 Replies)
Discussion started by: cumeh1624
17 Replies

4. UNIX for Dummies Questions & Answers

Removing the lines which are same as 1st line

Hi, My file has the below content Heading 1 2 3 Heading 4 5 6 I need to remove the other occurrences of first line and display other lines. The content of first line is not static My output should be: Heading 1 2 (8 Replies)
Discussion started by: pandeesh
8 Replies

5. Shell Programming and Scripting

how to append multiple lines to the last line of a file

Hello, This is what I am trying to achieve: file1 a b c d file2 e f g h (8 Replies)
Discussion started by: smarones
8 Replies

6. UNIX for Dummies Questions & Answers

Get only the 1st and 8th line from every 10 lines

Hi, I have 1000 line text file. I need only the 1st and 8th line from every set of 10 lines, that is, 1,8,11,18,21,21,28,31,38,... lines etc into a text file. Please let me know how I can achieve the same. Regards, Don (2 Replies)
Discussion started by: donisback
2 Replies

7. Shell Programming and Scripting

need to delete all lines from a group of files except the 1st 2 lines

Hello, I have a group of text files with many lines in each file. I need to delete all the lines in each and only leave 2 lines in each file. (3 Replies)
Discussion started by: script_op2a
3 Replies

8. Shell Programming and Scripting

Append text to end of line on all lines

Hi, I've spent some time researching for this but can't seem to find a solution. I have a file like this 1234|Test|20101111|18:00|19:00There will be multiple lines in the file with the same kind of format. For every line I need to make it this 1234|Test|20101111|18:00|19:00||create... (5 Replies)
Discussion started by: giles.cardew
5 Replies

9. Shell Programming and Scripting

Append specific lines to a previous line based on sequential search criteria

I'll try explain this as best I can. Let me know if it is not clear. I have large text files that contain data as such: 143593502 09-08-20 09:02:13 xxxxxxxxxxx xxxxxxxxxxx 09-08-20 09:02:11 N line 1 test line 2 test line 3 test 143593503 09-08-20 09:02:13... (3 Replies)
Discussion started by: jesse
3 Replies

10. Shell Programming and Scripting

Joining lines in reverse. append line 1 to line 2.

Hi I have used many times the various methods to append two lines together in a file. This time I want to append the 1st line to the second and repeat for the complete file.... an example This is the file owns the big brown dog joe owns the small black dog jim What I want is ... (7 Replies)
Discussion started by: dwalley
7 Replies
Login or Register to Ask a Question