Unix/Linux Go Back    


Shell Programming and Scripting 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

Shell Programming and Scripting


Closed    
 
Thread Tools Search this Thread Display Modes
    #1  
Old Unix and Linux 05-24-2009   -   Original Discussion by jdhahbi
jdhahbi jdhahbi is offline
Registered User
 
Join Date: Sep 2008
Last Activity: 31 May 2011, 8:15 PM EDT
Posts: 71
Thanks: 6
Thanked 1 Time in 1 Post
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
Sponsored Links
    #2  
Old Unix and Linux 05-24-2009   -   Original Discussion by jdhahbi
TonyFullerMalv's Unix or Linux Image
TonyFullerMalv TonyFullerMalv is offline Forum Advisor  
Registered User
 
Join Date: Sep 2008
Last Activity: 1 July 2013, 6:45 PM EDT
Location: Malvern, Worcs. U.K.
Posts: 1,033
Thanks: 0
Thanked 8 Times in 8 Posts
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

Sponsored Links
    #3  
Old Unix and Linux 05-24-2009   -   Original Discussion by jdhahbi
jdhahbi jdhahbi is offline
Registered User
 
Join Date: Sep 2008
Last Activity: 31 May 2011, 8:15 PM EDT
Posts: 71
Thanks: 6
Thanked 1 Time in 1 Post
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 Unix and Linux 05-25-2009   -   Original Discussion by jdhahbi
panyam panyam is offline Forum Advisor  
Registered User
 
Join Date: Sep 2008
Last Activity: 13 April 2017, 4:34 AM EDT
Posts: 1,160
Thanks: 20
Thanked 105 Times in 100 Posts
The same script is working fine for me.

Open the file in "vi" and check there may some characters like "^M" in the file .
Sponsored Links
    #5  
Old Unix and Linux 05-25-2009   -   Original Discussion by jdhahbi
matrixmadhan matrixmadhan is offline Forum Advisor  
Technorati Master
 
Join Date: Mar 2005
Last Activity: 10 January 2017, 8:10 AM EST
Location: classification algos
Posts: 3,215
Thanks: 19
Thanked 31 Times in 28 Posts
just a ls and reading using a while should do

Code:
ls file* | while read file
do
# apply changes, transformations
done

Sponsored Links
    #6  
Old Unix and Linux 05-25-2009   -   Original Discussion by jdhahbi
TonyFullerMalv's Unix or Linux Image
TonyFullerMalv TonyFullerMalv is offline Forum Advisor  
Registered User
 
Join Date: Sep 2008
Last Activity: 1 July 2013, 6:45 PM EDT
Location: Malvern, Worcs. U.K.
Posts: 1,033
Thanks: 0
Thanked 8 Times in 8 Posts
Quote:
Originally Posted by jdhahbi View Post
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.
Sponsored Links
    #7  
Old Unix and Linux 05-25-2009   -   Original Discussion by jdhahbi
jdhahbi jdhahbi is offline
Registered User
 
Join Date: Sep 2008
Last Activity: 31 May 2011, 8:15 PM EDT
Posts: 71
Thanks: 6
Thanked 1 Time in 1 Post
Thanks

Thanks to all of you. I got it to work.
Sponsored Links
Closed

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Linux More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
shell script to find and replace string in multiple files pharos467 Shell Programming and Scripting 11 01-15-2009 11:40 AM
Find and replace a string in multiple files pharos467 UNIX for Dummies Questions & Answers 2 11-05-2007 11:47 PM
Find and Replace in multiple files (Shell script) jatins_s Shell Programming and Scripting 13 11-05-2007 02:11 PM
Find and replace files in multiple folders lodey Shell Programming and Scripting 6 09-28-2007 03:00 AM



All times are GMT -4. The time now is 05:22 AM.