what sed command to print the first line and the last line

what sed command to print the first line and the last line

Post by rawsh » Sat, 08 Feb 2003 08:36:17



when I use the grep and awk I get the time stamp list but I only need
first line and last line. I dont know the number of line as it changes
so it is hard to print the last line.

thanks in advance

bash-2.03$ egrep -i "start|attempt" 1003F4F.ACT | awk '{FS=","}{print
$7}'
23:29:21.1 <----------- first line to be printed
23:29:41.8
23:29:43.6
23:29:42.1
23:29:46.8
23:29:42.4
23:29:58.6
23:29:53.8
23:29:35.8
23:30:08.2
23:29:43.5
23:29:57.9
23:30:07.5
23:30:18.2
23:30:14.2
23:30:10.7
23:30:21.9
23:30:19.8
23:30:21.9
23:30:24.0
23:30:21.7
23:30:37.0
23:30:23.7
23:30:43.2
23:30:35.9
23:30:45.2
23:30:48.1
23:30:43.6
23:28:58.7  <---------------- last needs to print(changes)
bash-2.03$

 
 
 

what sed command to print the first line and the last line

Post by Brian Hile » Sat, 08 Feb 2003 09:04:50



> when I use the grep and awk I get the time stamp list but I only need
> first line and last line. I dont know the number of line as it changes
> so it is hard to print the last line.
> thanks in advance
> bash-2.03$ egrep -i "start|attempt" 1003F4F.ACT | awk '{FS=","}{print
> $7}'
> 23:29:21.1 <----------- first line to be printed
> 23:30:43.6
> 23:28:58.7  <---------------- last needs to print(changes)

sed -n '1p;$p' 1003F4F.ACT

=Brian

 
 
 

what sed command to print the first line and the last line

Post by Chris F.A. Johnso » Sat, 08 Feb 2003 11:30:09



> when I use the grep and awk I get the time stamp list but I only need
> first line and last line. I dont know the number of line as it changes
> so it is hard to print the last line.

> bash-2.03$ egrep -i "start|attempt" 1003F4F.ACT | awk '{FS=","}{print
> $7}'
> 23:29:21.1 <----------- first line to be printed
> 23:29:41.8
[snip]
> 23:30:43.6
> 23:28:58.7  <---------------- last needs to print(changes)
> bash-2.03$

egrep -i "start|attempt" 1003F4F.ACT | {
    read -r h
    echo $h
    tail -1
  } | cut -d, -f7

   Or:

egrep -i "start|attempt" 1003F4F.ACT | {
    read -r h
    echo $h
    tail -1
  } |  while IFS=, read a b c d e f g h; do echo $g; done

   Or:

awk 'BEGIN { FS = "," }
     /start|attempt/ { if ( n++ == 0 ) print $7
                       else line = $7 }
     END { print line }' 1003F4F.ACT

--
    Chris F.A. Johnson                        http://cfaj.freeshell.org
    ===================================================================
    My code (if any) in this post is copyright 2003, Chris F.A. Johnson
    and may be copied under the terms of the GNU General Public License

 
 
 

what sed command to print the first line and the last line

Post by William Par » Sat, 08 Feb 2003 12:58:09



> when I use the grep and awk I get the time stamp list but I only need
> first line and last line. I dont know the number of line as it changes
> so it is hard to print the last line.
> 23:29:21.1 <----------- first line to be printed
> 23:29:41.8
...
> 23:30:43.6
> 23:28:58.7  <---------------- last needs to print(changes)

How about
    awk 'NR==1 {print}
         END {print $0}'
or
    sed -n -e '1p' -e '$p'

--

Linux solution for data management and processing.

 
 
 

what sed command to print the first line and the last line

Post by John W. Krah » Sat, 08 Feb 2003 16:04:24



> when I use the grep and awk I get the time stamp list but I only need
> first line and last line. I dont know the number of line as it changes
> so it is hard to print the last line.

> thanks in advance

> bash-2.03$ egrep -i "start|attempt" 1003F4F.ACT | awk '{FS=","}{print
> $7}'
> 23:29:21.1 <----------- first line to be printed
> 23:29:41.8

> [snip]

> 23:30:43.6
> 23:28:58.7  <---------------- last needs to print(changes)
> bash-2.03$


John
--
use Perl;
program
fulfillment

 
 
 

what sed command to print the first line and the last line

Post by Dr. Yuan Li » Sat, 08 Feb 2003 16:23:31




>>when I use the grep and awk I get the time stamp list but I only need
>>first line and last line. I dont know the number of line as it changes
>>so it is hard to print the last line.

>>bash-2.03$ egrep -i "start|attempt" 1003F4F.ACT | awk '{FS=","}{print
>>$7}'
>>23:29:21.1 <----------- first line to be printed
>>23:29:41.8

> [snip]

>>23:30:43.6
>>23:28:58.7  <---------------- last needs to print(changes)
>>bash-2.03$

> egrep -i "start|attempt" 1003F4F.ACT | {
>     read -r h
>     echo $h
>     tail -1
>   } | cut -d, -f7

>    Or:

> egrep -i "start|attempt" 1003F4F.ACT | {
>     read -r h
>     echo $h
>     tail -1
>   } |  while IFS=, read a b c d e f g h; do echo $g; done

>    Or:

> awk 'BEGIN { FS = "," }
>      /start|attempt/ { if ( n++ == 0 ) print $7
>                        else line = $7 }
>      END { print line }' 1003F4F.ACT

Similarly in sed,
$ sed -n '/start/b7;/attempt/b7;b
:7
s/^\([^,]*,\)\{6\}\(\[^,]*\).*/\1/p'
1003F4F.ACT

Breaks my neck to watch grep (with e!), awk, and sed conjuncted.

Yuan Liu

 
 
 

what sed command to print the first line and the last line

Post by Tim Cargi » Sun, 09 Feb 2003 00:28:56



> when I use the grep and awk I get the time stamp list but I only need
> first line and last line. I dont know the number of line as it changes
> so it is hard to print the last line.

> thanks in advance

> bash-2.03$ egrep -i "start|attempt" 1003F4F.ACT | awk '{FS=","}{print
> $7}'
> 23:29:21.1 <----------- first line to be printed
> 23:29:41.8
> [ ... ]
> 23:30:43.6
> 23:28:58.7 <---------------- last needs to print(changes)
> bash-2.03$

It's not absolutely clear to me what the triggering (start/attempt)
words are for the first and last lines, but assuming something
that looks like this:

23:29:21.1,START,f3,f4,f5,f6,f7A
23:29:21.1,staxt,f3,f4,f5,f6,f7a
23:29:41.8,junk,f3,f4,f5,f6,f7
[ ... ]
23:29:42.8,junk,f3,f4,f5,f6,f7
23:28:58.7,attempt,f3,f4,f5,f6,f7b
23:28:58.7,ATTEMPT,f3,f4,f5,f6,f7B

If your 'egrep' supports the -m option:

     egrep -i -m 2 "start|attempt" filename | cut -d',' -f7

If not, then (the relatively inefficient):

  unset c7p

  egrep -i "start|attempt" filename \
        | while IFS=, read c1 c2 c3 c4 c5 c5 c7 cjunk;do
                #echo "read $c1 $c2 $c3 $c4 $c5 $c6 $c7"
                if [ -z "$c7p" ];then
                        c7p=$c7
                else
                        echo "$c7p"
                        echo "$c7"
                        break  
                fi
        done

As far as 'sed' goes ... I makes my brain fry ... and I'm still trying
to get Dr. Liu's solution to run with output occurring ...

HTH

Tim - PITA