Trouble using awk command


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Trouble using awk command
# 1  
Old 12-22-2014
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
Code:
> 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?
# 2  
Old 12-22-2014
Wrench

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.
Code:
cat t.txt temp.dml > OUTPUT_check

Just redirecting the output to a file named OUTPUT_check which will be as follows.
Code:
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
# 3  
Old 12-22-2014
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.
# 4  
Old 12-22-2014
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.

Code:
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.
Code:
./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
# 5  
Old 12-22-2014
Try
Code:
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

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Advanced & Expert Users

awk trouble inside another command

I tried running this. dsh -w server1 'lsof /audit | awk '{ print $2 }'' It did not like above so I tried to escape the single parenthesis at the end. dsh -w server1 'lsof /audit | awk '{ print $2 }\'' It then hung so I changed up the parenthesis to this. This worked. dsh -w server1... (6 Replies)
Discussion started by: cokedude
6 Replies

2. Shell Programming and Scripting

Trouble with awk command

Hi, I need to read a string with ; separated using loop one filed by one field and perform some operation. Can you please check and let me know how to print command parameterised. key=phani;ravi;kiran number_of_keys=`echo $key|awk '{print NF}' FS=';'` for (( i = 1; i <= $number_of_keys;... (4 Replies)
Discussion started by: Ravindra Swan
4 Replies

3. Shell Programming and Scripting

Trouble with passing Variable from bash to awk gsub command

Would really appreciate it if someone could point out my mistake in this line of code, i've been staring blankly at it trying everything i can think of some time now and coming up with nothing. #!/bin/bash echo "Enter Username" read Username awk -F: -v var=${Username} '/^var:/... (9 Replies)
Discussion started by: Nostyx
9 Replies

4. UNIX for Dummies Questions & Answers

trouble with awk

I am trying to figure awk. I have a file in my home directory called testawk.sh, have made it executable, and have run it... But don't see any output. This is the contents of the file: #!/usr/bin/awk -f { print " - HI -" }I enter ./testawk.sh in the prompt, press enter, and watch as the... (2 Replies)
Discussion started by: matthewden
2 Replies

5. Shell Programming and Scripting

Trouble getting the next to last record with awk

Hello all, I'm a beginner to shell/ awk script writing, and I'm trying to do something that looks like it shouldn't be (too) hard at all to do, but unfortunately, I can't seem to be able to find the right way to do it with awk. I need to look for the time several processes start & end in a... (5 Replies)
Discussion started by: Muadib
5 Replies

6. Shell Programming and Scripting

Trouble with Awk

Hi all, I'm writing a program in bourne shell that compresses a file 3 different ways then displays a table of data with the compression type, original file size, compressed size and compression ratio. I've written most of it but reached 2 problems that won't allow me to finish it correctly. The... (2 Replies)
Discussion started by: javajynx
2 Replies

7. Shell Programming and Scripting

having trouble with using if clause in AWK

The goal: I have a list of people in teams. The list looks something like this $1 = Job Position (marketing, IT, PR) $2 = Name $3 = Team Name $4 = Targeted member (somebody in field 2 targets somebody else) $5 = Employment Status (full time/part time/etc) The idea is to search through... (2 Replies)
Discussion started by: MaestroRage
2 Replies

8. Shell Programming and Scripting

Trouble with awk

This is probably a fairly simple question but I cant seem to get it to work. Im trying to multiply an entire column in a file by a variable in my bash script but just cant seem to get it to work with awk. Here is what I'm trying $varr is some value $line is my file awk '{print... (1 Reply)
Discussion started by: RichieFondel
1 Replies

9. AIX

Trouble formatting egrep command with AWK

Hi, I'm new to scripting and AIX. I'm running the following: lspv | awk '{ print "lspv",$1" | egrep 'PP\|PHYSICAL'; lspv -l",$1 }' Which creates this command: lspv hdisk0 | egrep PP|PHYSICAL; lspv -l hdisk0 lspv hdisk1 | egrep PP|PHYSICAL; lspv -l hdisk1 Troube is, I need the... (2 Replies)
Discussion started by: cruiser
2 Replies

10. Shell Programming and Scripting

trouble using mailx command

Hi. I have been trying to send mail using the mailx command. I also tryed to use the mail command. The thing is that when I try to send the email, i receive automatically to my mailbox a DAEMON response sayng that the mailhost is unknown... The syntax I am using is this: $mailx -s "this... (2 Replies)
Discussion started by: ldrojasm
2 Replies
Login or Register to Ask a Question