join last 2 lines of a file 
Author Message
 join last 2 lines of a file

Hello
I am trying to find a way to display
the last two lines of a file joined together

for example the file contains:
aaaaaa
bb
ccc
ddd

desired output
ccc ddd

any awk or sed sugestions/examples are welcome



Thu, 29 Jul 2004 21:44:20 GMT  
 join last 2 lines of a file

Quote:

> Hello
> I am trying to find a way to display
> the last two lines of a file joined together

> for example the file contains:
> aaaaaa
> bb
> ccc
> ddd

> desired output
> ccc ddd

> any awk or sed sugestions/examples are welcome

awk '{penultimate=last}{last=$0}END{print penultimate,last}'

HTH
--
roland



Thu, 29 Jul 2004 22:47:42 GMT  
 join last 2 lines of a file

Quote:


>>Hello
>>I am trying to find a way to display
>>the last two lines of a file joined together

>>for example the file contains:
>>aaaaaa
>>bb
>>ccc
>>ddd

>>desired output
>>ccc ddd

>>any awk or sed sugestions/examples are welcome

> tail -2 thefilename | tr '\n' ' '

> no need for awk :-)

ok, then sed :  

sed -n '$!h;${H;g;s/\n/ /p;}'  
--
roland



Fri, 30 Jul 2004 02:49:10 GMT  
 join last 2 lines of a file


Quote:
> Hello
> I am trying to find a way to display
> the last two lines of a file joined together

> for example the file contains:
> aaaaaa
> bb
> ccc
> ddd

> desired output
> ccc ddd

> any awk or sed sugestions/examples are welcome

Here's a silly way to do it in sed

sed -n -e '${' -e H -e g -e 's/\n/ /' -e p -e '}' -e h

Put every line, except the last line, into the hold space, replacing
what's in the hold space. For the last line only, append to the hold
space with a preceding newline, copy it all back to the pattern space,
change the newline to a space, and print the pattern space.

--
Frederick



Fri, 30 Jul 2004 02:54:01 GMT  
 join last 2 lines of a file


Quote:


>> Hello
>> I am trying to find a way to display
>> the last two lines of a file joined together

>> for example the file contains:
>> aaaaaa
>> bb
>> ccc
>> ddd

>> desired output
>> ccc ddd

>> any awk or sed sugestions/examples are welcome

I have not seen an awk one yet - If I read the specs above correctly
this seems to work with Solaris old awk even --

awk '{prev=last} {last=$0} END {printf("%s %s\n",prev,last)}' atextfile

awk rocks : >



Fri, 30 Jul 2004 04:42:58 GMT  
 join last 2 lines of a file

banged randomly at the keyboard, until the following was produced:

Quote:
> Hello
> I am trying to find a way to display
> the last two lines of a file joined together

> for example the file contains:
> aaaaaa
> bb
> ccc
> ddd

> desired output
> ccc ddd

> any awk or sed sugestions/examples are welcome

tail -2 infile | awk 'BEGIN{ORS=" "}{print}' > outfile

Michael Heiming
--
Remove the +SIGNS case mail bounces.



Fri, 30 Jul 2004 09:37:16 GMT  
 join last 2 lines of a file

Quote:
> tail -2 thefilename | tr '\n' ' '
> no need for awk :-)

A good solution. Note, however, that depending on the platform tail
and tr might not be at hand, while an awk-port could. Such a
situation is quite conceivable e.g. in MS-DOS. Of course, there
_are_ tail and tr port to MS-DOS as well.

In case of MS-DOS rather than the usual default assumption Unix one
of the options is

  type myfile.txt|gawk '{prev=last}{last=$0}END{printf "%%s %%s\n",prev,$0}'

   All the best, Timo

--
Prof. Timo Salmi ftp & http://garbo.uwasa.fi/ archives 193.166.120.5
Department of Accounting and Business Finance  ; University of Vaasa

Timo's  FAQ  materials  at   http://www.uwasa.fi/~ts/http/tsfaq.html



Fri, 30 Jul 2004 14:41:44 GMT  
 join last 2 lines of a file
...

Quote:
>In case of MS-DOS rather than the usual default assumption Unix one
>of the options is

>  type myfile.txt|gawk '{prev=last}{last=$0}END{printf "%%s %%s\n",prev,$0}'

>   All the best, Timo

Of course, this gets you a UUOT award...


Fri, 30 Jul 2004 21:25:40 GMT  
 join last 2 lines of a file

Quote:


> >  type myfile.txt|gawk '{prev=last}{last=$0}END{printf "%%s %%s\n",prev,$0}'
> Of course, this gets you a UUOT award...

Your, of course, Kenny. Thank you for the cute touch of wit. A UUOC
would have been so pedestrian. (Each subculture for his/her own).

   All the best, Timo

--
Prof. Timo Salmi ftp & http://garbo.uwasa.fi/ archives 193.166.120.5
Department of Accounting and Business Finance  ; University of Vaasa

Timo's  FAQ  materials  at   http://www.uwasa.fi/~ts/http/tsfaq.html



Sat, 31 Jul 2004 02:04:48 GMT  
 join last 2 lines of a file

Quote:

> Hello
> I am trying to find a way to display
> the last two lines of a file joined together

> for example the file contains:
> aaaaaa
> bb
> ccc
> ddd

> desired output
> ccc ddd

> any awk or sed sugestions/examples are welcome

Silly way from BEGIN block since I haven't seen one yet.

BEGIN {
   while ((getline array[i++] < var) > 0) {
  }
  print "Count",i
    for (all in array) {
        print "Index:",all,"Element:", array[all]
         if (all == (i - 1)) {
              last = array[all]
           }
       if (all == (i - 2)) {
           secondtol = array[all]
        }
     }
  printf "\nLast:%s,\nsecondtolast:%s\n", last,secondtol
  close(var)

Quote:
}

(also would make a decent function)
To run:
awk -v var="filename" -f scriptname


Sat, 31 Jul 2004 10:37:32 GMT  
 
 [ 10 post ] 

 Relevant Pages 

1. Join multiple lines of records to single line

2. read the last line in file

3. deleting newline from last line in file

4. print last line of input file

5. how to specify last line of file ?

6. How to read the x last lines of a text file

7. How to read the last line of http:// file

8. File: How to read the last line?

9. Best way to get Last line of file?

10. last 20 lines of a file on WinDoze ?

11. Deleting the last lines in a text file with tcl

12. Removing last line while appending to file

 

 
Powered by phpBB® Forum Software