Home Man
Search
Today's Posts
Register

BSD, Linux, and UNIX shell scripting — Post awk, bash, csh, ksh, perl, php, python, sed, sh, shell scripts, and other shell scripting languages questions here.

awk find and replace in multiple files

Tags
shell scripts

Login to Reply

 
Thread Tools Search this Thread
# 1  
Old 05-24-2009
awk find and replace in multiple files

Hi
I use the following code to replace ‘.’ with ‘N’ in my files and keep both versions.

awk '{ gsub(/\./,"N"); print }' file_0001.txt > path/to/new/dir/file_0001.txt

I need help on how to apply the code to 100 files instead of doing them one file at a time. The files are labeled file_0001.txt to file_0100.txt.
Thanks
joseph
# 2  
Old 05-24-2009
Assuming the only files in the current directory with a filename pattern of file_0*.txt are the ones you wish to make editted copies of then the following would do the trick:

Code:
for FILE in file_0*.txt; do
  awk '{ gsub(/\./,"N"); print }' ${FILE} > path/to/new/dir/${FILE}
done

# 3  
Old 05-24-2009
got an error

Hi
Thank you for your help. I got an error.

here is the code:

#!/bin/bash
#
for FILE in s_7_1_0*_qseq.txt; do
awk '{ gsub(/\./,"N"); print }' ${FILE} > /Users/mydir/${FILE}
done

I named it 'script.sh'

and this is what I did:
chmod +x script.sh
./script.sh

this what I got:

'/script.sh: line 3: syntax error near unexpected token `do
done' '{ gsub(/\./,"N"); print }' ${FILE} > /Users/mydir/${FILE}
# 4  
Old 05-25-2009
The same script is working fine for me.

Open the file in "vi" and check there may some characters like "^M" in the file .
# 5  
Old 05-25-2009
just a ls and reading using a while should do
Code:
ls file* | while read file
do
# apply changes, transformations
done

# 6  
Old 05-25-2009
Quote:
Originally Posted by jdhahbi
Hi
Thank you for your help. I got an error.

here is the code:

#!/bin/bash
#
for FILE in s_7_1_0*_qseq.txt; do
awk '{ gsub(/\./,"N"); print }' ${FILE} > /Users/mydir/${FILE}
done

I named it 'script.sh'

and this is what I did:
chmod +x script.sh
./script.sh

this what I got:

'/script.sh: line 3: syntax error near unexpected token `do
done' '{ gsub(/\./,"N"); print }' ${FILE} > /Users/mydir/${FILE}
Works for me:

Code:
$ cat ./script.sh
#!/bin/bash
#
for FILE in s_7_1_0*_qseq.txt; do
  echo FILE = ${FILE}
  ls -l ${FILE}
  mkdir -p Users/mydir
  awk '{ gsub(/\./,"N"); print }' ${FILE} > Users/mydir/${FILE}
  echo before
  cat ${FILE}
  echo after
  cat Users/mydir/${FILE}
done
$
$ ./script.sh
FILE = s_7_1_00_qseq.txt
-rw-r--r-- 1 tony tony 37 2009-05-25 09:31 s_7_1_00_qseq.txt
before
m.m.m.m.m.m.m.m.m
n.n.n.n.n.n.n.n.n.
after
mNmNmNmNmNmNmNmNm
nNnNnNnNnNnNnNnNnN
FILE = s_7_1_01_qseq.txt
-rw-r--r-- 1 tony tony 37 2009-05-25 09:31 s_7_1_01_qseq.txt
before
m.m.m.m.m.m.m.m.m
n.n.n.n.n.n.n.n.n.
after
mNmNmNmNmNmNmNmNm
nNnNnNnNnNnNnNnNnN
$

I have changed the target directory to be under my urrent directory for the purposes of testing.
# 7  
Old 05-25-2009
Thanks

Thanks to all of you. I got it to work.
Login to Reply

« Previous Thread | Next Thread »
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Find and replace from multiple files ninocap UNIX for Beginners Questions & Answers 2 06-12-2018 04:14 AM
Find and replace in multiple files ashokvpp Shell Programming and Scripting 1 05-04-2018 07:45 AM
Script to find & replace a multiple lines string across multiple php files and subdirectories spfc_dmt Shell Programming and Scripting 12 03-07-2012 09:05 AM
linux find and replace in multiple files cokedude UNIX for Advanced & Expert Users 2 09-14-2011 07:55 AM
Find, replace, file path in multiple files for Solaris 10 LRoberts Emergency UNIX and Linux Support 5 04-21-2011 09:31 AM
how to find and replace strings in multiple files etldeveloper UNIX for Dummies Questions & Answers 0 02-11-2011 05:00 AM
shell script to find and replace string in multiple files pharos467 Shell Programming and Scripting 11 01-15-2009 10:40 AM
Find and replace a string in multiple files pharos467 UNIX for Dummies Questions & Answers 2 11-05-2007 10:47 PM
Find and Replace in multiple files (Shell script) jatins_s Shell Programming and Scripting 13 11-05-2007 01:11 PM
Find and replace files in multiple folders lodey Shell Programming and Scripting 6 09-28-2007 02:00 AM


All times are GMT -4. The time now is 10:50 AM.

Unix & Linux Forums Content Copyright©1993-2018. All Rights Reserved.
UNIX.COM Login
Username:
Password:  
Show Password