Shell Script to Print Only First / Last Words in all Lines of a Text File

I have a text file and I wanted to print only first and last words of the file.
My sample input file is with following data
$ cat dat
 MONTH YYYY WK Mo Tu We Th Fr Sa Su
01-JAN 2013 01    01 02 03 04 05 06
01-JAN 2013 02 07 08 09 10 11 12 13
01-JAN 2013 03 14 15 16 17 18 19 20

01-JAN 2013 04 21 22 23 24 25 26 27
01-JAN 2013 05 28 29 30 31
02-FEB 2013 05             01 02 03
02-FEB 2013 06 04 05 06 07 08 09 10
02-FEB 2013 07 11 12 13 14 15 16 17
02-FEB 2013 08 18 19 20 21 22 23 24
02-FEB 2013 09 25 26 27 28

Method 1: Using cut and while loop
while read -r line
 do
 echo $line|cut -d " " –f1,`echo $line|wc -w`
 done
Method 2: Using awk command.
$ awk '{ print $1” “ $NF }' dat
Output would be like this
MONTH Su
01-JAN 06
01-JAN 13
01-JAN 20
01-JAN 27
01-JAN 31
02-FEB 03
02-FEB 10
02-FEB 17
02-FEB 24

02-FEB 28

DBA Tips Data Pump Reference

0 comments:

Post a Comment

 

dba topics. Copyright 2011-16 All Rights Reserved | Site Map | Contact | Disclaimer | Google