Go Back   The UNIX and Linux Forums > Top Forums > UNIX for Dummies Questions & Answers


UNIX for Dummies Questions & Answers If you're not sure where to post a UNIX or Linux question, post it here. All UNIX and Linux newbies welcome !!

Closed Thread    
 
Thread Tools Search this Thread Display Modes
    #1  
Old 06-20-2012
Registered User
 
Join Date: Mar 2012
Posts: 18
Thanks: 8
Thanked 1 Time in 1 Post
Append text to files with a same pattern

Hi Folks,

I wanted to know if i can use RegEx in a for-loop of a shell script.
Here's a scenario, I have a set of files say x1, x2, x3..x9 in a directory(obviously with files other than this pattern). I want to append a line of text to all files that follow pattern x[1-9]. Can someone help me out? I know its simple. But i just cannot get it work.


Code:
for i in "x[1-9]"
do
echo hello >> "$i"
done

I'm looking for something like the one above(which is not working for me).

Last edited by Scrutinizer; 06-20-2012 at 10:28 AM.. Reason: code tags
Sponsored Links
    #2  
Old 06-20-2012
ctsgnb ctsgnb is offline Forum Advisor  
Registered User
 
Join Date: Oct 2010
Location: France
Posts: 2,763
Thanks: 72
Thanked 587 Times in 561 Posts
make sure you are in the directory that contains those files or use their absolute path.


Code:
cd $YOURDIR
for i in x?
do
echo "hello" >>./$i
done

The Following User Says Thank You to ctsgnb For This Useful Post:
prithvirao17 (06-20-2012)
Sponsored Links
    #3  
Old 06-20-2012
Registered User
 
Join Date: May 2012
Posts: 58
Thanks: 5
Thanked 9 Times in 9 Posts
Or this:


Code:
#!/bin/ksh
 
cd ${working_dir}
ls x[1-9] | while read file
do
  echo "hello" >> ${file}
done

    #4  
Old 06-20-2012
Registered User
 
Join Date: Mar 2012
Posts: 18
Thanks: 8
Thanked 1 Time in 1 Post
Thanks. This one worked perfect.

Quote:
Originally Posted by ctsgnb View Post
make sure you are in the directory that contains those files or use their absolute path.


Code:
cd $YOURDIR
for i in x?
do
echo "hello" >>./$i
done

Sponsored Links
    #5  
Old 06-20-2012
alister alister is offline Forum Advisor  
Registered User
 
Join Date: Dec 2009
Posts: 2,599
Thanks: 122
Thanked 716 Times in 599 Posts
The original solution is great; it just needs to lose the double quotes around the x[1-9] .

Regards,
Alister
Sponsored Links
Closed 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
append text to column in all files of directory alpesh Shell Programming and Scripting 7 06-15-2012 01:33 PM
Append lines for a specific pattern chitech Shell Programming and Scripting 7 02-23-2012 05:46 PM
Count Number Of lines in text files and append values to beginning of file motoxeryz125 UNIX for Dummies Questions & Answers 7 04-28-2011 02:36 AM
sed: Find start of pattern and extract text to end of line, including the pattern TestTomas Shell Programming and Scripting 5 05-27-2009 11:16 AM
how to append the pattern at the end of the line aoussenko Shell Programming and Scripting 2 10-31-2008 03:06 PM



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