Hi, if you have to display only the odd lines from the file we can try
let file
$ cat myfile.txt
1
2
3
4
5
6
7
8
9
$ sed -ne '1~2p' myfile.txt
1
3
5
7
9
$ awk '{if (NR%2==1) {print;} }' myfile.txt
1
3
5
7
9
Friday, November 4, 2011
Friday, October 14, 2011
Scatter Gather on linux
Hi,
I have been doing this many times but never blogged, its the scatter gather use to be on dbase days;
This program:
$ cat hello.sh
source e/big.conf
set `date` && export WeekDay=$1 Month=$2 Day=$3 Time=$4 Year=$6
echo Hello $NAME
echo $NO
echo Time: $Time
echo Date: $WeekDay $Month $Day $Year
$ sh hello.sh
Hello SmaLl
2000
Time: 11:24:29
Date: Fri Oct 14 2011
In hello.sh, i am calling the name/value pairs file,
$ cat e/big.conf
Exporting the date o/p to the positional parameters, hey did it in college days.
set `date` && export WeekDay=$1 Month=$2 Day=$3 Time=$4 Year=$6
Here we can have large name value pairs from static file or runtime command and play with it.
I have been doing this many times but never blogged, its the scatter gather use to be on dbase days;
This program:
$ cat hello.sh
source e/big.conf
set `date` && export WeekDay=$1 Month=$2 Day=$3 Time=$4 Year=$6
echo Hello $NAME
echo $NO
echo Time: $Time
echo Date: $WeekDay $Month $Day $Year
$ sh hello.sh
Hello SmaLl
2000
Time: 11:24:29
Date: Fri Oct 14 2011
In hello.sh, i am calling the name/value pairs file,
$ cat e/big.conf
NAME=SmaLl
NO=2000
Exporting the date o/p to the positional parameters, hey did it in college days.
set `date` && export WeekDay=$1 Month=$2 Day=$3 Time=$4 Year=$6
Here we can have large name value pairs from static file or runtime command and play with it.
Wednesday, August 24, 2011
more on date and old date
Hi,
I had to work with epoch dates, that was older than the current, struggled a bit.
check this,
dt2=`date -d "$dt 1 hour ago"`
s1=`date -d "$dt1" +"%s"`
s2=`date -d "$dt2" +"%s"`
echo Now - $dt1
echo
echo "1 Hour Before -- $dt2 : $s2"
echo "Now -- $dt1 : $s1"
put whatever for 1 hour ago as (ago for old, just 1 hour for future, you can have day, minutes)
change dt1 for any date, here its current can be date by -d option
PS:
check good examples in the info pages, i checked this late.. :(
$info date examples
I had to work with epoch dates, that was older than the current, struggled a bit.
check this,
#!/bin/bash
dt1=`date`dt2=`date -d "$dt 1 hour ago"`
s1=`date -d "$dt1" +"%s"`
s2=`date -d "$dt2" +"%s"`
echo Now - $dt1
echo
echo "1 Hour Before -- $dt2 : $s2"
echo "Now -- $dt1 : $s1"
put whatever for 1 hour ago as (ago for old, just 1 hour for future, you can have day, minutes)
change dt1 for any date, here its current can be date by -d option
PS:
check good examples in the info pages, i checked this late.. :(
$info date examples
Tuesday, May 10, 2011
reading from file or stream
check Occurrences of words in file
What if you had to use if frequently.. simple put it in shell program,
and allow it to either read the given file or from the stream, like wc does.
man bash|col -b > bash.txt
$ wc bash.txt
4910 37273 260193 bash.txt
$ cat bash.txt |wc
4910 37273 260193
the below program does it 'occurrences'
$ cat bash.txt | occurrences
2969 the
1501 is
948 to
944 of
868 a
620 and
..
..
What if you had to use if frequently.. simple put it in shell program,
and allow it to either read the given file or from the stream, like wc does.
man bash|col -b > bash.txt
$ wc bash.txt
4910 37273 260193 bash.txt
$ cat bash.txt |wc
4910 37273 260193
the below program does it 'occurrences'
#!/bin/bash
#program: occurrences
file=${1:--}
cat $file | sed -e 's/[^a-zA-Z0-9.]/ /g' | xargs | tr ' ' '\n' | sort | uniq -c | sort -nr -k1,2
cat $file | sed -e 's/[^a-zA-Z0-9.]/ /g' | xargs | tr ' ' '\n' | sort | uniq -c | sort -nr -k1,2
$ cat bash.txt | occurrences
2969 the
1501 is
948 to
944 of
868 a
620 and
..
..
converting man to text
This will give the text output for 'wc' man page
$ man wc |col -b
this man page may not be that big, try 'bash'
$ man bash|col -b
redirect it to a file, dont ask me how.. this is quite helpful, when you open the text page in you favourite editor and search and read the options on 'bash'
its quite big and does lots of things you tried using other commands.
$ man wc |col -b
this man page may not be that big, try 'bash'
$ man bash|col -b
redirect it to a file, dont ask me how.. this is quite helpful, when you open the text page in you favourite editor and search and read the options on 'bash'
its quite big and does lots of things you tried using other commands.
Wednesday, April 27, 2011
more on time zones
converting to UTC from PDT or IST (date needs timezone short form)
$ TZ="UTC" date -d "Wed Apr 27 23:00:00 PDT 2011"
Thu Apr 28 06:00:00 UTC 2011
$ TZ="UTC" date -d "Wed Apr 27 23:00:00 IST 2011"
Wed Apr 27 17:30:00 UTC 2011
$ TZ="UTC" date -d "Wed Apr 27 11:00:00 IST 2011"
Wed Apr 27 05:30:00 UTC 2011
$ TZ="UTC" date -d "Wed Apr 27 11:00:00 PST 2011"
Wed Apr 27 19:00:00 UTC 2011
$ TZ="UTC" date -d "Wed Apr 27 11:00:00 PDT 2011"
Wed Apr 27 18:00:00 UTC 2011
$ TZ="UTC" date -d "Wed Apr 27 23:00:00 PDT 2011"
Thu Apr 28 06:00:00 UTC 2011
$ TZ="UTC" date -d "Wed Apr 27 23:00:00 IST 2011"
Wed Apr 27 17:30:00 UTC 2011
$ TZ="UTC" date -d "Wed Apr 27 11:00:00 IST 2011"
Wed Apr 27 05:30:00 UTC 2011
$ TZ="UTC" date -d "Wed Apr 27 11:00:00 PST 2011"
Wed Apr 27 19:00:00 UTC 2011
$ TZ="UTC" date -d "Wed Apr 27 11:00:00 PDT 2011"
Wed Apr 27 18:00:00 UTC 2011
Tuesday, April 5, 2011
Date for different time zone
Get the right date of other timezone
$ date
Tue Apr 5 12:55:41 IST 2011
$ TZ=UTC date
Tue Apr 5 07:25:47 UTC 2011
$ TZ="America/Los_Angeles" date
Tue Apr 5 00:25:50 PDT 2011
$ TZ="America/New_York" date
Tue Apr 5 03:26:00 EDT 2011
$ TZ="Europe/Paris" date
Tue Apr 5 09:28:28 CEST 2011
$ date
Tue Apr 5 12:58:37 IST 2011
reference:http://en.wikipedia.org/wiki/List_of_tz_database_time_zones#List
$ date
Tue Apr 5 12:55:41 IST 2011
$ TZ=UTC date
Tue Apr 5 07:25:47 UTC 2011
$ TZ="America/Los_Angeles" date
Tue Apr 5 00:25:50 PDT 2011
$ TZ="America/New_York" date
Tue Apr 5 03:26:00 EDT 2011
$ TZ="Europe/Paris" date
Tue Apr 5 09:28:28 CEST 2011
$ date
Tue Apr 5 12:58:37 IST 2011
reference:http://en.wikipedia.org/wiki/List_of_tz_database_time_zones#List
Wednesday, February 23, 2011
Bash Shell Shortcuts
Bash Shell Shortcuts (Alt - escape in MAC)
Ctrl A - Jump to the begin of line(BOL)
Ctrl E - Jump to the end of line(EOL)
Ctrl B - Move back a char
Ctrl F - Move forward a char
Alt B - Move backward word
Alt F - Move forward word
Alt C - Capitalize the word
Alt L - Make word lowercase
Alt U - Make word uppercase
Alt D - Delete word to right
Ctrl D - Delete char from under the cursor
Alt Backspace - Delete backward from cursor
Ctrl U - Delete from cursor to BOL
Ctrl K - Delete from cursor to EOL
Ctrl C - Terminate the command
Ctrl L - Clear the screen
Ctrl R - Search the history backwards
Ctrl xx - Move between BOL and current cursor position
Ctrl Z - Suspend/ Stop the command
Alt < - Move to the first line in the history
Alt > - Move to the last line in the history
Alt ? - Show current completion list
Alt * - Insert all possible completions
Alt / - Attempt to complete filename
Alt . - Yank last argument to previous command
Alt T - swaps the last two words on curser position and before
Tab twice(TT) - all commands
ls TT - files and directories in current directory
stringTT - starting with string commands
/TT - directory with hidden
*TT - sub dirs without hidden
~TT - users from /etc/passwd
$TT - All Sys variables
@TT - Entries from "/etc/hosts"
=TT - Output like ls or dir
Ctrl A - Jump to the begin of line(BOL)
Ctrl E - Jump to the end of line(EOL)
Ctrl B - Move back a char
Ctrl F - Move forward a char
Alt B - Move backward word
Alt F - Move forward word
Alt C - Capitalize the word
Alt L - Make word lowercase
Alt U - Make word uppercase
Alt D - Delete word to right
Ctrl D - Delete char from under the cursor
Alt Backspace - Delete backward from cursor
Ctrl U - Delete from cursor to BOL
Ctrl K - Delete from cursor to EOL
Ctrl C - Terminate the command
Ctrl L - Clear the screen
Ctrl R - Search the history backwards
Ctrl xx - Move between BOL and current cursor position
Ctrl Z - Suspend/ Stop the command
Alt < - Move to the first line in the history
Alt > - Move to the last line in the history
Alt ? - Show current completion list
Alt * - Insert all possible completions
Alt / - Attempt to complete filename
Alt . - Yank last argument to previous command
Alt T - swaps the last two words on curser position and before
Tab twice(TT) - all commands
ls TT - files and directories in current directory
stringTT - starting with string commands
/TT - directory with hidden
*TT - sub dirs without hidden
~TT - users from /etc/passwd
$TT - All Sys variables
@TT - Entries from "/etc/hosts"
=TT - Output like ls or dir
Tuesday, February 22, 2011
who logged in last since last two hours
when we have to check who have logged in last few minutes, here say two hours, or 120minutes.
who | awk -v d=`date +"%Y-%m-%d"`
-v h=`date +"%H"`
-v m=`date +"%M"`
' { split($4,wa,":");
wc=wa[1]*60+wa[2];
dc=h*60+m;
if ($3==d && dc-wc<=120 ) printf ("%s\n",$0)
}'
you can change that dc-wc<=120 -- this 120 to any minutes to check
dc - is the current system date time (Hours and minutes converted into minutes),
wc - is the date time from who command for user logged in
explained with example
$date
Tue Feb 22 09:01:05 UTC 2011
$ who
sanjeev pts/0 2011-02-22 07:54 (what.is.the.host)
jro pts/1 2011-02-21 16:43 (what.is.the.host)
beak pts/2 2011-02-22 00:49 (beak.has.the.host)
madrose pts/3 2011-02-22 07:59 (what.is.the.host)
onthepk pts/4 2011-02-22 08:00 (is.this.the.host)
anita pts/6 2011-02-22 02:35 (what.is.the.host)
$ who|awk -v d=`date +"%Y-%m-%d"` -v h=`date +"%H"` -v m=`date +"%M"` '{ split($4,wa,":"); wc=wa[1]*60+wa[2]; dc=h*60+m; if ($3==d && dc-wc<=120 ) printf ("%s\n",$0) }'
sanjeev pts/0 2011-02-22 07:54 (what.is.the.host)
madrose pts/3 2011-02-22 07:59 (what.is.the.host)
onthepk pts/4 2011-02-22 08:00 (is.this.the.host)
who | awk -v d=`date +"%Y-%m-%d"`
-v h=`date +"%H"`
-v m=`date +"%M"`
' { split($4,wa,":");
wc=wa[1]*60+wa[2];
dc=h*60+m;
if ($3==d && dc-wc<=120 ) printf ("%s\n",$0)
}'
you can change that dc-wc<=120 -- this 120 to any minutes to check
dc - is the current system date time (Hours and minutes converted into minutes),
wc - is the date time from who command for user logged in
explained with example
$date
Tue Feb 22 09:01:05 UTC 2011
$ who
sanjeev pts/0 2011-02-22 07:54 (what.is.the.host)
jro pts/1 2011-02-21 16:43 (what.is.the.host)
beak pts/2 2011-02-22 00:49 (beak.has.the.host)
madrose pts/3 2011-02-22 07:59 (what.is.the.host)
onthepk pts/4 2011-02-22 08:00 (is.this.the.host)
anita pts/6 2011-02-22 02:35 (what.is.the.host)
$ who|awk -v d=`date +"%Y-%m-%d"` -v h=`date +"%H"` -v m=`date +"%M"` '{ split($4,wa,":"); wc=wa[1]*60+wa[2]; dc=h*60+m; if ($3==d && dc-wc<=120 ) printf ("%s\n",$0) }'
sanjeev pts/0 2011-02-22 07:54 (what.is.the.host)
madrose pts/3 2011-02-22 07:59 (what.is.the.host)
onthepk pts/4 2011-02-22 08:00 (is.this.the.host)
Monday, February 21, 2011
Occurrences of words in file
Here I am trying to split the words separately on basis of non-aplhanumeric chars or dot, they are replaced by space, and xargs trims more than one spaces or enterkey to one normal space char.
You can include the chars which may be part of your word in the sed ignore block.
cat filename | sed -e 's/[^a-zA-Z0-9.]/ /g' | xargs | tr ' ' '\n' | sort | uniq -c | sort -nr -k1,2
voila you should get the most occured word on top.
This is output for the above text.
4 the
2 xargs
2 word
2 to
2 space
2 sort
2 sed
2 or
2 one
2 on
2 of
2 chars
1 zA
1 your
1 you
1 words
1 which
1 voila
1 uniq
1 trying
1 trims
1 tr
1 top.
1 they
1 than
1 split
1 spaces
1 should
1 separately
1 s
1 replaced
1 part
1 occured
1 nr
1 normal
1 non
1 n
1 most
1 more
1 may
1 include
1 in
1 ignore
1 get
1 g
1 filename
1 enterkey
1 e
1 dot
1 char.
1 cat
1 can
1 c
1 by
1 block.
1 be
1 basis
1 are
1 aplhanumeric
1 and
1 am
1 a
1 Z0
1 You
1 I
1 Here
1 9.
You can include the chars which may be part of your word in the sed ignore block.
cat filename | sed -e 's/[^a-zA-Z0-9.]/ /g' | xargs | tr ' ' '\n' | sort | uniq -c | sort -nr -k1,2
voila you should get the most occured word on top.
This is output for the above text.
4 the
2 xargs
2 word
2 to
2 space
2 sort
2 sed
2 or
2 one
2 on
2 of
2 chars
1 zA
1 your
1 you
1 words
1 which
1 voila
1 uniq
1 trying
1 trims
1 tr
1 top.
1 they
1 than
1 split
1 spaces
1 should
1 separately
1 s
1 replaced
1 part
1 occured
1 nr
1 normal
1 non
1 n
1 most
1 more
1 may
1 include
1 in
1 ignore
1 get
1 g
1 filename
1 enterkey
1 e
1 dot
1 char.
1 cat
1 can
1 c
1 by
1 block.
1 be
1 basis
1 are
1 aplhanumeric
1 and
1 am
1 a
1 Z0
1 You
1 I
1 Here
1 9.
Subscribe to:
Posts (Atom)