A highly motivated and ambitious individual able to give timely and accurate advice, guidance, support and training to team members and individuals. Having the ability to work with the minimum of supervision whilst leading a team. Having a proven ability to lead by example, consistently hit targets, improves best practices and organizes time efficiently.
Showing posts with label UNIX. Show all posts
Showing posts with label UNIX. Show all posts
Saturday, 6 February 2016
Wednesday, 2 September 2015
Unix Script to generate files based on unique values of a field
Below file has different mobile companies. I need to generate the files dynamically based on the unique values of the field.I can have blank values also.But I should not generate files for those.
Input:
Generate the Unique values :
Script to generate files based on the unique values. Here -z in the if condition represents the check for empty variable.
Output:
Input:
Generate the Unique values :
Script to generate files based on the unique values. Here -z in the if condition represents the check for empty variable.
Output:
Tuesday, 1 September 2015
Sunday, 2 August 2015
Unix command to calculate the maximum length of a column in a file
I have a scenario where I have to find the maximum length of the data in a column in file.
Here, the column i need to find is for 12th column.
awk -F'|' 'NR>1{for (i=12; i<=12; i++) max[i]=(length($i)>max[i]?length($i):max[i])} END {for (i=12; i<=12; i++) printf "%d%s", max[i], (i==12?RS:FS)}' BETA.PAD.AUDIT.DAT
Here, the column i need to find is for 12th column.
awk -F'|' 'NR>1{for (i=12; i<=12; i++) max[i]=(length($i)>max[i]?length($i):max[i])} END {for (i=12; i<=12; i++) printf "%d%s", max[i], (i==12?RS:FS)}' BETA.PAD.AUDIT.DAT
Find the maximum length of a line in a Unix file
awk ' { if ( length > L ) { L=length} }END{ print L}' <Filename>
Saturday, 1 August 2015
Find the bad record in a file using Sed and AWK command
I have a file which has 23 million records loading into a stage table using Informatica.
Suddenly there is a failure due to bad record in between loading.Our file delimiter is " | " pipe.
There is an extra pipe in between the fields. My file has 11 fields.
sed -n '14409589,2300000p' <filename>| awk -F "|" 'NF != 11'
returns the rows that have more than 11 columns.
Tuesday, 28 July 2015
XML indentation in Unix
I have an XML file with no proper indentation.It has to be changed as XML tags indented.
Input:My input file looks like
<a><b>c</b></a>
Output:
$ xmllint --format <myfilename>
<a>
<b>c</b>
</a>
Friday, 17 July 2015
Check permissions in Unix while executing a script
$ cat check-permissions.sh
#!/bin/bash
file=$1
# Handle non-absolute paths
if ! [[ "$file" == /* ]] ; then
path=.
fi
dirname "$file" | tr '/' $'\n' | while read part ; do
path="$path/$part"
# Check for execute permissions
if ! [[ -x "$path" ]] ; then
echo "'$path' is blocking access."
fi
done
if ! [[ -r "$file" ]] ; then
echo "'$file' is not readable."
fi
$ ./check-permissions.sh /long/path/to/file.txt
Sunday, 14 June 2015
Send Mail From Shell Script
Here we will see simple bash script to send emails using the mail command in linux operating system.
#!/bin/bash TO_ADDRESS="recipient@domain.com" FROM_ADDRESS="sender" SUBJECT="Mail Server Hosting Demo" BODY="This is a linux mail system. Linux is one of the email operating systems which can be used to send and receive emails." echo ${BODY}| mail -s ${SUBJECT} ${TO_ADDRESS} -- -r ${FROM_ADDRESS}
We will enhance the above script to attach files, to read body from a file and specifying a list of users in CC. The enhanced mail script is shown below:
#!/bin/bash TO_ADDRESS="recipient@domain.com" FROM_ADDRESS="sender" SUBJECT="linux mail send attachment example" BODY_FILE="script.dat" ATTACHMENT_FILE="logfile.txt" CC_LIST="user1@gmail.com;user2@yahoomail.com;user3@earthlink.com;user4@cheetahmail.com" uuencode ${ATTACHMENT_FILE} | mail -s ${SUBJECT} -c ${CC_LIST} ${TO_ADDRESS} -- -r ${FROM_ADDRESS} < ${BODY_FILE}
Tar Command
The syntax of tar command is
The options of tar command are:
tar [options] [Archive file] [files list]
The options of tar command are:
c : creates a tar file. v : verbose. Displays the files information. f : Specify the tar file name. r : updates the tar file with new files. x : Extracts files from the archive (tar file). t : view contents of tar file. z : Specify the tar command to create a tar file using gzip in unix. j : uses bzip2 to create the tar file.
1. Creating a tar file
Let see a sample example by archiving all the files in my current directory. The ls -l command displays the files and directories in the current directory.
We see how to tar all these files using the -c option with the tar command. This is shown below:
Let see a sample example by archiving all the files in my current directory. The ls -l command displays the files and directories in the current directory.
> ls -l drwxr-xr-x 2 user group 4096 Aug 8 03:23 debian -rw-r--r-- 1 user group 174 Aug 2 23:39 file -rw-r--r-- 1 user group 0 Aug 8 03:22 linux_server.bat -rw-r--r-- 1 user group 76 Aug 2 02:21 test.sh -rw-r--r-- 1 user group 0 Aug 8 03:22 unix_distro
We see how to tar all these files using the -c option with the tar command. This is shown below:
> tar -cvf archive.tar * debian/ file linux_server.bat test.sh unix_distro > ls archive.tar debian file linux_server.bat test.sh unix_distro
2. Printing the contents of tar file
We have created the tar file and we dont know whether it contains the actual files or not. To view the contents of the tar file use the -t option as
We have created the tar file and we dont know whether it contains the actual files or not. To view the contents of the tar file use the -t option as
> tar -tvf archive.tar drwxr-xr-x user/group 0 2012-08-08 03:23:07 debian/ -rw-r--r-- user/group 174 2012-08-02 23:39:51 file -rw-r--r-- user/group 0 2012-08-08 03:22:19 linux_server.bat -rw-r--r-- user/group 76 2012-08-02 02:21:32 test.sh -rw-r--r-- user/group 0 2012-08-08 03:22:09 unix_distro
3. Updating the tar file with new contents.
You can add new files to the existing archive (tar) file using the -r option.
You can add new files to the existing archive (tar) file using the -r option.
>touch red-hat-linux.dat >tar -rvf archive.tar red-hat-linux.dat red-hat-linux.dat >tar -tvf archive.tar drwxr-xr-x pcenter/pcenter 0 2012-08-08 03:23:07 debian/ -rw-r--r-- pcenter/pcenter 174 2012-08-02 23:39:51 file -rw-r--r-- pcenter/pcenter 0 2012-08-08 03:22:19 linux_server.bat -rw-r--r-- pcenter/pcenter 76 2012-08-02 02:21:32 test.sh -rw-r--r-- pcenter/pcenter 0 2012-08-08 03:22:09 unix_distro -rw-r--r-- pcenter/pcenter 0 2012-08-08 04:00:00 red-hat-linux.dat
4. Extracting the contents of tar file
In the first example, we have created the archive file. Now we will see how to extract the set of files from the archive. To extract the contents of the tar file use the -x option.
In the first example, we have created the archive file. Now we will see how to extract the set of files from the archive. To extract the contents of the tar file use the -x option.
> tar -xvf archive.tar debian/ file linux_server.bat test.sh unix_distro
5. Creating compressed tar file
So far we have created a uncompressed tar file in the above examples. We can create a compressed tar file using the gzip
So far we have created a uncompressed tar file in the above examples. We can create a compressed tar file using the gzip
Compressing files using gzip > tar -zcvf new_tar_file.tar.gz *
Find Command
How to find for a file using name?
find -name "sum.txt" ./bkp/sum.txt ./sum.txt
-i can be used for case insensitive file names.
How to remove files which contain the name "java".
This will delete all the files which have the word “java" in the file name in the current directory and sub-directories.
find -name "*java*" -exec rm -r {} \;
This will delete all the files which have the word “java" in the file name in the current directory and sub-directories.
How to find for a file in the current directory only?
find -maxdepth 1 -name "sum.txt"
Sed Command
Sed is a Stream Editor used for modifying the files in unix (or linux). Whenever you want to make changes to the file automatically, sed comes in handy to do this. Most people never learn its power; they just simply use sed to replace text. You can do many things apart from replacing text with sed.
Consider the below text file as an input.
1. Replacing or substituting string
Sed command is mostly used to replace the text in a file. The below simple sed command replaces the word "unix" with "linux" in the file.
Here the "s" specifies the substitution operation. The "/" are delimiters. The "unix" is the search pattern and the "linux" is the replacement string.
By default, the sed command replaces the first occurrence of the pattern in each line and it won't replace the second, third...occurrence in the line.
2. Replacing the nth occurrence of a pattern in a line.
Use the /1, /2 etc flags to replace the first, second occurrence of a pattern in a line. The below command replaces the second occurrence of the word "unix" with "linux" in a line.
3. Replacing all the occurrence of the pattern in a line.
The substitute flag /g (global replacement) specifies the sed command to replace all the occurrences of the string in the line.
4. Replacing from nth occurrence to all occurrences in a line.
Use the combination of /1, /2 etc and /g to replace all the patterns from the nth occurrence of a pattern in a line. The following sed command replaces the third, fourth, fifth... "unix" word with "linux" word in a line.
Replacing string on a specific line number.
You can restrict the sed command to replace the string on a specific line number. An example is
courtesy:folkstalk
Consider the below text file as an input.
>cat file.txt unix is great os. unix is opensource. unix is free os. learn operating system. unixlinux which one you choose.
Sed Command Examples
1. Replacing or substituting string
Sed command is mostly used to replace the text in a file. The below simple sed command replaces the word "unix" with "linux" in the file.
>sed 's/unix/linux/' file.txt linux is great os. unix is opensource. unix is free os. learn operating system. linuxlinux which one you choose.
Here the "s" specifies the substitution operation. The "/" are delimiters. The "unix" is the search pattern and the "linux" is the replacement string.
By default, the sed command replaces the first occurrence of the pattern in each line and it won't replace the second, third...occurrence in the line.
2. Replacing the nth occurrence of a pattern in a line.
Use the /1, /2 etc flags to replace the first, second occurrence of a pattern in a line. The below command replaces the second occurrence of the word "unix" with "linux" in a line.
>sed 's/unix/linux/2' file.txt unix is great os. linux is opensource. unix is free os. learn operating system. unixlinux which one you choose.
3. Replacing all the occurrence of the pattern in a line.
The substitute flag /g (global replacement) specifies the sed command to replace all the occurrences of the string in the line.
>sed 's/unix/linux/g' file.txt linux is great os. linux is opensource. linux is free os. learn operating system. linuxlinux which one you choose.
4. Replacing from nth occurrence to all occurrences in a line.
Use the combination of /1, /2 etc and /g to replace all the patterns from the nth occurrence of a pattern in a line. The following sed command replaces the third, fourth, fifth... "unix" word with "linux" word in a line.
>sed 's/unix/linux/3g' file.txt unix is great os. unix is opensource. linux is free os. learn operating system. unixlinux which one you choose.
Replacing string on a specific line number.
You can restrict the sed command to replace the string on a specific line number. An example is
>sed '3 s/unix/linux/' file.txt unix is great os. unix is opensource. unix is free os. learn operating system. linuxlinux which one you choose.
Replacing string on a range of lines.
You can specify a range of line numbers to the sed command for replacing a string.
Here the sed command replaces the lines with range from 1 to 3.
You can specify a range of line numbers to the sed command for replacing a string.
>sed '1,3 s/unix/linux/' file.txt linux is great os. unix is opensource. unix is free os. learn operating system. linuxlinux which one you choose.
Here the sed command replaces the lines with range from 1 to 3.
Deleting lines.
You can delete the lines a file by specifying the line number or a range or numbers.
You can delete the lines a file by specifying the line number or a range or numbers.
>sed '2 d' file.txt >sed '5,$ d' file.txt
Subscribe to:
Posts (Atom)