Changing Line in Txt File


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Changing Line in Txt File
# 1  
Old 07-31-2012
Changing Line in Txt File

So I have a python program that I run, which runs accordingly to options I have listed in a text file (ie user_prefs). Now there are many options listed in this user_prefs.txt, but the one of most interest to me is that of the file path of the time series.

I have over a hundred of these time series that I would like to run this python program through (which I would rather not do randomly).....My question is how could I possibly write a script a shell script to go through and just change the time series file name and then in a loop I would be running the other python program.

All the time series files have a common suffix, but just a numbered prefix, but the number are not of any pattern, rather random. (ie 843523ascii_vF.txt)

I though of trying to write a fortran program to go in and just replace that line but cant think of the way to write the prefix from the file name to inside the program.


Any help would be greatly appreciated, thanks!

---------- Post updated 07-31-12 at 11:13 AM ---------- Previous update was 07-30-12 at 03:00 PM ----------

I was thinking about possibly constructing a shell script using

Code:
sed -i "" 's/phrase/newphrase/'g' user_prefs.txt

in order to replace the file name, however, i would want the newphrase to be the filename. I know when using a fortran I could do something like

Code:
for File in *.txt (where File new phrase I would like to insert into the txt file)
do
         ./blah.o < "$File" > 
done

so would I be able to similarly do something like this for my problem.

Code:
for File in *.txt 
do
            sed -i "" 's/phrase/"$File"/'g' user_prefs.txt
done

Thanks for the help

Last edited by Scrutinizer; 07-31-2012 at 12:23 PM.. Reason: code tags
# 2  
Old 07-31-2012
What you want is probably possible in awk but you'll need to be more specific.

Show some example input and output, please.
# 3  
Old 07-31-2012
So I have a hundred or so time series files labeled as such:
10831ascii_vF.anom.txt
13165ascii_vF.anom.txt
83437ascii_vF.anom.txt

Now I have a main program to run, which when I run, runs based upon a user_prefs text file which tells the main program which time series to use, and specifies several other options for the main program. However, I am only interested in changing the name of the time series in the varying runs.

So more or less I want to run the main program through for each time series, but want to do so without having to run that program over a hundred times manually each time opening, editing, and closing the user_prefs file.

So all I really need is a command which I could have in a loop to change the name of the time series based upon all the time series within the directory.
So some type of script structured as this:
  • For this time series
    Change User Prefs, replacing old time series with new time series name (just deleting and replacing a line with new file name)
    Run main program coaps.py
    Re Loop


I have never worked with awk so any help would be perfect. Thanks.
# 4  
Old 07-31-2012
Yes, that's what the files are named...

Now, what do the contents look like? And what in them do you want changed?
# 5  
Old 07-31-2012
I want nothing changed in the time series files, the file ####ascii_vF.anom.txt simply contain a single list of 2000 observations.

What I want changed is in the user_prefs file, which mandates how the main program is run.
In there, I want the 266th to be altered, all the other lists, options, should remain the same.

The only file to be altered is my user preferences text file, all the time series text files should remain unaltered.
Just want Path=/User/fjjfjf/fjfjfj/timeseries.txt
to change from one ascii file name to another with each run.
So
Path=/User/fjjfjf/fjfjfj/10831ascii_vF.anom.txt
to
Path=/User/fjjfjf/fjfjfj/13165ascii_vF.anom.txt
to
Path=/User/fjjfjf/fjfjfj/83437ascii_vF.anom.txt
# 6  
Old 07-31-2012
Instead of editing the same file over and over, I'd keep an original template which I work from every time. It's not a good idea to edit your originals; if anything goes wrong, you've lost your data.

Code:
for FILE in *anom.txt
do
        awk '/Path=/ { print "Path=" F; next } 1' F="$FILE" config_template.txt > config.txt
        do_something_with $FILE
done

# 7  
Old 07-31-2012
ya I have the original copied and stored in a few other directories so no problem there, but I will give this a go and let you know

---------- Post updated at 05:10 PM ---------- Previous update was at 11:58 AM ----------

Got it to work using sed, just for the possible future need of others I will post how I got it to work
Code:
for File in *.txt

