Adding a line to 1000's of files right after x amt of characters.


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Adding a line to 1000's of files right after x amt of characters.
# 1  
Old 09-22-2012
PHP Adding a line to 1000's of files right after x amt of characters.

I am trying to add a single line of text to every file in a particular folder. There are thousands of files in the folder.

Each file contains this same start of the first line:
{d "%w- %d %m-, %Y - %T"} <some message here>

with the rest of the text following the second curly bracket (where <some message here> is present).

I would like to add this right after the right curly bracket on the first line:
Model_Type={t}, ModelName={m}

(with the option of adding a CRLF after {m})

So the result would look like:
{d "%w- %d %m-, %Y - %T"} ModelType={t}, ModelName={m} <some message here>

Any ideas? MANY THANKS!!!

Moderator's Comments:
Mod Comment Please view this code tag video for how to use code tags when posting code and data.

Last edited by vbe; 09-22-2012 at 06:22 PM..
# 2  
Old 09-22-2012
Try this..

Code:
sed 's/}/}Model_Type={t}, ModelName={m}/1' file

This User Gave Thanks to pamu For This Post:
# 3  
Old 09-22-2012
Assuming that only the first line in each file is the one to be checked for replacement, try:
Code:
cd /your/directory
find . -type f -name 'Event*' -exec perl -pi.bak -e 'if($ARGV ne $prev){$prev=$ARGV;s/(\{.*?\})/\1 Model_Type={t}, ModelName={m}/}' {} +

This will edit your files "in-place" with a back-up of each file kept with .bak extension.

Last edited by elixir_sinari; 09-22-2012 at 06:12 PM..
This User Gave Thanks to elixir_sinari For This Post:
# 4  
Old 09-22-2012
Data

Quote:
Originally Posted by pamu
Try this..

Code:
sed 's/}/}Model_Type={t}, ModelName={m}/1' file


I'm assuming I can put this line in a script so it might look like:

#!/bin/sh

# read files
cat ${WORKFILE} | while read file
do
sed 's/}/}Model_Type={t}, ModelName={m}/1' $file
done

NOTE: ${WORKFILE} is a text file that has the listing of all the files I want to edit.

---------- Post updated at 02:46 PM ---------- Previous update was at 02:32 PM ----------

After running the above script it is placing "ModelType={t}, ModelName={m}" after EVERY right curly bracket in the file. There are multiple variables in each file, which look like this: {v}. So I am getting multiple incertions in each file.

---------- Post updated at 02:52 PM ---------- Previous update was at 02:46 PM ----------

Quote:
Originally Posted by pamu
Try this..

Code:
sed 's/}/}Model_Type={t}, ModelName={m}/1' file


After putting this in a loop I can see it do two things.
1st: I am getting output to the screen, but it is not writting the addition to the file Smilie

2nd: The code is adding multiple instances of "Model_Type={t}..." for each file may contain multple instances of variables, which look like: {v} where "v" is the variable letter., so, In the screen output it is adding "Model_Type=..." every time it sees another right curley bracket.

My entire script looks like this:
Code:
#!/bin/sh
####### MAIN SCRIPT #################
host=$1
DATE=`date '+%m%d%y_%H%M'`
CLISESSID=$$
SPECROOT=d:/
SCRIPTHOME=${SPECROOT}/custom/scripts/TestEventFolder
WORKFILE=${SCRIPTHOME}/Event_OneLine.txt
#WORKFILE=${SPECROOT}/tmp/worklist.txt
LOGFILE=${SCRIPTHOME}/LOGFILE_${DATE}.txt
SEARCHDIR=${SPECROOT}/Events
CLIMNAMEWIDTH=64
CLIPATH=${SPECROOT}/vnmsh
export CLISESSID CLIMNAMEWIDTH CLIPATH host DATE SPECROOT SCRIPTHOME WORKFILE LOGFILE
 
cd ${SCRIPTHOME}
 
# get event files
ls Event* > ${WORKFILE}
 
# read files
cat ${WORKFILE} | while read file
do
sed 's/}/}Model_Type={t}, ModelName={m}/1' $file
done

Screen Output:

d "%w- %d %m-, %Y - %T"}Model_Type={t}, ModelName={m} - A "caSwitchLinkUpTrap" event has occurred, from {t} device, named {m}.
caSwitchLinkUp trap signifies that the caSwitchIfOperStatus object for a switch interface left the down state and transitioned into
te). This other state is indicated by the included value of caSwitchIfOperStatus.
aSwitchIfSwitchIndex = {I 1}Model_Type={t}, ModelName={m}
aSwitchIfIndex = {I 3}Model_Type={t}, ModelName={m}
aSwitchIfAdminStatus = {T caSwitchIfAdminStatus 4}Model_Type={t}, ModelName={m}
aSwitchIfOperStatus = {T caSwitchIfOperStatus 5}Model_Type={t}, ModelName={m}
event [{e}Model_Type={t}, ModelName={m}])


As can be seen, when the script is run it is looking for the right curley bracket in EACH LINE of the file. Is there a way to have the sed operation look at the FIRST LINE ONLY?

Moderator's Comments:
Mod Comment Please view this code tag video for how to use code tags when posting code and data.

Last edited by vbe; 09-22-2012 at 06:25 PM.. Reason: Adding information.
# 5  
Old 09-22-2012
Quote:
Originally Posted by dlundwall
After putting this in a loop I can see it do two things.
1st: I am getting output to the screen, but it is not writting the addition to the file Smilie

