How do I combine the last 2 lines of a file


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers How do I combine the last 2 lines of a file
# 1  
Old 02-18-2010
Question How do I combine the last 2 lines of a file

I tried to put the history line number and the date into the file with one command, and failed. Can't figure out how to get the date variable substituted for the last space captured.

Code:
history | tail -1 | sed -e 's/.\{7\}/&/g' | head -1 | sed 's/ $/$date/'

Result was:
Code:
  729 $date

So, I sent the line number of my history to the file and the the date to the file, but I need to merge these 2 lines into one (so I can append my current history to my old history and remove duplicate lines while maintaining a date in the file for each day I login.

Code:
history | tail -1 | sed -e 's/.\{7\}/&\n/g' | head -1 >> myhistory
date >> myhistory

Resulted in:
Code:
  724
Thu Feb 18 00:16:42 MST 2010

for the last two lines of the file.


Can anyone help with either solution?

Thanks

Last edited by Scott; 02-18-2010 at 04:11 AM.. Reason: Please use code tags
# 2  
Old 02-18-2010
To get a shell variable substituted inside sed here are two suggestions how to do it:

Code:
... sed 's/ $/'$date'/'

# or

... sed "s/ $/$date/"

Btw, you can spare out head and tail and address inside sed the 1st or the last line. Checkout "addressing" in sed examples on the web.
# 3  
Old 02-18-2010
Didn't work. Thanks anyway.
# 4  
Old 02-18-2010
What did not work?

Code:
$> cat infile
AA 111 CC
DD 222 EE
BB 111 FF
$> VAR=bla; sed 's/EE$/'$VAR'/' infile
AA 111 CC
DD 222 bla
BB 111 FF
$> VAR=bla; sed "s/EE$/$VAR/" infile
AA 111 CC
DD 222 bla
BB 111 FF

If you tell more, maybe error output and maybe a snippet of your input file as example, it might be easier to help.
Did shell variable substitution inside sed not work or the whole construct?

Can't see if you fille $date with the output of the date command. Maybe you do this or just add a
Code:
`date`

instead of just putting in a $date.
# 5  
Old 02-18-2010
Solution for this problem

Try the following ,

Code:
(history | tail -1 | sed -e 's/.\{7\}/&\n/g' | head -1 ;date)>>hist ; tr -d '\012'  < hist >> myhistory ; rm -rf hist ;

Here I used the temporary file hist.

Last edited by Franklin52; 02-22-2010 at 04:29 AM.. Reason: Please use code tags, thank you!
# 6  
Old 02-18-2010
Code:
history | tail -1 | sed -e 's/.\{7\}/&\n/g' | head -1 ;date)>>hist ; tr -d '\012' < hist >> myhistory ; rm -rf hist ;

Didn't work:

Result was:
Code:
-bash: syntax error near unexpected token `)'


For:
Code:
history | tail -1 | sed -e 's/.\{7\}/&\n/g' | head -1 | sed 's/ $/'$date'/'

Result was:
Code:
  733

For:
Code:
history | tail -1 | sed -e 's/.\{7\}/&\n/g' | head -1 | sed 's/ $/$date/'

Result was:
Code:
  729 $date

P.S.

Code:
history | tail -1 | sed -e 's/.\{7\}/&\n/g' | head -1 | sed 's/ $/'date'/'

Results in:

Code:
  738 date


Last edited by jimbob75; 02-18-2010 at 05:59 AM..
# 7  
Old 02-18-2010
I think you misunderstood. Try the following:

Code:
history | tail -1 | sed -e 's/.\{7\}/&\n/g' | head -1 | sed "s/ $/`date`/"

If this still does not work - post a snippet of your history output please.

Last edited by zaxxon; 02-18-2010 at 08:29 AM.. Reason: Changed from single to double quotes
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Script to combine lines in a file

Greetings, I have large file with following format name1 name2 name3 name4 name5 name6 child7 child8 child9 <== there is leading blank space child10 child11 child12 <== there is leading blank space name13 name14 name15 name16 child17 child18... (6 Replies)
Discussion started by: rnnyusa
6 Replies

2. Shell Programming and Scripting

awk to combine matching lines in file

I am trying to combine all matching lines in the tab-delimited using awk. The below runs but no output results. Thank you :). input chrX 110925349 110925532 ALG13 chrX 110925349 110925532 ALG13 chrX 110925349 110925532 ALG13 chrX 47433390 47433999 SYN1... (3 Replies)
Discussion started by: cmccabe
3 Replies

3. Shell Programming and Scripting

Program to combine two lines in a file on checking the first character of each line

Hi, I have a requirement where I need to combine two lines in a file based on first character of each line in a file. Please find the sample content of the file below: _______________________ 5, jaya, male, 4-5-90, single smart 6, prakash, male, 5-4-84, married fair 7, raghavi, female,... (12 Replies)
Discussion started by: jayaP
12 Replies

4. UNIX for Advanced & Expert Users

Need to combine two lines in a file based on first character of each line in a file

Hi, I have a requirement where I need to combine two lines in a file based on first character of each line in a file. Please find the sample content of the file below: Code: _______________________ 5, jaya, male, 4-5-90, single smart 6, prakash, male, 5-4-84, married fair 7, raghavi,... (1 Reply)
Discussion started by: jayaP
1 Replies

5. UNIX for Dummies Questions & Answers

Need to combine two lines in a file based on first character of each line in a file

Hi, I have a requirement where I need to combine two lines in a file based on first character of each line in a file. Please find the sample content of the file below: Code: _______________________ 5, jaya, male, 4-5-90, single smart 6, prakash, male, 5-4-84, married fair 7, raghavi,... (1 Reply)
Discussion started by: jayaP
1 Replies

6. UNIX for Dummies Questions & Answers

Combine lines in file

Hi All, I am trying to understand if its possible to carry out the following. I have a text file which contains output from multiple commands, within the file a node will be quiered twice if there was 2 commands for example. Is it possible do combine 2 lines into 1 if the first word is the... (1 Reply)
Discussion started by: mutley2202
1 Replies

7. Shell Programming and Scripting

Combine multiple unique lines from event log text file into one line, use PERL or AWK?

I can't decide if I should use AWK or PERL after pouring over these forums for hours today I decided I'd post something and see if I couldn't get some advice. I've got a text file full of hundreds of events in this format: Record Number : 1 Records in Seq : ... (3 Replies)
Discussion started by: Mayday22
3 Replies

8. UNIX for Dummies Questions & Answers

Combine lines from file

Hello, Input file looks like this: apples bananas oranges and rice pears cherries mango I want output to look like this: apples bananas oranges and rice pears cherries mango It should combine line 1 with line 2 and line 3 with line 4 like that.... Right now, only way I can... (4 Replies)
Discussion started by: holyearth
4 Replies

9. Shell Programming and Scripting

Combine multiple lines in file based on specific field

Hi, I have an issue to combine multiple lines of a file. I have records as below. Fields are delimited by TAB. Each lines are ending with a new line char (\n) Input -------- ABC 123456 abcde 987 890456 7890 xyz ght gtuv ABC 5tyin 1234 789 ghty kuio ABC ghty jind 1234 678 ght ... (8 Replies)
Discussion started by: ratheesh2011
8 Replies

10. Shell Programming and Scripting

Combine all lines of a file with all lines of another file.

Dear all, I would like to combine all lines of a file with all lines of another file: The input are file 1 A B C D file 2 A B C D The output is final file A_A A_B A_C (3 Replies)
Discussion started by: valente
3 Replies
Login or Register to Ask a Question