Advanced Unix/Linux Commands

1)  Convert single column to single row:
Input: filename : try
REF_PERIOD
PERIOD_NAME
ACCOUNT_VALUE
CDR_CODE
PRODUCT
PROJECT
SEGMENT_CODE
PARTNER
ORIGIN
BILLING_ACCRUAL
Output:
REF_PERIOD PERIOD_NAME ACCOUNT_VALUE CDR_CODE PRODUCT PROJECT SEGMENT_CODE PARTNER ORIGIN BILLING_ACCRUAL
Command: cat try | awk ‘{printf “%s “,$1}’

2) Print the list of employees in Technology department :
Now department name is available as a fourth field, so need to check if $4 matches with the string “Technology”, if yes print the line.
Command: $ awk ‘$4 ~/Technology/’ employee.txt
200  Jason   Developer  Technology  $5,500
300  Sanjay  Sysadmin   Technology  $7,000
500  Randy   DBA        Technology  $6,000
Operator ~ is for comparing with the regular expressions. If it matches the default action i.e print whole line will be  performed.

3) Convert single column to multiple column :
For eg: Input file contain single column with 84 rows then output should be single column data converted to multiple of 12 columns i.e. 12 column * 7 rows with field separtor (fs ;)
Script:
#!/bin/sh
rows=`cat input_file | wc -l`
 cols=12
 fs=;
awk -v r=$rows -v c=$cols -v t=$fs '
 NR<r*c{printf("%s",NR%c?$0"$":$0"\n");next}{print}
 END{if(NR%c&&NR<r*c){print ""}}' input_file > output_file

4) Last field print:
input:
a=/Data/Files/201-2011.csv
output:
201-2011.csv
Command: echo $a | awk -F/ ‘{print $NF}’

5) Count no. of fields in file:
file1: a, b, c, d, 1, 2, man, fruit
Command: cat file1 | awk ‘BEGIN{FS=”,”};{print NF}’
and you will get the output as:8

6) Find ip address in unix server:
Command: grep -i your_hostname /etc/hosts

7) Replace the word corresponding to search pattern:
 >cat file
 the black cat was chased by the brown dog.
 the black cat was not chased by the brown dog.
 >sed -e '/not/s/black/white/g' file
 the black cat was chased by the brown dog.
 the white cat was not chased by the brown dog.

8) The below i have shown the demo for the “A” and “65″.
Ascii value of character: It can be done in 2 ways:
1. printf “%d” “‘A”
2. echo “A” | tr -d “\n” | od -An -t dC
Character value from Ascii:  awk -v char=65 ‘BEGIN { printf “%c\n”, char; exit }’

9) Input file:
crmplp1 cmis461 No Online
cmis462 No Offline
crmplp2 cmis462 No Online
cmis463 No Offline
crmplp3 cmis463 No Online
cmis461 No Offline
Output –>crmplp1 cmis461 No Online cmis462 No Offline
crmplp2 cmis462 No Online cmis463 No Offline
Command:
awk ‘NR%2?ORS=FS:ORS=RS’ file

10) Variable can used in AWK
awk -F”$c” -v var=”$c” ‘{print $1var$2}’ filename

11) Search pattern and use special character in sed command:
sed -e ‘/COMAttachJob/s#”)#.”:JobID)#g’ input_file

12) Get the content between two patterns:
sed -n ‘/CREATE TABLE table/,/MONITORING/p’ table_Script.sql

13) Pring debugging script output in log file Add following command in script:
exec 1>> logfilename
exec 2>>logfilename

14) Check Sql connection:#!/bin/sh
ID=abc
PASSWD=avd
DB=sdf
exit | sqlplus -s -l $ID/$PASSWD@$DB
echo variable:$?
exit | sqlplus -s -L avd/df@dfg > /dev/null
echo variable_crr: $?
15) Trim the spaces using sed command

echo “$var” | sed -e ‘s/^[[:space:]]*//’ -e ‘s/[[:space:]]*$//’
Another option is:
Code:
var=$(echo “$var” | sed -e ‘s/^[[:space:]]*//’ -e ‘s/[[:space:]]*$//’)
echo “Start $var End”

16) How to add sigle quote in statement using awk:Input:
/Admin/script.sh abc 2011/08                        29/02/2012 00:00:00
/Admin/script.sh abc 2011/08                        29/02/2012 00:00:00
command:
cat command.txt | sed -e ‘s/[[:space:]]/ /g’ | awk -F’ ‘ ‘{print \x27″$1,$2,$3″\x27″,”\x27″$4,$5″\x27″}’
output:
‘/Admin/script.sh abc 2011/08′ ’29/02/2012 00:00:00′
‘/Admin/script.sh abc 2011/08′ ’29/02/2012 00:00:00′

No comments:

Post a Comment

python

  import datetime import pandas as pd import cx_Oracle con = cx_Oracle.connect( 'SYSTEM/oracle123@localhost:1521/xe' ) c = con.c...