Append text to files with a same pattern


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Append text to files with a same pattern
# 1  
Old 06-20-2012
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 11:28 AM.. Reason: code tags
# 2  
Old 06-20-2012
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

This User Gave Thanks to ctsgnb For This Post:
# 3  
Old 06-20-2012
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
Thanks. This one worked perfect.

Quote:
Originally Posted by ctsgnb
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

# 5  
Old 06-20-2012
The original solution is great; it just needs to lose the double quotes around the x[1-9].

Regards,
Alister
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

How to preserve the same text and modify the same text to append?

Hi, I have an issue modifying a file. Below is the content of the file. File name - a.txt SELECT LISTAGG(SCORE), LISTAGG(VB), LISTAGG(SCORE*12 + 3), LISTAGG(DOB) from T_CONCAT ; SELECT LISTAGG(SCORE), LISTAGG(VB), LISTAGG(SCORE*100 + 3), from... (1 Reply)
Discussion started by: Mannu2525
1 Replies

2. UNIX for Beginners Questions & Answers

UNIX script to append multiple text files into one file based on pattern present in filaname

Hi All-I am new to Unix , I need to write a script. Can someone help me with a requirement where I have list of files in a directory, I want to Merge the files if a pattern of string matches in filenames? AAAL_555A_ORANGE1_F190404.TXT AAAL_555A_ORANGE2_F190404.TXT AAAL_555A_ORANGE3_F190404.TXT... (6 Replies)
Discussion started by: Shankar455
6 Replies

3. Shell Programming and Scripting

Append text on particular line after pattern found

hi, i have /etc/inittab, I want to add another line after that when i find a pattern "l6:6:wait:/etc/rc.d/rc 6". original l6:6:wait:/etc/rc.d/rc 6 after-change l6:6:wait:/etc/rc.d/rc 6 /sbin/if-pp-to-cng (3 Replies)
Discussion started by: learnbash
3 Replies

4. Shell Programming and Scripting

Use sed to append text to filenames if text not already present

I have some html with hrefs that contain local links to pdf filenames. These filenames should have standardised names, i.e. there should be a label prior to the ".pdf" filename suffix. There can be many of these links on a single line of text and some may already have the label. For example ... (13 Replies)
Discussion started by: adb
13 Replies

5. Shell Programming and Scripting

append text to column in all files of directory

Hi, I want to append "chr" to all col 2 values of all files in a particular folder. This is what I came up with but isnt working. Please help. ls -1 * | ( while read line do awk 'BEGIN {FS=OFS=":"} {$2="chr"$2;print $0}' $line > $line_new done ) Another question is, how to delete... (7 Replies)
Discussion started by: alpesh
7 Replies

6. Shell Programming and Scripting

Append lines for a specific pattern

Input: 09:43:46,538 INFO first text 10:45:46,538 INFO second text 11:00:46,538 INFO third more text Output: 09:43:46,538 INFO first text 10:45:46,538 INFO second text 11:00:46,538 INFO third more text The rule is to append all lines so each line contains this format... (7 Replies)
Discussion started by: chitech
7 Replies

7. UNIX for Dummies Questions & Answers

Count Number Of lines in text files and append values to beginning of file

Hello, I have 50 text files in a directory called "AllFiles" I want to make a program that will go inside of the "AllFiles" Directory and count the number of lines in each individual text file. Then, the program will calculate how many more lines there are over 400 in each text file and... (7 Replies)
Discussion started by: motoxeryz125
7 Replies

8. Shell Programming and Scripting

Find required files by pattern in xml files and the change the pattern on Linux

Hello, I need to find all *.xml files that matched by pattern on Linux. I need to have written the file name on the screen and then change the pattern in the file just was found. For instance. I can start the script with arguments for keyword and for value, i.e script.sh keyword... (1 Reply)
Discussion started by: yart
1 Replies

9. Shell Programming and Scripting

read from a specific pattern from one file and append it to another

Hi! Everyone, Say this file1 -------------- line 1 51610183 420001010 0010CTCTLEDPPOO 2151610183 line 2 2151610183 420001010 0030A2TH2 line 3 2151610183 420001010 0040A2TH3 line 4 2151610183 420001010 ... (0 Replies)
Discussion started by: kinkar_ghosh
0 Replies

10. Shell Programming and Scripting

how to append the pattern at the end of the line

-Hi I have multiple files which contain a line with the word "exec". I need to add the following pattern " -cmode -ccheap" on the same line where "exec" is at the end. Any idea? Thanks a lot in advance to everybody... -A (2 Replies)
Discussion started by: aoussenko
2 Replies
Login or Register to Ask a Question