cut
cut in unix is not exactly the same as the cut option in gui based windows. cut is
a text manipulative command that is widely used in shell scripting .
You can cut fields, same as the way you do by using awk, or cut a required number of characters from the lines of a file.
Example1)
You have to read only a particular column from a file,use cut with –d option along with -f.
Suppose the file contains the following content.
/home/khan $ cat workingTime.txt
Ist II Break III IV
10:00-:11:20am 11:25am-12:30pm 2:30-1:45pm 1:45-3:00pm 3:05-4:30pm
10:00-:11:15am 11:20am-12:40pm 12:40-1:30pm 1:30-2:45pm 2:50-4:20pm
10:00-:11:00am 11:00am-12:00pm 12:00-1:30pm 1:35-3:00pm 3:05-4:15pm
10:00-:11:20am 11:25am-12:30pm 12:30-1:45pm 1:45-3:00pm 3:05-4:30pm
10:00-:11:15am 11:20am-12:25pm 12:25-1:30pm 1:30-2:45pm 2:50-4:20pm
Five rows for five working days
/home/khan $
Note: here there is only one space between each entry in the column. I have used longer spaces for better visibility.
To get only the timings for the break, use the following command.
/home/khan $ cut –f3 –d “ “ workingTime.txt
Break
2:30-1:45pm
12:40-1:30pm
12:00-1:30pm
12:30-1:45pm
12:25-1:30pm
for
similarly if the columns in the file is separated by some other characters such as , or | or – etc include them as an argument after –d
Example 2)
suppose you have a variable which stores a date in a particular format and you want to access the month, year and day, use it as shown by the script.
/home/khan $ cat bday.sh
echo "ENTER YOUR BIRTH DAY (MMDDYYYY) "
read b_date
b_yr=`echo $b_date | cut –c5-8`
dt=`echo $b_date|cut –c3-4`
mon=`echo $b_date | cut –c1-2`
case $mon in
01) month=JAN;;
02) month=FEB;;
03) month=MAR;;
04) month=APR;;
05) month=MAY;;
06) month=JUN;;
07) month=JUL;;
08) month=AUG;;
09) month=SEP;;
10) month=OCT;;
11) month=NOV;;
12) month=DEC;;
esac
echo “YOU WERE BORN ON $month MONTH”
Example 3)
It is needed to display all the characters starting from nth character, use cut as shown.
/home/khan$ cat expense.txt
145.8967
134.56
911.342
100.67890
234.5
867.4545
If you need to fetch the decimal value,
/home/khan$ cut -c4- expense.txt
.8967
.56
.342
.67890
.5
.4545
Similarly if you want to
1) cut all starting characters up to 4th character
2)1st 2 characters then 5 th to 7th character use
cut -c -4 and
cut -c -2,5-7 respectively.
Example 4)
There is sometimes an unusual situation where you want to cut only backwards.
i:e you only know that nth field starting from end of a line is fixed and you want to cut the nth character. Here it is done with cut and rev command (only in Aix,Linux. If you do not have rev use this url http://www.askdavetaylor.com/where_can_i_find_the_rev_utility_for_solaris.html to get a c code for rev and compile and save it in a path defined in your $PATH variable).
Consider a file which contains patterns as shown.
/home/khan$ cat fool-art-works.lst
Heart_shaped_rice_1853.jpg
Oils_And_Sand_Stones_Tree_3567.png
Beetles_Moths_Sun_Butter_20954.bmp
Cats_Fight_Dogs_Donkeys_450.jpeg
You may have to access the code number and file extension which are fixed to appear as last 2 fields among all the lines.
Use cut and rev as follows.
/home/khan$ for line in `cat fool-art-works.lst `
>do
>echo “$line” | rev | cut -f1 -d “_” | rev
>done
_1853.jpg
_3567.png
_20954.bmp
_450.jpeg
You can further use pipes and cuts to separate number and extension from these line strings..now you know how…!!
Really nice article. Helped me a lot.
ReplyDeleteLinux cut command examples