Another awk question


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Another awk question
# 1  
Old 01-23-2011
Another awk question

Hello

I'm (another) newbie in bash scripting (my second day).
I'm trying to locate a phrase in a number of files (multiple files in subdirectories) replace this phrase with another one
and produce an output that contains the filename of the search string occurrence, the row in that file and the changed row.

In order to feed with files my search command I use

Code:
find "$TopDir" -type f | xargs ......

I have build the following command in order to locate the phrase that I'm looking for and produce FILENAME and line number.

Code:
find $SearchDir -type f | xargs awk '/FindMe/ {print FILENAME " " FNR}'> out.txt

I also build the following command in order to replace the FindMe phrase with the WriteMe phrase.

Code:
find $SearchDir -type f | while read file
 do
awk '{gsub(/FindMe/,"WriteMe");print}' $file >$file.$$
 mv $file.$$ $file
 done

Trying to add into the out.txt a 3rd column with the changed line is giving a headache.

Any help would greatly appreciated.


Pavlos

Last edited by pavlosgr; 01-23-2011 at 09:34 AM..
# 2  
Old 01-23-2011
Assuming you want to search for the word "FindMe" in the files and replace them with "WriteMe":
Code:
#!/bin/ksh

search="FindMe"
replace="WriteMe"
SearchDir="/your/dir"

find $SearchDir -type f | 
while read file
  grep "$search" "$file" > /dev/null 2>&1
  if [ $? -eq 0 ]
  then
    awk '$0 ~ s {gsub(s, r)}1' s="$search" r="$replace" "$file" > "${file}".$$
    mv "${file}".$$ "$file"
  fi
done

# 3  
Old 01-23-2011
Quote:
Originally Posted by Franklin52
Assuming you want to search for the word "FindMe" in the files and replace them with "WriteMe":
Code:
#!/bin/ksh

search="FindMe"
replace="WriteMe"
SearchDir="/your/dir"

find $SearchDir -type f | 
while read file
  grep "$search" "$file" > /dev/null 2>&1
  if [ $? -eq 0 ]
  then
    awk '$0 ~ s {gsub(s, r)}1' s="$search" r="$replace" "$file" > "${file}".$$
    mv "${file}".$$ "$file"
  fi
done

Thanks for the reply but I'm a little lost.
What the redirection /dev/null 2 is? And a 2nd redirection after that?
Can we direct output in two directions at the some time?

That's my main problem as I cannot figure out how I'm going to produce a report file (filename - file line number - new line content) and on the some time to direct my output to each file that needs to be changed.
# 4  
Old 01-23-2011
Code:
> /dev/null

- direct standard output (which has a file descriptor of 1 (and can also be written as 1> /dev/null)) to the null device.

Code:
2>&1

- copy standard error (which has a file descriptor 2) to standard output (which now points to /dev/null)

(thus sending both standard output and standard error to /dev/null)

You could rewrite that slightly:
Code:
if grep -q "$search" "$file"; then
  ...
fi

# 5  
Old 01-25-2011
Finally I have build the following code.

awk produces a file replaced.txt which is a log of the changes that will take place. It logs line, filename and the changed line.

sed makes the changes.

As each file is read twice and I have a few thousands of files I wonder if I can perform the task with one file read.

Code:
read SearchDir
read SearchString
read ReplaceString


find "$SearchDir" -type f | 
while read file
do
  awk '$0 ~ /'$SearchString'/ {gsub(s, r); print FNR "\t" FILENAME "\t" $0}' s="$SearchString" r="$ReplaceString" "$file" >> replaced.txt
  sed -i 's/'$SearchString'/'$ReplaceString'/g' "$file"
done

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

awk question : system output to awk variable.

Hi Experts, I am trying to get system output to capture inside awk , but not working: Please advise if this is possible : I am trying something like this but not working, the output is coming wrong: echo "" | awk '{d=system ("date") ; print "Current date is:" , d }' Thanks, (5 Replies)
Discussion started by: rveri
5 Replies

2. Homework & Coursework Questions

awk question.

Use and complete the template provided. The entire template must be completed. If you don't, your post may be deleted! 1. The problem statement, all variables and given/known data: im using ls -l | xargs | awk '{what ever files here}' im trying to get something that looks like this... (7 Replies)
Discussion started by: rontopia
7 Replies

3. UNIX for Dummies Questions & Answers

Basic awk question...getting awk to act on $1 of the command itself

I have a script problem that I am not able to solve due my very limited understanding of unix/awk. This is the contents of test.sh awk '{print $1}' From the prompt if I enter: ./test.sh Hello World I would expect to see "Hello" but all I get is a blank line. Only then if I enter "Hello... (2 Replies)
Discussion started by: JasonHamm
2 Replies

4. UNIX for Dummies Questions & Answers

awk question

Can anyone help with this this one liner: nawk -v RS='' '$1=$1' InputFile What I have in the file: 0.0013985457223116 -0.0002338180925628 0.0 0.0003709430584958 -0.0005763523138347 0.0 And the output I want: 0.0013985457223116 -0.0002338180925628 0.0 0.0003709430584958... (1 Reply)
Discussion started by: mookie123
1 Replies

5. UNIX for Dummies Questions & Answers

awk question

Hi, in the following script, what does " a=$0} END " mean ? do we need that ? Thanks awk '{a=$0} END {for (i=NR; i>=1; i--) print a}' file (3 Replies)
Discussion started by: james94538
3 Replies

6. Shell Programming and Scripting

Awk Question

How I can rid of the following presentation du -sk /u*/oradata/TEST/*.dbf |awk '{print total+=$1} 1.28003e+06 4.35109e+06 4.36134e+06 4.4535e+06 5.47752e+06 5.48777e+06 7.52554e+06 7.73036e+06 9.06158e+06 :confused: thank you (3 Replies)
Discussion started by: zam
3 Replies

7. Shell Programming and Scripting

awk question

I am trying to read through a file, gather the states in that file and change it from an abbreviation to the ful text. Can anyone provide some assistance. Thanks!! (4 Replies)
Discussion started by: cnitadesigner
4 Replies

8. Shell Programming and Scripting

awk .. question?

i have a little awk script that I use looks this: awk '{if (FNR==1){print FILENAME; print $0}else print $0}' file1...file2....fi... > bundled. i have completely forgotten how to unbundle this. I have tried several different approaches and still can not remember how to unbundle the file bundled.... (2 Replies)
Discussion started by: moxxx68
2 Replies

9. Shell Programming and Scripting

One more awk question!

Hello, I have the following command that does 2 searches. awk '{if ($0 ~ /STRING1/) {c++} }{if ( c == 2 ) {sub(/STRING1/,"NEWSTRING") } } { print }' FILE How do I search up after the first search? thanks (4 Replies)
Discussion started by: ctcuser
4 Replies

10. Shell Programming and Scripting

awk question

I have the following error: ls -lt | awk 'BEGIN NR > 1 { print $2, $9 }' Syntax Error The source line is 1. The error context is BEGIN >>> NR <<< > 1 { print $2, $9 } awk: 0602-500 Quitting The source line is 1. What I want to do is ls a directory, skip the first... (3 Replies)
Discussion started by: lesstjm
3 Replies
Login or Register to Ask a Question