Useful Unix Commands with Example
1. Check
6th Columns without Null value from any file
nawk -F"|" '($6!="")' FileName > test_24_ok
2. Check
6th Columns with Null value from any file
nawk -F"|"
'($6=="")' FileName > test_24
3. Finding
distinct row's from any file
awk -F"|" '{print
$32 }' File_Name.txt | sort -u
4. For
creating direcory from another location
mkdir -p
/u05/PRODDSS/data3/srcfiles/mcd
#
Send mail through Unix
/usr/bin/mailx
-s "Mount Point status" -r EmergencyMail
devendrakumar.yadav@relianceada.com /Path/diskutilization.txt
/usr/bin/mailx –s “Subject Line” –r “Sender
Mail ID/XYZ” Receipient Mail Id Attachment
5. For
going 37072948 no’s rows in a file then
awk NR==37072948 S_VLD_COMPLETE_DATA_2008-10-23.dat
awk NR==37072948 S_VLD_COMPLETE_DATA_2008-10-23.dat
6. For
File spliting in Unix
split -37072945 S_VLD_COMPLETE_DATA_2008-10-23.dat
split -37072945 S_VLD_COMPLETE_DATA_2008-10-23.dat
(automatically file splitted into total rows/given row no)
7. For
deleting Particular record from a large file
where first column No's is '9999705895'
cat merge_R_VOI_27_2008-10-27.dat|grep -v 9999705895 > test.txt
cat merge_R_VOI_27_2008-10-27.dat|grep -v 9999705895 > test.txt
8.
Sagrigate Substrings
awk -f"|" '{print
substr($1,1,4)"-"substr($1,5,2)}' test.txt |more
9. Display
1,2,3,18,19 columns from that file
awk -F"," '{print $1,$2,$3,$18,$19 }' File_name | head -20
awk -F"," '{print $1,$2,$3,$18,$19 }' File_name | head -20
10. Cut a
string from a file and display without spacing
awk 'BEGIN {OFS="|"}{print $1,$2,$3,substr($12,1,4)"-"substr($12,5,2)}' file_name
awk -F"n" '{ print substr($1,1,4) }' file_name
awk 'BEGIN {OFS="|"}{print $1,$2,$3,substr($12,1,4)"-"substr($12,5,2)}' file_name
awk -F"n" '{ print substr($1,1,4) }' file_name
11.
Display particular column($1 for 1st column) from given file
awk -F"," '{print $1}' FileName
awk -F"," '{print $1}' FileName
12. Command
For search a file in unix
find /u04/ -name File_name -print
find /u04/ -name File_name -print
13. To
print Number of Columns
awk 'BEGIN{FS="|"}{print NF}' File_Name | head -3
awk 'BEGIN{FS="|"}{print NF}' File_Name | head -3
14. Print
columns which is less or greater than 500
nawk 'BEGIN{FS=OFS="|"}{if(NF > 500 || NF < 500){print $0}}' File_Name
nawk 'BEGIN{FS=OFS="|"}{if(NF > 500 || NF < 500){print $0}}' File_Name
Devendra1.dat File is having onlyNF=7 Record's
ð nawk 'BEGIN{FS=OFS="|"}{if(NF==7){print $0}}' Devendra.dat
Testing a file with first colum having "200903"
ðawk
-F"|" '{if($1=="200903")}{print $0}' S_04-01.dat
15.For
serch and replace in VI Editor
%s/old/new/g
%s/20081215/20081115/g
%s/20081215/20081115/g
16.In Fix
Width File Search or cut
awk -F"n" '{ print
substr($1,124,8)}' File name
( $1 means whole string, 124th
position upto + 8th position display)
17.For
Updating Crontab using VI Editor
in Command line type
EDITOR=vi then Enter then
export EDITOR Then Type
Crontab –e
18.For
concatinate two files same columns in another file
paste file1 file2 > file3
19.For
changing all albhabets in Capital letter from any file
tr -s '[a-z]' '[A-Z]' < File_Name
20. ping
to server in unix
/usr/sbin/ping 97.253.32.155
21.Take
count from any file having '2009-02' records
awk -F"," '{print $5}'
R_SMS_CDR_2009-04-02.dat| grep '2009-02' | wc -l
22.compare
any substring from a file
nawk
'BEGIN{FS=","}{if(substr($3,1,7)=="2009-03"){print $0}}'
test.txt
23.for
copying whole directories
tar -cvf ScriptBKP.tar
/u06/srcfiles/scriptbackup/*
tar -xvf ScriptBKP.tar
24.Insert
'2' in all record's of end of every row in file DEV_TEST.TXT
sed 's/$/|2/' DEV_TEST.TXT
25. For
Executing Command Continuosly or looping
while true ; do smat -d | wc
-l ; sleep 2 ; echo "==================="; done ;
while true ; do ls -lrt | wc -l
; sleep 2 ; echo "====================="; done ;
26.
Display time also in unix prompt
PS1="\u-\t$"
27.Print
Particular (2nd) line from any file
sed -n '2,2p;2q'
R_2009-07-17_03_03.dat
sed -n '5,5p;5q'
R_2009-08-28_00_00.dat | awk -F"," '{print $51}'
28.Convert
'2009-03-31' in 20090331
MDT=`echo $RDT|cut
-c1-4,6-7,9-10` ; export MDT
29.
Current Month Calculation ==>
MON1=`echo $RecordDate|cut -c6-7`
Year=`echo $RecordDate|cut -c1-4`
cal $MON1 $Year|awk NR==1|cut -c4-6
> CurrMonth
MON=`tr '[a-z]' '[A-Z]' <
CurrMonth`
30.
Connect any server through script and ftp the files
echo "Connecting
97.253.48.8"
ftp -n 97.253.48.8 <<!
quote user devendra1
quote pass devendraPwd
binary
cd /app_inrich/OUTPUT_INTERFACE/call_hist/dump/
lcd /u04/PisPath/pisfiles
prompt
mget DSS_INRich_subscriber_info$RD.txt
quit
31. Output
Of Date Command in unix
operator-17:28:11$
date
Thu Nov 19 17:35:46 IST 2009
32. Output Of Cal Command in unix
operator-17:35:47$ cal
November 2009
S M
Tu W Th
F S
1
2 3 4
5 6 7
8 9 10
11 12 13 14
15 16 17 18 19 20 21
22 23 24 25 26 27 28
29 30
33. Print some particular columns from any file
operator-17:42:12$ls -ltr *NOV*2009-11-17*dat
-rw-rw-r-- 1 operator adhoc 554 Nov 17 18:00 R_VOI_2009-11-17_14_14.dat
operator-17:42:19$ls -ltr *NOV*2009-11-17*dat |awk '{print
$1,"|",$2,"|",$3,"VMA"}'
-rw-rw-r-- | 1 | operator VMA
ls -ltr *JAN*2009-02-27*dat|awk '{print
$1,"|",$2,"|",$3,"VMA"}'
34. Remove
Empty Lines from any Files.
sed
'/^$/d'
/tmp/myFile.txt > /tmp/Output.txt
Nice Article
ReplyDelete