How would you print just the 25th line in a file (smallest possible script please)?
Want to delete the record if I find a word (dd)in the fixed length(5-7).
Want to delete the record if I find a word (dd)in the length(5-7). Lets think that (5-7) length is ff2. this is a fixed width file. ex f1 ff1 ff2 ff3 sds dd fd sd ss ew dd dd se o/p should be like ff1 ff2 ff3 sd ss ew
How would you get the character positions 10-20 from a text file?
Answered by: Suman Saha
Answered On : Jun 27th, 2005cat filename.txt | cut -c 10-20
cat file_name | cut -c 10-20
head -20
How to print nth column of a pattern/file without using awk,cut commands?
cat file_name | awk {print $n} # where N is the column no. and tab is field seperator
cat SO_02052012_2.csv | awk -F; {print $n} # where N is is column no. and semicolon is field seperator
Shell script to add file extension
Write a small shell script that adds an extension ".New" to all the files in a directory.
path=${1?pls provide the path to script}
cd $path
for i in *
do
mv $i ${i}.new
if [ $? -eq 0 ]
then
echo "file rename successful"
ls -l ${i}*
fi
done
for file in /dxusers/testing/test_delete/*
do
if [ -f $file ]
then
mv $file $file.new
fi
done
How do you read arguments in a shell program - $1, $2 ?
Positional parameters are reads either by displaying there value or can be assigned to another variables.
echo $1 $2 $9 ${10} ${20}
shift operators can be used to move around the positional parameters.
example: test.sh 1 2 3 4 5
a=$1
while [ -z "$a" ]
do
echo $a
shift
done
echo $1
echo $2
$# : Stands for counts of the number of the positional parameters passed to a script.
example test.sh 1 2 3 4 5 6 7 , there are 7 paramaters as passed so the count of the $# is 7
By using $# parameter, the program checks whether the right number of arguments have been entered.
$? returns the status of the last command executed . if the last command is successfull then it will 0 , otherwise it will retrun non-zero. ex: date ; echo $? or try this dte ; echo $?
UNIX commands (well the "well behaved" ones) give a return code between 0 and 127 (inclusive). General convention is that a return code of 0 (zero) indicates success and that a non-zero return code i...
How will you list only the empty lines in a file (using grep)?
grep "^$" filename
grep -c "^$" file name
here -c option count the no.of empty line..the anchors denotes the empty line from your file name
How to extract the second row of a text-file?
awk NR==
cut -d -f2
How would you replace the n character in a file with some xyz?
:1,$s/n/xyz/g
All the sed replacements are by default global. It's in the vi editor that you have to specifically give "g" for global replacement.
How to find how many users have logged in and logged out in last five minutes using shell scripts?
How many users have logged in and logged out in last five or 10 minutes
Current_time=`date +%H%M`
who -u | awk $Current_time - $4 <= 5 { print }
last -5|grep -i "logged in"|wc -l
last -5|grep -v "logged out"|wc -l
How do you invoke and send mail from UNIX ?
Code
mailx -s "<subject>" <email_id list> < <input_file>
The following is a more detailed example for using mailx in shell script. This should help.
Code
SUBJECT="Email Subject Line" TO=xyz@company.com CC=`tr ' ' ', ' < mailCClist.txt` #BCC="xyz2@comp.com" echo "Send the E-mail message..." /usr/bin/mailx -s "${SUBJECT}" ${TO} <<-END ~< ! uuencode $DCdir/$FileName "Result.txt" ~c ${CC} ~b ${BCC} Hi, Blah, Blah.. body of the email. Thanks Satya ~. END echo "Mail Sent!!"
Hello,i am trying to write a UNIX shell script program to send mail to cc and to list.I am using the '-c' option for mailx UNIX command,but am getting an error illegal -c option. Could any one please guide me to use the mailx command in UNIX shell scripting to send the mail to the cc and to list along...
Code
uuencode attachment1 attachment1 | mailx -c x@yz.com,a@bc.com -s "subject" m@no.com
You should be able to use something like the following example to get the CC option working with mailx. Preferably create a list of users needed to be put in the CC option (as mailCClist.txt in the ex...
How will you write a shell script to connect to SQL database?
in vi editor
vi connect.sh
#!/bin/bash
sqlplus -s sys as sysdba <
spool on;
spool ashu;
connect scott/tiger
select * from emp;
spool off;
exit;
EOF
cat ashu
sqlplus -s username/password@schema name
:wq
How to read the contents of fie inside jar without extracting in shell script?
You can read the contents of file inside jar without extracting in shell script by using:
tar -tvf file.tar
jar tvf jarName.jar
Using KSH while loop#!/bin/ksh
#Get the count of total number of arguments passed while executing this script
int i = $#; #total number of arguments passed#
names=$@; #list of actual arguments passed#
while [ ${i} -gt 0 ]
do
echo '${names[i]}';
i=`expr i - 1`;
done
exit 0;
$ cat rev2
i=$#
echo "$@" > file_1
while [ $i -gt 0 ]
do
cut -f $i -d" " file_1 >> file_2
i=`echo $i-1|bc`
done
echo `cat file_2`;
rm -f file_1 file_2;
$ sh rev2 A B C D E
E D C B A
If you have a string "one two three", which shell command would you use to extract the strings?
Try this:
echo "one two three" | xargs
the output is: one two three
What are the different security mechanisms available in UNIX?
a) vi /etc/default/login
comment the line of /dev/console
b)vi .rhosts
enter the hostname by which the only user can connect with your system....bt no one else.....
c) disable the ftp for the selected users in /etc/ftpusers file ...
Unix is having 3 ways of Security Mechanism:1. By granting or revoking File permissions. Owner or Admin can change permissions to be given to group or others by using chmod command in Unix.2. Login is...
In shell scripting how to indentify that the previous command was run successfully?
in 2 ways it can be done:
1st way
$ echo $?
2nd way:
$ pwd 2> error| cat error|wc -l
0
$ cat h 2> error| cat error|wc -l
1
pwd or cat or any command if outpt is 0 then successful else failed.
in bash:
cat $filename | awk {print $25 }
a simple perl one liner to print only the 25th line