Linux Unix help !!

"Give respect to Time, One day at right Time, Time will respect You"

Tuesday, January 4, 2011

scripting sort hand


## Count the occurrences of a specific word in a .txt file in bash shell.
# tr -s ' ' '\n' < myfile.txt | grep -c 'searchword'
# cat mydr/myfiles-1-* |grep -c "searchtest"

## Find Days in months(eg apr:30 days)

# tmnth=$(cal 4 2010 | egrep -v '[A-Za-z]' | wc -w)  

## gunzip & untar in 1 cmd
# cat May-2010.tar.gz | gunzip -d | tar -xvf -               
##
# gzcat test123.tar.gz | tar -xvf -
##
# tar -zcvf   May-2010.tar.gz


## Check is your variable is numeric or not?
# cat  isnum.sh
#!/bin/bash
# Script to test variable is numeric or not
# Shirish Shukla
# Pass arg1 as number
a1=$1
a=$(echo "$a1" |awk '{if($1 > 0) print $1; else print $1"*-1"}'| bc)
b=$(echo "scale=2;$a2/$a2 + 1" | bc -l 2>/dev/null)
if [[ $b > 1 ]]
then
    echo "$a1 is Numeric"
else
    echo "$a1 is Non Numeric"
fi

## Output
# sh isnum.sh 12
12 is Non Numeric

# sh isnum.sh 12-5+4
12-5+4 is Non Numeric

# sh isnum.sh abc
abc is Non Numeric

# sh isnum.sh shirish-shukla
shirish-shukla is Non Numeric

