Featured Post

15 Python Tips : How to Write Code Effectively

Image
 Here are some Python tips to keep in mind that will help you write clean, efficient, and bug-free code.     Python Tips for Effective Coding 1. Code Readability and PEP 8  Always aim for clean and readable code by following PEP 8 guidelines.  Use meaningful variable names, avoid excessively long lines (stick to 79 characters), and organize imports properly. 2. Use List Comprehensions List comprehensions are concise and often faster than regular for-loops. Example: squares = [x**2 for x in range(10)] instead of creating an empty list and appending each square value. 3. Take Advantage of Python’s Built-in Libraries  Libraries like itertools, collections, math, and datetime provide powerful functions and data structures that can simplify your code.   For example, collections.Counter can quickly count elements in a list, and itertools.chain can flatten nested lists. 4. Use enumerate Instead of Range     When you need both the index ...

19 Top Unix File Scenario Commands

ETL developers main task is to browse various flat files before they start testing. File browsing in UNIX is tricky. If you know right command to do it you can save a lot of time. These 19 top UNIX files commands useful to use in your project.


In UNIX a file normally can have Header, Detail and Trailer. There are scenarios where you need only details without header and Trailer, and need only recent one record, and you need to skip some records from the input files.

So for all the File based scenarios, I have given useful UNIX commands.



19 tricky Unix File Commands

 

1). How to print/display the first line of a file? 

There are many ways to do this. However the easiest way to display the first line of a file is using the [head] command. 
$> head -1 file. Txt

If you specify [head -2] then it would print first 2 records of the file. 
Another way can be by using [sed] command. [sed] is a very powerful text editor which can be used for various text manipulation purposes like this. 

$> sed '2,$ d' file. Txt

2). How does the above command work? 

The 'd' parameter basically tells [sed] to delete all the records from display from line 2 to last line of the file (last line is represented by $ symbol). 

Of course it does not actually delete those lines from the file, it just does not display those lines in standard output screen. So you only see the remaining line which is the 1st line. 

3). How to print/display the last line of a file? 

The easiest way is to use the [tail] command. 
$> tail -1 file. Txt
if you want to do it using [sed] command, here is what you should write: 
$> sed -n '$ p' test
from our previous answer, we already know that '$' stands for the last line of the file. So '$ p' basically prints (p for print) the last line in standard output screen. '-n' switch takes [sed] to silent mode so that [sed] does not print anything else in the output. 

4). How to display n-th line of a file? 

the easiest way to do it will be by using [sed] i guess. Based on what we already know about [sed] from our previous examples, we can quickly deduce this command: 
$> sed –n ' p' file. Txt
you need to replace with the actual line number. So if you want to print the 4th line, the command will be 
$> sed –n '4 p' test
of course you can do it by using [head] and [tail] command as well like below: 
$> head - file. Txt | tail -1
you need to replace with the actual line number. So if you want to print the 4th line, the command will be 
$> head -4 file. Txt | tail -1

5). How to remove the first line / header from a file? 

we already know how [sed] can be used to delete a certain line from the output – by using the'd' switch. So if we want to delete the first line the command should be: 
$> sed '1 d' file. Txt
but the issue with the above command is, it just prints out all the lines except the first line of the file on the standard output. It does not really change the file in-place. So if you want to delete the first line from the file itself, you have two options. 
either you can redirect the output of the file to some other file and then rename it back to original file like below: 
$> sed '1 d' file. Txt > new_file. Txt
$> mv new_file. Txt file. Txt
or, you can use an inbuilt [sed] switch '–i' which changes the file in-place. See below: 
$> sed –i '1 d' file. Txt

6). How to remove the last line/ trailer from a file in unix script? 

always remember that [sed] switch '$' refers to the last line. So using this knowledge we can deduce the below command: 
$> sed –i '$ d' file. Txt
7). How to remove certain lines from a file in unix? 

if you want to remove line to line from a given file, you can accomplish the task in the similar method shown above. Here is an example: 
$> sed –i '5,7 d' file. Txt
the above command will delete line 5 to line 7 from the file file. Txt 
8). How to remove the last n-th line from a file? 

