Trouble using awk command

Hi,

I have 2 .txt pads containing data.
I need a script which reads content of one .txt file, performs some operations and calculates a number which is stored in a variable.
Now , all the content of another .txt pad should be appended to first .txt pad at pre calculated nth line number.

Below is sample data and commands i tried

> cat temp.dml
record
  decimal("\x01") lst_updt_run_id; /* INTEGER NOT NULL*/
decimal("\x01") invalid_data;
end;
 
cat > t.txt
this is the first line
[[this is second line]]
. h.ksh
 
expected O/P: If want to append at 1st line
 
this is the first line
[[this is second line]]
. h.ksh
record
  decimal("\x01") lst_updt_run_id; /* INTEGER NOT NULL*/
decimal("\x01") invalid_data;
end;
 
tried code:
> awk 'NR==1{file="t.txt";while ((getline<file) > 0) {print}}1' temp.dml | uniq
this is the first line
[[this is second line]]
. h.ksh
  decimal("\x01") lst_updt_run_id; /* INTEGER NOT NULL*/
decimal("\x01") invalid_data;
end;

there were couple of issues faced:

  1. I tried matching NR with a variable which contains the line position to append but it was not working.
    2.Last row of second .txt pad is repeating and hence i used uniq keyword.

Any solutions to this?

Hi Ravindra_swan,

If I understood your requirement correctly you need to append 2 files, you can simple use cat command for same. Let me know if this helps if not kindly let us know all the details for requirement, with os which you are using.

cat t.txt temp.dml > OUTPUT_check

Just redirecting the output to a file named OUTPUT_check which will be as follows.

cat OUTPUT_check
this is the first line
[[this is second line]]
. h.ksh
record
  decimal("\x01") lst_updt_run_id; /* INTEGER NOT NULL*/
decimal("\x01") invalid_data;
end;

Thanks,
R. Singh

Thanks RavinderSingh13,

Yes you got my requirement mostly but the content of first .txt file should be appended to another at nth position.
This nth position can vary.

With the solution you gave it will always append at the first line.

So, i need a solution where a variable can be used which can hold the line position to append.

Hello Ravindra_swan,

Following may help you in same, this scrit will enter the second file lines from the position provided by user while running the script.

cat check.ksh
echo "Please enter the line number:"
read line_number

awk -vLINE=$line_number 'function check (){system("cat temp.dml")} FNR==LINE{check()} 1' t.txt

Output will be as follows.

./get_line_check.ksh 
Please enter the line number:
2
this is the first line
record
  decimal("\x01") lst_updt_run_id; /* INTEGER NOT NULL*/
decimal("\x01") invalid_data;
end;
[[this is second line]]
. h.ksh

NOTE: If you want to print first the nth line and then start to print the other file's lines you can use FNR==LINES+1 in above code.

Thanks,
R. Singh

Try

sed '1 r temp.dml' t.txt
this is the first line
record
  decimal("\x01") lst_updt_run_id; /* INTEGER NOT NULL*/
decimal("\x01") invalid_data;
end;
[[this is second line]]
. h.ksh