2nd: The code is adding multiple instances of "Model_Type={t}..." for each file may contain multple instances of variables, which look like: {v} where "v" is the variable letter., so, In the screen output it is adding "Model_Type=..." every time it sees another right curley bracket.
1. Nothing extraordinary. sed is a filter; it will not change its input source. Rather it will work on a copy of the input, line-by-line, and produce output. You may redirect that output to a temporary file and then rename that temporary file with the original file. That is actually done by the perl code I'd suggested.

2. That's because that sed command is faulty (according to your requirement). It's replacing the first occurrence of the closing curly brace with whatever you wanted on every line.

Why don't you try what I've suggested?

EDIT:

Replace this section of your code:
Code:
cd ${SCRIPTHOME}

# get event files
ls Event* > ${WORKFILE}
 
# read files
cat ${WORKFILE} | while read file
do
sed 's/}/}Model_Type={t}, ModelName={m}/1' $file
done

with
Code:
cd ${SCRIPTHOME}
perl -pi.bak -e 'if($ARGV ne $prev){$prev=$ARGV;s/(\{.*?\})/\1 Model_Type={t}, ModelName={m}/}' Event*


Last edited by elixir_sinari; 09-22-2012 at 06:06 PM..
# 6  
Old 09-22-2012
Question Additional info...

Quote:
Originally Posted by elixir_sinari
1. Nothing extraordinary. sed is a filter; it will not change its input source. Rather it will work on a copy of the input, line-by-line, and produce output. You may redirect that output to a temporary file and then rename that temporary file with the original file. That is actually done by the perl code I'd suggested.

2. That's because that sed command is faulty (according to your requirement). It's replacing the first occurrence of the closing curly brace with whatever you wanted on every line.

Why don't you try what I've suggested?

Sorry about not trying it.... I just took the first suggestion and went with it.
Follow up Question for you:
Can your method be placed in a script so that I can use a loop function, or will the command line version simply do what I want to do for every file?

Thanks for your help!!!!!
# 7  
Old 09-22-2012
Check Post #5 again (I've added some things).
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Creating a sequence of numbers in a line for 1000 files

Hi, I try to explain my problem , I have a file like this: aasdsaffsc23 scdsfsddvf46567 mionome0001.pdb asdsdvcxvds dsfdvcvc2324w What I need to do is to create 1000 files in which myname line listing a sequence of numbers from 0001 to 1000. So I want to have : nomefile0001.txt that must... (10 Replies)
Discussion started by: danyz84
10 Replies

2. UNIX for Advanced & Expert Users

Help in adding a string at the end of each line and append files vertically

hi, i need a help in the script , need to append a string at the end of each line of a files , and append the files into a single file vertically. eg file1 has the following columns abc,def,aaa aaa,aa,aaa files 2 has the following rows and columns abc,def,aaa aaa,aa,aaa i... (3 Replies)
Discussion started by: senkerth
3 Replies

3. 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

4. Shell Programming and Scripting

Adding info to end of line if two columns match from files with different separators

I have two files (csv and vcf) which look exactly like this S1.csv func,gene,start,info "exonic","AL","2309","het" "exonic","NEF","6912","hom"S1.vcf ##fileinfo #CHROM POS ID INFO chr1 4567 rs323211 1/1:84,104,99 chr4 2309 rs346742 1/1:27,213,90 chr6 5834 ... (5 Replies)
Discussion started by: Sarah_19
5 Replies

5. Shell Programming and Scripting

Need help in column comparison & adding extra line to files

Hi, I wanted to check whether the x,y,z coordinates of two files are equal or not. At times, when one file is converted to another suitable file extension , there are some chances that the data mismatch would happen during the conversion. In order to avoid the data misfit, i would like to... (6 Replies)
Discussion started by: b@l@ji
6 Replies

6. Shell Programming and Scripting

adding characters end of line where line begins with..

Hi all, using VI, can anyone tell me how to add some characters onto the end of a line where the line begins with certain charactars eg a,b,c,......., r,s,t,........, a,b,c,......., all lines in the above example starting with a,b,c, I want to add an x at the end of the line so the... (6 Replies)
Discussion started by: satnamx
6 Replies

7. Shell Programming and Scripting

retain last 1000 line in a file

I have large file with around 100k+ lines. I wanted to retain only the last 100 lines in that file. One way i thought was using tail -1000 filename > filename1 mv filename1 filename But there should be a better solution.. Is there a way I can use sed or any such command to change the... (9 Replies)
Discussion started by: nss280
9 Replies

8. Shell Programming and Scripting

Adding characters at the top of all files in directory

Hi Unix experts; I have 30000 files in a directory and am willing to do the following changes on each of them. The input files look like the following: 1 , 2 3 , 4 5 , 6 7 , 8 9 , 10 the output will have # in top 10 lines, insert space instead of comma. This looks like: ... (4 Replies)
Discussion started by: nxp
4 Replies

9. UNIX for Dummies Questions & Answers

Displays line number 1000 to 2000

Hi, I have 1 million records and want to extract lines betwen 10000 -20000 and put it in another file. Could you please suggest a command for this. Thanks in advance (3 Replies)
Discussion started by: unxusr123
3 Replies

10. Shell Programming and Scripting

Command to add 1000 spaces to end of line

hi, could anyone tell me the command to append spaces at the end of the line. for example, i need 1000 spaces after the word "helloworld" echo "helloworld " i need to achieve this in someother way hardcoding 1000 spaces is not practical. as i am totally new... (3 Replies)
Discussion started by: kavithacs
3 Replies
Login or Register to Ask a Question