Unix Commandnfds 25
Unix Commandnfds 25
lines. The commands discussed here are particulary useful for the developers working in the middle-tier (e.g. ETL) systems, where they may need to interact with several *nx source systems for data retrieval.
$> sed n '4 p' test Of course you can do it by using [head] and [tail] command as well like below: $> head -<n> file.txt | tail -1 You need to replace <n> with the actual line number. So if you want to print the 4th line, the command will be $> head -4 file.txt | tail -1
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
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.
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
You will be using the same [sqlplus] command to connect to database that you use normally even outside the shell script. To understand this, let's take an example. In this example, we will connect to database, fire a query and get the output printed from the unix shell. Ok? Here we go $>res=`sqlplus -s username/password@database_name <<EOF SET HEAD OFF; select count(*) from dual; EXIT; EOF` $> echo $res 1
If you connect to database in this method, the advantage is, you will be able to pass
Unix side shell variables value to the database. See below example
<<EOF
select count(*) from student_table t where t.last_name=$1; EXIT; EOF` $> echo $res 12
How to check the command line arguments in a UNIX command in Shell Script?
In a bash shell, you can access the command line arguments using $0, $1, $2, variables, where $0 prints the command name, $1 prints the first input parameter of the command, $2 the second input parameter of the command and so on.