26 comments:

  1. ## Multiple SED
    sed -e "s/['}',{']//g"

    ## Disable bell
    set bell-style none

    -- Shirish Shukla

    ReplyDelete
  2. ### read multiple file at a time
    exec 7<$1
    exec 8<$2
    #for [[ read ln1 <&7 && read ln2 <&8 ]]
    while read ln1 <&7 && read ln2 <&8
    #`cat $2`
    #while IFS=" " read ln1 <&`cat $1`
    do
    echo $ln1
    echo $ln2
    done
    -- Shirish Shukla

    ReplyDelete
  3. i want to learn linux scripting....can u suggest any of the free ebooks available in internet.....

    ReplyDelete
  4. ### User running process and load on your server one liner

    # ps -eo %u | sort |uniq -c |sort -rn;uptime |awk -F"user," '{print $2}'

    --Shirish Shukla

    ReplyDelete
  5. #### replace ! symbols using sed

    # sed -i "s/\!1\!/\!11\!/g" source-fle

    # sed -i "s/\!2\!/\!12\!/g" * ## for all files inside dir

    -- Shirish Shukla

    ReplyDelete
  6. This comment has been removed by a blog administrator.

    ReplyDelete
  7. #### Batch script to generate win sys info

    echo List of softwares > software_list.txt
    echo ================= >>software_list.txt
    reg export HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall temp1.txt
    find "DisplayName" temp1.txt| find /V "ParentDisplayName" > temp2.txt
    for /f "tokens=2,3 delims==" %%a in (temp2.txt) do (echo %%a >> software_list.txt)
    del temp1.txt temp2.txt

    ReplyDelete
  8. ## Remove spaces
    echo "Shirish Shukla" | sed "s/[ \t][ \t]*//g"

    --Shirish Shukla

    ReplyDelete
  9. vi tips delete first 3 chars from each line ..

    :%s/^.\{3}//g


    --Shirish Shukla

    ReplyDelete
    Replies
    1. #### WITH sed

      [root@SKS Shirish]# echo "123 shirish" > a
      [root@SKS Shirish]# cat a
      123 shirish
      [root@SKS Shirish]# sed -i 's/^.\{2\}//g' a
      [root@SKS Shirish]# cat a
      3 shirish
      [root@SKS Shirish]#

      --SHIRISH SHUKLA

      Delete
  10. [root@SKS Shirish]# echo "123 shirish" > a
    [root@SKS Shirish]# cat a
    123 shirish
    [root@SKS Shirish]# sed -i 's/^.\{2\}//g' a
    [root@SKS Shirish]# cat a
    3 shirish
    [root@SKS Shirish]#

    --SHIRISH SHUKLA

    ReplyDelete
  11. ### 10.3 hours before time
    perl -e '$x=localtime(time-(10.5*3600));print $x'
    ### 5 hours after time
    perl -e '$x=localtime(time+(5*3600));print $x'

    --Shirish shukla

    ReplyDelete
  12. Splitting file based on column values
    # cat myfile.txt | awk -F"," '{gsub(/"/,""); if ($4 == "") print > "file4"; else if ($5 == "") print > "file5"; else print > "file"}'

    --Shirish Shukla

    ReplyDelete
  13. Convert IST to EST ...an fantastic sorthand script script ... in bash using awk ...

    echo "10:23:am" | awk -F":" '/PM|pm/ {print ($1+12)*60*60+$2*60} /AM|am/ {print $1*60*60+$2*60}' | awk '{print $1-37800}'|awk '{print strftime("%H:%M %p", $1,1) " EST"}'

    ReplyDelete
    Replies
    1. EG:

      TMIST="10:10:AM"

      # echo $TMIST | awk -F":" '/PM|pm/ {print ($1+12)*60*60+$2*60} /AM|am/ {print $1*60*60+$2*60}' | awk '{print $1-37800}'|awk '{print strftime("%H:%M %p", $1,1) " EST"}'

      23:40 PM EST

      --Shirish Shukla

      Delete
  14. Encrypt - Decrypt While tar ..

    ## Encrypt
    # tar cvzf - a.sh | openssl des3 -salt -k sks | dd of=enc-info

    ## Try
    # tar -ztvf enc-info

    ## Decrypt
    dd if=enc-info | openssl des3 -d -k sks | tar zvfz -


    -- Shirish Shukla

    ReplyDelete
  15. ### Sort hand Replace a char/word from a file

    eg:
    # cat test
    1 2 3 4 5 6 7 4 8 9 10 4

    1> Using awk to wrap line replace 4
    # awk '{ gsub(/4[ ]|4/,"\n"); print }' test
    1 2 3
    5 6 7
    8 9 10

    2> Using vi
    %s/4/\r/g

    3> Using sed
    sed -i "s/4/\n/g" test

    --Shirish Shukla

    ReplyDelete
  16. ## If consider with and without blank space after 4 use below for that .

    # %s/4 \|4/\r/g

    --Shirish Shukla

    ReplyDelete
  17. ###Delete all white spaces lines etccc..
    sed -i 's/^[ \t]*//;s/[ \t]*$//'
    sed -i 's/[ \t]*$//'


    --Shirish Shukla

    ReplyDelete
  18. ## Delete blank lines within vi?
    # :g/^$/d
    # :g/^ *$/d

    ReplyDelete
    Replies
    1. ### With Awk
      # awk 'NF' file1 > file2

      ### SED
      # sed -e 's/[ ^I]*$//' -e '/^$/ d'

      --Shirish Shukla

      Delete
  19. ## Find large files updated since 3 days
    find / -xdev -type f -size +10000000c -mtime -3 -exec ls -lad {} \; |awk '{print $5/1024 "\t\t\t" $9}'

    ReplyDelete
  20. ### Last 5 days updated file with least 10MB size
    # find /var/ -xdev -type f -size +10000000c -mtime -5 -exec ls -lad {} \; |awk '{print $5/2048 "\t\t\t" $9}'

    ReplyDelete
  21. ## Find file newer than file Ita
    # find . -newer a | xargs du -sk {} | sort -n

    ReplyDelete
  22. ## List all dir having name starting with digits ...
    # ll
    total 0
    -rw-r--r-- 1 root root 0 May 17 17:24 1
    -rw-r--r-- 1 root root 0 May 17 17:24 1a
    -rw-r--r-- 1 root root 0 May 17 17:24 a2

    # ls -ld [[:digit:]]*
    -rw-r--r-- 1 root root 0 May 17 17:24 1
    -rw-r--r-- 1 root root 0 May 17 17:24 1a

    --Shirish

    ReplyDelete
  23. ## List files having name start with char s or p usign awk
    # ll -l | awk '{print $NF}' | awk '/^(s|p).*/ {print $1}'

    ## List files having name first char "s" and 7th char "h" i.e :" shirish*
    # ll -l | awk '{print $NF}' | awk '/^(s.....h).*/ {print $1}'


    --Shirish

    ReplyDelete

Write Here .. your comments are always wellcome ..but no spam please !!

Followers

Pls LIKE my Story !!!