awk script problem


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting awk script problem
# 1  
Old 10-20-2011
awk script problem

Hello guys i have following problem. I'm trying to copy content of one file and paste this content in all .txt files in directory, but at line 15. My script copy the content at first line, not 15. I'm confused how to do this. Thank you in advance for your help!

This is my script:
Code:
ARGS=2 
E_WRONGARGS=85 
if [$# -ne "ARGS"] 
then  
  echo "Usage: directory template" 
  exit $WRONGARGS 
fi 

args=("$@") 
temp=$RANDOM 

for file in $(ls $1/*.txt) 
do 
  awk 'NR==15 {print}1' $2 $file>$temp 
  mv $temp $file 
done


Last edited by Franklin52; 10-20-2011 at 03:46 AM.. Reason: Please use code tags, thank you
# 2  
Old 10-20-2011
That's a useless use of ls *, the statement has the same effect without the ls and backticks -- or perhaps even better, since it won't get confused by spaces the way backticks will.

Code:
'NR==15 {print}1'

A '1' in that location causes it to print every line.

I don't think your awk script does what you mean it to anyway. It will act like 'cat $2 $file | awk', smushing the content of both files together before feeding it into your awk program.

I'm not entirely sure what you actually want. Are you trying to replace line N in file.txt with line N from your template? Could you post what your template file looks like, what the input textfile should look like, and what the output text file should look like?
# 3  
Old 10-20-2011
This is the content of file that i want to edit:

Code:
LIST "test"
DEFAULTS
{
    FILESYSTEM
    {

    }  
    RAWDISK
    {

    } 
}


HOST "localhost" localhost
{
}

This is the content of template that i want to insert in first file:

Code:
GROUP "test"
{
}



I want result file like this :

Code:
LIST "test"
DEFAULTS
{
    FILESYSTEM
    {

    }  
    RAWDISK
    {

    } 
}

GROUP "test"
{
}


HOST "localhost" localhost
{
}

I want if awk find "RAWDISK" string in first file to copy content of template file 6 lines below it

Last edited by radoulov; 10-20-2011 at 12:03 PM.. Reason: Code tags!
# 4  
Old 10-20-2011
You can't give awk two files in parameters if you expect awk to do anything but read them sequentially. Use getline to get things out-of-order.

How about this:

Code:
$ cat insert.awk
BEGIN { RAW=-1 }

# Whenever we get a line matching ^RAWDISK, set RAW=6.
/^RAWDISK/ { RAW=6 }

# Print every line we get and subtract 1 from RAW.
{ RAW-- } 1

# RAW must have been set to 6, 6 lines ago.
# Read and print everything from FILE.
RAW==0 { while(getline <FILE) print; }

$ awk -v FILE="insert" -f insert.awk data

LIST "test"
DEFAULTS
{
FILESYSTEM
{

}
RAWDISK
{

}
}

GROUP "test"
{
}

HOST "localhost" localhost
{
}

$

# 5  
Old 10-20-2011
My final script is this:
Code:
ARGS=2  
E_WRONGARGS=85 
 if [$# -ne "ARGS"]
then     
echo "Usage: directory template"    
exit $WRONGARGS 
 fi   

args=("$@")  
temp=$RANDOM   

for file in $1/*.txt)  do 
IFS=\$
cat $2
while read line
do
awk -v template=$line '/RAWDISK/ {c=6}! (c--) {if (NR!=1){print template}1}1 $file>$temp  mv $temp $file 
done <$2 
done

But as result i have in new file:
Code:
}
{
GROUP "test"

not

GROUP "test" {
}


Last edited by Franklin52; 10-21-2011 at 07:44 AM.. Reason: Please use code tags, thank you
# 6  
Old 10-20-2011
There are so many things wrong with that it shouldn't even execute. You've forgotten some important quotes, you've got leftover brackets in random places, that 'cat' prints to the terminal and nowhere else, and you're not even reading the template in awk anywhere at all.

I suggest you try what I actually posted, it works.

---------- Post updated at 09:47 AM ---------- Previous update was at 09:40 AM ----------

Code:
for f in $1/*.txt
do
        awk -v FILE="$2" -f template.awk "$f" > "$f.new"
done

Don't move $f.new overtop of $f until you've tested and are SURE the output is what you want.
# 7  
Old 10-20-2011
I try yor solution , but nothing happens , no change in txt files Smilie
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Awk Script Problem

Can someone please explain to me what is wrong with this awk script? echo 74 85 | awk '{ if ( $1 > $2 ) PRESULTS = ( $1 - $2 ); print $0,"=>","P"PRESULTS ; else if ( $1 > $2 ) NRESULTS = ( $2 - $1... (3 Replies)
Discussion started by: SkySmart
3 Replies

2. Shell Programming and Scripting

problem with awk script

Hi, I have two files Hi, I have two files file1 :> Code: val="10" port="localhost:8080"httpadd="http:\\192.168.0.239" file2 :> Code: val=${val} val="pdssx" port=${port}port="1324"httpadd=${httpadd}httpadd="raamraav"fileloc=${fileloc} file3(or file2) should have following... (1 Reply)
Discussion started by: nitin.pathak
1 Replies

3. Shell Programming and Scripting

Awk script Problem

Hi , I am having two files FILE1 and FILE2 as shown below I need to search each and every element of Coulumn1 in the FILE1 in FILE2 and Globally replace with the Corresponding element of the Column2 in the FILE2 , For example and1 which is the first element of COl 1 of the FILE1 should be... (4 Replies)
Discussion started by: jaita
4 Replies

4. Shell Programming and Scripting

Problem with awk script

Hi, I have one csv file with 3 fileds like tmp1.csv 2079|2010Aug|cardilogy 2349|2010Aug|numerology 2213|2010Aug|immunlogy another csv file with code for those specialities spec.csv cardiology|CRD numerology|NMY immunology|IMY i want to replace the contents of file 1 with codes... (2 Replies)
Discussion started by: Man83Nagesh
2 Replies

5. Shell Programming and Scripting

Problem with an awk Script

hello, first, yes i searched the forum , google and read many tutorials but still have a problem with my script. I have great Problems, because i haven't worked with regular expressions before and never had anything to do with shellscripts. i am a complete Newby in this sort of theme. I have... (8 Replies)
Discussion started by: Crashvogel
8 Replies

6. Shell Programming and Scripting

awk script problem

Hi All, I have the following input data: That I'd like to look like this ($2 is the column I'd like it to appear in) where the entries are grouped by date: The code I have at present is: awk 'BEGIN {} { dt = $1 if (dt == dt_prev) { pp = $3 ... (7 Replies)
Discussion started by: pondlife
7 Replies

7. Shell Programming and Scripting

Problem with a AWK Script

Hi I am having some contents in my file like this file1 ########################## pin (PIN1) { direction : input ; capacitance : 121 ; max_transition : 231 ; } pin (PIN2) { direction : input ; capacitance : 124 ; max_transition : 421 ;... (8 Replies)
Discussion started by: kshitij
8 Replies

8. Shell Programming and Scripting

Problem with awk script

Hi Can anyone help me in this Problem File1 ######################### HOLI 123 AND ONE TWO THREE AMITABH SAMSUNG POLI AND TWO SENSE CRYING WING PPIN TBFLAG I B AND OROLE TB_HOT=" DCT" TB_CAT=" CAT" TC_NOT=" AND" +PIN TB=" HOT" TB_GATE=" KOT" TB_LATE=" MAT" TC=LOT MAT DAT SAT... (5 Replies)
Discussion started by: kshitij
5 Replies

9. Shell Programming and Scripting

Problem with one awk script

Hi , I am having a file having the contents like this file1 ##################### kite kshitij jolly admire in the wing and tell me the secret behind opus 123 and the right of the track ######################### I have to write one awk script to substitue some values with other... (6 Replies)
Discussion started by: kshitij
6 Replies

10. Shell Programming and Scripting

awk script Problem

I wrote a awk but doesnt work as expected. The Input File attached input file My awk Script /^.......*EXEC CICS /,/END-EXEC/ { if ( $0 ~ / LINK / ) { tsflag=1 } if ( $0 ~ /EXEC CICS/ && tsflag == 1 ) ... (6 Replies)
Discussion started by: pbsrinivas
6 Replies
Login or Register to Ask a Question