Monday, March 5, 2007
Command in discussion- "paste"
Today we will discuss about "paste". Basically it's used to merge lines of files, but it got some other uses as well. Here are they.
Normal Uses:
$ cat file1
line1 of file1
line2 of file1
line3 of file1
$ cat file2
line1 of file2
line2 of file2
line3 of file2
$ cat file3
line1 of file3
line2 of file3
line3 of file3
$ paste file1 file2 file3
line1 of file1 line1 of file2 line1 of file3
line2 of file1 line2 of file2 line2 of file3
line3 of file1 line3 of file2 line3 of file3
(-s serial)
$ paste -s file1 file2 file3
line1 of file1 line2 of file1 line3 of file1
line1 of file2 line2 of file2 line3 of file2
line1 of file3 line2 of file3 line3 of file3
(-d you can mention the delimiter)
$ paste -d : file1 file2 file3
line1 of file1:line1 of file2:line1 of file3
line2 of file1:line2 of file2:line2 of file3
line3 of file1:line3 of file2:line3 of file3
Here comes a good use of paste, formatting a output of any command with number of entries in each line as per your choice.
Suppose
$ ls
cprt.cc file1 file2 file3 main.cc newdir
Now, I want 2 file names in a line
$ ls | paste - -
cprt.cc file1
file2 file3
main.cc newdir
And three similarly
$ ls | paste - - -
cprt.cc file1 file2
file3 main.cc newdir
Sunday, March 4, 2007
Sunday's in 2008?
It sounds like a class room assignment for the students, I just did it, no reason behind it :-)
#!/bin/sh
NOYEAR=65
YEAR=$1
f_Usage()
{
echo "Usage: `basename $0`"
}
[ -z $YEAR ] && f_Usage && exit $NOYEAR
for j in `seq 12`
do
if [ `cal $j $YEAR | sed -n '3p' | wc -w` -lt 7 ]
then
cal $j $YEAR |sed -e 3d -e 2d -e '/^$/d'|awk '{print $1}'|while read i; do echo -n $i; echo -n "/"; done
else
cal $j $$YEAR |sed -e 2d -e '/^$/d'|awk '{print $1}'|while read i; do echo -n $i; echo -n "/"; done
fi
echo
done
$ ./sundays 2008
January/6/13/20/27/
February/3/10/17/24/
March/2/9/16/23/30/
April/6/13/20/27/
May/4/11/18/25/
June/1/7/14/21/28/
July/6/13/20/27/
August/3/10/17/24/31/
September/7/14/21/28/
October/5/12/19/26/
November/2/9/16/23/30/
December/7/14/21/28/
#!/bin/sh
NOYEAR=65
YEAR=$1
f_Usage()
{
echo "Usage: `basename $0`
}
[ -z $YEAR ] && f_Usage && exit $NOYEAR
for j in `seq 12`
do
if [ `cal $j $YEAR | sed -n '3p' | wc -w` -lt 7 ]
then
cal $j $YEAR |sed -e 3d -e 2d -e '/^$/d'|awk '{print $1}'|while read i; do echo -n $i; echo -n "/"; done
else
cal $j $$YEAR |sed -e 2d -e '/^$/d'|awk '{print $1}'|while read i; do echo -n $i; echo -n "/"; done
fi
echo
done
$ ./sundays 2008
January/6/13/20/27/
February/3/10/17/24/
March/2/9/16/23/30/
April/6/13/20/27/
May/4/11/18/25/
June/1/7/14/21/28/
July/6/13/20/27/
August/3/10/17/24/31/
September/7/14/21/28/
October/5/12/19/26/
November/2/9/16/23/30/
December/7/14/21/28/
Subscribe to:
Posts (Atom)
© Jadu Saikia http://unstableme.blogspot.com