do

eval "sed -e 's/oldwordstring/"${File}"/' usertemplate.txt>user_prefs.txt"

#Must make output a different file name

done


Last edited by Franklin52; 08-01-2012 at 08:14 AM.. Reason: Please use code tags for data and code samples
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Switch line in txt file

Hi I have problem with replace line in txt file , I have this string: 144185 DISK Piece qqr8ot6l_1_1 -- 144186 DISK Piece ukr8pf2e_1_1 -- 144187 DISK Piece ter8p9gc_1_1 -- 144188 DISK Piece 4er8qb84_1_1 and (8 Replies)
Discussion started by: primo102
8 Replies

2. UNIX for Dummies Questions & Answers

Split Every Line In Txt Into Separate Txt File, Named Same As The Line

Hi All Is there a way to export every line into new txt file where by the title of each txt output are same as the line ? I have this txt files containing names: Kandra Vanhooser Rhona Menefee Reynaldo Hutt Houston Rafferty Charmaine Lord Albertine Poucher Juana Maes Mitch Lobel... (2 Replies)
Discussion started by: Nexeu
2 Replies

3. Shell Programming and Scripting

Need to append the date | abcddate.txt to the first line of my txt file

I want to add/append the info in the following format to my.txt file. 20130702|abcd20130702.txt FN|SN|DOB I tried the below script but it throws me some exceptions. <#!/bin/sh dt = date '+%y%m%d'members; echo $dt+|+members+$dt; /usr/bin/awk -f BEGIN { FS="|"; OFS="|"; } { print... (6 Replies)
Discussion started by: harik1982
6 Replies

4. Shell Programming and Scripting

create txt file form data file and add some line on it

Hi Guys, I have file A.txt File A Data AK1521 AK2536 AK3164 I want create text file of all data above and write some data on each file. want Output on below folder /home/kka/out AK1521.txt Hi Welocme (3 Replies)
Discussion started by: asavaliya
3 Replies

5. Shell Programming and Scripting

Count per line in txt file

In a txt file called, eso.txt, I have: ...... 3 where process_status_flag = 70 and LISTENER_ID in (930.00, 931.00, 932.00, 933.00, 934.00) 4 group by LISTENER_ID 5 order by LISTENER_ID; LISTENER COUNT ----------... (3 Replies)
Discussion started by: Daniel Gate
3 Replies

6. Shell Programming and Scripting

write filename as first line in a txt file

Could anyone very kindly help me a simple way to perform the - perhaps - very trivial task of writing the name of a file as first line of that file which is in txt format? And would be possible to do this recursively for some thousands files in the XY directory? And, again, add to the simple... (3 Replies)
Discussion started by: mjomba
3 Replies

7. Programming

Reading a particular line from a .txt file

Hi, I have a .txt file which contains the x, y and z co-ordinates of particles which I am trying to cast for a particular compound. The no. of particles present is of the order of 2 billion and hence the size of the text file is of the order of a few Gigabytes. The particles have been casted layer... (5 Replies)
Discussion started by: mugga
5 Replies

8. Shell Programming and Scripting

Changing txt

Hi all, I currently use this code to delete text from file names when they are dropped in a folder. find . -type f | while read i;do && mv "$i" "${i//2009abc/}" ;done I have four versions of the code that delete the unwanted text if it has 2006, 2007, 2008 and 2009 with other standard... (4 Replies)
Discussion started by: Monkey Dean
4 Replies

9. Shell Programming and Scripting

i need to read the last line in a txt file

i'm a beginner in shell and i have a txt file that is updating every second or msec so i need a program to read the last line of this txt file is this possible to do? (5 Replies)
Discussion started by: _-_shadow_-_
5 Replies

10. UNIX for Dummies Questions & Answers

How to read last line of a txt file?

I need to read the last file for a particular day, such as, "Jun 13" because the CSV file is cumulative for the entire day, so I don't want all the previous files, I just want the last file, for that day. I ran an 'ls -al | grep "June 13" > myLs.txt' (simplified) to list all files from that day.... (2 Replies)
Discussion started by: yongho
2 Replies
Login or Register to Ask a Question