this is bit tricky. Suppose your file contains 100 lines and you want to remove the last 5 lines. Now if you know how many lines are there in the file, then you can simply use the above shown method and can remove all the lines from 96 to 100 like below: 
$> sed –i '96,100 d' file. Txt   # alternative to command [head -95 file. Txt] 
but not always you will know the number of lines present in the file (the file may be generated dynamically, etc. ) in that case there are many different ways to solve the problem. There are some ways which are quite complex and fancy. But let's first do it in a way that we can understand easily and remember easily. Here is how it goes: 
$> tt=`wc -l file. Txt | cut -f1 -d' '`;sed –i "`expr $tt - 4`,$tt d" test
as you can see there are two commands. The first one (before the semi-colon) calculates the total number of lines present in the file and stores it in a variable called “tt”. The second command (after the semi-colon), uses the variable and works in the exact way as shows in the previous example. 

9). How to check the length of any line in a file? 

we already know how to print one line from a file which is this: 

$> sed –n ' p' file. Txt

where is to be replaced by the actual line number that you want to print. Now once you know it, it is easy to print out the length of this line by using [wc] command with '-c' switch. 

$> sed –n '35 p' file. Txt | wc –c

The above command will print the length of 35th line in the file. Txt. 
10). How to get the nth word of a line in unix? 

assuming the words in the line are separated by space, we can use the [cut] command. [cut] is a very powerful and useful command and it's real easy. All you have to do to get the n-th word from the line is issue the following command: 
cut –f -d' '

'-d' switch tells [cut] about what is the delimiter (or separator) in the file, which is space ' ' in this case. If the separator was comma, we could have written -d',' then. So, suppose i want find the 4th word from the below string: “a quick brown fox jumped over the lazy cat”, we will do something like this: 
$> echo “a quick brown fox jumped over the lazy cat” | cut –f4 –d' '
and it will print “fox” 

11). How to reverse a string in unix? 

pretty easy. Use the [rev] command. 
$> echo "unix" | rev
xinu
12). How to get the last word from a line in unix file? 

we will make use of two commands that we learnt above to solve this. The commands are [rev] and [cut]. Here we go. 

let's imagine the line is: “c for cat”. We need “cat”. First we reverse the line. We get “tac rof c”. Then we cut the first word, we get 'tac'. And then we reverse it again. 

$>echo "c for cat" | rev | cut -f1 -d' ' | rev

cat

13). How to get the n-th field from a unix command output? 

we know we can do it by [cut]. Like below command extracts the first field from the output of [wc –c] command 
$>wc -c file. Txt | cut -d' ' -f1
109
but i want to introduce one more command to do this here. That is by using [awk] command. [awk] is a very powerful command for text pattern scanning and processing. Here we will see how may we use of [awk] to extract the first field (or first column) from the output of another command. Like above suppose i want to print the first column of the [wc –c] output. Here is how it goes like this: 
$>wc -c file. Txt | awk ' ''{print $1}'
109 
the basic syntax of [awk] is like this: 
awk 'pattern space''{action space}'
the pattern space can be left blank or omitted, like below: 
$>wc -c file. Txt | awk '{print $1}'
109
in the action space, we have asked [awk] to take the action of printing the first column ($1). More on [awk] later. 
14). How to replace the n-th line in a file with a new line in unix? 
this can be done in two steps. The first step is to remove the n-th line. And the second step is to insert a new line in n-th line position. Here we go. 
step 1: remove the n-th line 
$>sed -i'' '10 d' file. Txt       # d stands for delete
step 2: insert a new line at n-th line position 
$>sed -i'' '10 i this is the new line' file. Txt     # i stands for insert

15). how to show the non-printable characters in a file? 
open the file in vi editor. Go to vi command mode by pressing [escape] and then [:]. Then type [set list]. This will show you all the non-printable characters, e. G. Ctrl-m characters (^m) etc. , in the file. 

16). How to zip a file in linux? 

Use inbuilt [zip] command in linux 

17). How to unzip a file in linux? 

use inbuilt [unzip] command in linux. 
$> unzip –j file. Zip

18). How to test if a zip file is corrupted in linux? 

Use “-t” switch with the inbuilt [unzip] command 
$> unzip –t file. Zip

19). How to check if a file is zipped in unix? 

In order to know the file type of a particular file use the [file] command like below: 
$> file file. Txt
file. Txt: ascii text
if you want to know the technical mime type of the file, use “-i” switch. 
$> file -i file. Txt
file. Txt: text/plain; charset=us-ascii
if the file is zipped, following will be the result 
$> file –i file. Zip
file. Zip: application/x-zip

Comments

Popular posts from this blog

How to Fix datetime Import Error in Python Quickly

SQL Query: 3 Methods for Calculating Cumulative SUM

Big Data: Top Cloud Computing Interview Questions (1 of 4)