Insert Filename into Multiple Files


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Insert Filename into Multiple Files
# 1  
Old 04-04-2013
Insert Filename into Multiple Files

Hi,

We have a folder that has files in the following structure

Code:
abc.sql
def.sql
efg.sql
.
.
.
xyz.sql

I have to find a certain string (say "test") in each file and replace it with the name of the file. For eg. if "test" is present in abc.sql, I want to replace it with "test abc". If test is present in def.sql, then I want to replace it with "test def"
# 2  
Old 04-04-2013
Try this
Code:
#!/bin/bash

awk '{f=FILENAME;sub(/.sql/,x,f);sub(/test/,"test "f)}{print >> FILENAME".new"}' *.sql
for file in *.new
do
  fname=${file%.*}
  mv $file $fname
done

--ahamed

Last edited by ahamed101; 04-04-2013 at 06:19 AM..
This User Gave Thanks to ahamed101 For This Post:
# 3  
Old 04-04-2013
I am very new to awk and also this forum. Kindly guide me as to where exactly should i be placing my search and replace terms in the code you've provided. Thank you for your time.
# 4  
Old 04-04-2013
You need to replace "test" in the code given to whatever string you want to replace with. Highlighted in previous post.
Also, please try to understand the code, so that you will also learn.

--ahamed
This User Gave Thanks to ahamed101 For This Post:
# 5  
Old 04-04-2013
Here is an alternative way that might be easier to understand (and hopefully works):
Code:
string=test
for file in *.sql; do
  base=`basename $file .sql`
  sed "s/$string/& $base/g" $file > temp
  mv temp $file
done

If you are kind of new to this, trying to figure out awk syntax is not the best place to start. Smilie
# 6  
Old 04-04-2013
saved my day! Thank you!SmilieSmilie

---------- Post updated at 04:30 AM ---------- Previous update was at 04:25 AM ----------

How do I ignore the case of my search string? Also I want to exclude files where the filename is already present
# 7  
Old 04-04-2013
Code:
awk 'BEGIN{IGNORECASE=1}{f=FILENAME;sub(/.sql/,x,f);sub(/test$/,"test "f)}{print >> FILENAME".new"}' *.sql

--ahamed
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Insert FileName to headers

hi there I have a lot of files with same number of rows and columns.$2 and $3 are the same in all files . I run awk script and merge $2,$3,$1 from first file and $1 from another files. I would like to know how to modify awk script to insert filename as header for merged columns ( Expected... (6 Replies)
Discussion started by: ali.seifaddini
6 Replies

2. Shell Programming and Scripting

Join multiple files with filename

Please help, I want to join multiple files based on column 1, and put the missing values as 0. Also the colname in the output should say which file the values came from. FILE1 1 11 2 12 3 13 FILE2 2 22 3 23 4 24 FILE3 1 31 3 33 4 34 FILE1 FILE2 FILE3 1 11 0 31 (1 Reply)
Discussion started by: newbie83
1 Replies

3. Shell Programming and Scripting

Creating Multiple data files with spaces in the filename

Hi, I have a list of filenames in the format with Spaces in the filename. As an example : Sample File 1.txt Sample File 2.txt Sample File 3.txt.....I have about 100 files like this. I am trying to create a block of code or use an available command to a) Create a file b) Put in some... (2 Replies)
Discussion started by: ban3rj33
2 Replies

4. Shell Programming and Scripting

Renaming multiple files at once (discarding suffix of the filename after a character)

Hi, I have a lot of files similar to the below order. I want to rename all the files .discrading the time stamp/numbers after cnf. Existing files id_info_20130405.cnf_20130801 in_info_20130405.cnf_20130891 iz_info_20130405.cnf_20130821 in_info_20130405.cnf_20130818... (2 Replies)
Discussion started by: saravanapandi
2 Replies

5. Shell Programming and Scripting

Insert a header record (tab delimited) in multiple files

Hi Forum. I'm struggling to find a solution for the following issue. I have multiple files a1.txt, a2.txt, a3.txt, etc. and I would like to insert a tab-delimited header record at the beginning of each of the files. This is my code so far but it's not working as expected. for i in... (2 Replies)
Discussion started by: pchang
2 Replies

6. UNIX for Dummies Questions & Answers

Insert a line into multiple files

HI All, I want to know if it is possible to print the same message but into 2 different files in the same command? Something like . .. ... echo "Text" >> file1 && file2 this is because i creating a script which i use a log but i don't want to duplicate lines of command just to... (5 Replies)
Discussion started by: lordseiya
5 Replies

7. Shell Programming and Scripting

Adding filename and line number from multiple files to final file

Hi all, I have 20 files (file001.txt upto file020.txt) and I want to read them from 3rd line upto end of file (line 1002). But in the final file they should appear to start from line 1. I need following kind of output in a single file: Filename Line number 2ndcolumn 4thcolumn I... (14 Replies)
Discussion started by: bioinfo
14 Replies

8. Shell Programming and Scripting

insert filename into each line of multiple files

I need to insert <filename + comma> into each line of multiple files. Any idea how to script that? Regards, Manu (5 Replies)
Discussion started by: linux.yahoo
5 Replies

9. Shell Programming and Scripting

joining multiple files into one while putting the filename in the file

Hello, I know how to join multiple files using the cat function. I want to do something a little more advanced. Basically I want to put the filename in the first column... One thing to note is that the file is tab delimited. e.g. file1.txt joe 1 4 5 6 7 3 manny 2 3 4 5 6 7 ... (4 Replies)
Discussion started by: phil_heath
4 Replies

10. Shell Programming and Scripting

IBM Informix Load and Insert with multiple files

Hi , Can you guys please help as I have list of files xaa, xab, xac.........xza for eg in which to perform load the 1st (xaa) and insert into table, then only proceed for the 2nd , 3rd and so forth. In other words, before 1st one finished, 2nd one shall not load and insert to table, and so... (0 Replies)
Discussion started by: rauphelhunter
0 Replies
Login or Register to Ask a Question