Prepending information from filenames into files

Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Prepending information from filenames into files
# 8  
It seems that in Mac OS, when sed is used with -i, the backup extension is required, not optional, so we'll use an empty string for that in order to ingore backups. Change the sed line to:
sed -i '' "s/^/${i} ${j} /g"  height_${i}_width_${j}.txt

Please let us know how it works for you.
# 9  
Ah, now it worked! Thanks for your help and time, ciupinet! Smilie
# 10  
Originally Posted by ciupinet
sed -i '' "s/^/${i} ${j} /g"  height_${i}_width_${j}.txt

A nit: In this case, the global flag is meaningless since the pattern is anchored.

A different approach, for kicks:

for f in height_*_width_*.txt; do
    temp=${f#*_}; temp=${temp%.txt}
    h=${temp%%_*}; w=${temp##*_}
    printf '%s\n' "1,$ s/^/$h $w /" w q | ed -s "$f"

# 11  
Ok, so since that all worked fine and good, I tried applying the same script idea to some actual research data of mine. It mostly worked, but instead of the values being prepended to the beginning of each line, the array symbols were prepended. Here is the whole script:


x=( 0.0025 0.025 0.5 0.125 0.25 0.5 0.75 1 1.25 1.5 2 2.5 )

indpop=( 1 2 4 6 8 10 12 14 16 18 20 )

length=( 250 500 750 1000 )

ntree=( 10 25 50 75 100 )

for d in "${x[@]}"; do
for p in "${indpop[@]}"; do
for l in "${length[@]}"; do
for t in "${ntree[@]}"; do
if [ -f nohead_mod_seqdiv_+_treedist_BP_${l}_GeneTrees_BranchLength_${d}_IndPop_${p}_ntree_${t}.txt ]; then
echo "Processing file nohead_mod_seqdiv_+_treedist_BP_${l}_GeneTrees_BranchLength_${d}_IndPop_${p}_ntree_${t}.txt"
sed -e 's/^/${l} ${d} ${p} /g' < nohead_mod_seqdiv_+_treedist_BP_${l}_GeneTrees_BranchLength_${d}_IndPop_${p}_ntree_${t}.txt > final_seqdiv_+_treedist_BP_${l}_GeneTrees_BranchLength_${d}_IndPop_${p}_ntree_${t}.txt
echo "File nohead_mod_seqdiv_+_treedist_BP_${l}_GeneTrees_BranchLength_${d}_IndPop_${p}_ntree_${t}.txt does not exist"
And here is a line from one of the result files:

${l} ${d} ${p} 0.05013333333 0 0.0467095461669496 0.044 4
For some reason, when executing the sed command it won't recognize the arrays, and I'm not sure why.
# 12  
I didn't really read your code, but the sed command is strongly quoted. You want to use the weaker double quotes for that.
# 13  
Ok, the double quotes worked. Thanks!
# 14  
I think perl is installed by default. Try this and let me know if it works.

perl -lane 'print "$1 $2 $F[0] $F[1]" if($ARGV=~/(\d+)\_\w+\_(\d+)/)' height_2_width_1.txt


Previous Thread | Next Thread
Thread Tools Search this Thread
Search this Thread:
Advanced Search

Test Your Knowledge in Computers #829
Difficulty: Medium
The JSON standard supports object references.
True or False?

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Renaming files with Spaces in Filenames

Entry level scripter. Any help appreciated. for file in *; do rename '4321_' '' $file ; done Doesn't work for files with spaces in between FOr eg 4321_1004.dat is renamed to 1004.dat but 4321_1004 2008.dat stays the same (1 Reply)
Discussion started by: davnavin
1 Replies

2. Shell Programming and Scripting

Sed script for appending & prepending

Hello Mates I am trying to write a script, which appends and prepends the text in a file. I tried testing with a small file and it worked fine. but for the large file, the script is wiping the entire file and adds only the word to be appended in the file. mv $file_name $file_name.bak sed... (6 Replies)
Discussion started by: sathyaac
6 Replies

3. UNIX for Dummies Questions & Answers

Prepending lines with file name

I have a number of dat-files, such as abc.dat, def.dat etc, as follows: 2011-07-01 100.0 2011-07-02 101.0 2011-07-03 101.7 I want to prepend the file with the base file name; so for abc.dat it would result in the following: abc 2011-07-01 100.0 abc 2011-07-02 101.0 abc 2011-07-03 ... (5 Replies)
Discussion started by: figaro
5 Replies

4. Shell Programming and Scripting

Sorting a list of filenames but keeping the path information.

Hi All I've googled around for this and can't see a way of doing it. I have a file that contains a number of records that are layed out something like the following. /path/to/directory/that/contains/a/file/I/need/filename.pdf The path itself can vary both in terms of the names and the... (7 Replies)
Discussion started by: Bashingaway
7 Replies

5. Shell Programming and Scripting

Redirection after prepending timestamp

Hi all, I have little experience with Scripting so hoping someone may be able to help me or point me in the right direction. I have a shell script which was outputting uncaught exceptions to a log file. $JAVA_MAIN_CLASS > $LOGNAME 2>&1 What I want to be able to do is prepend a timestamp on... (8 Replies)
Discussion started by: MacAonghusa
8 Replies

6. AIX

How to grep filenames from a file in other files?

Hi, We have a file called applcust.txt where we keep records of our customizations in the following format. We want to verify whether the file, which is in 3rd column in this file is there in another file called patch999999.log to find out if any of our customization has got overwritten by the... (2 Replies)
Discussion started by: sshah1001
2 Replies

7. Shell Programming and Scripting

Conditionally prepending text

I am currently writing a script to compare a file list created over an FTP connection to a local directory. I have cleaned the FTP file list up so that I just have a raw list of filenames however due to the directory structure employed (both locally and on the ftp site) I need to prepend each line... (6 Replies)
Discussion started by: Dal
6 Replies

8. Shell Programming and Scripting

copy all files with the same filenames as those in another folder

Hi, all: I've got two folders, folder A contains some image files (say, 100 files) in .jpg format; folder B contains all description files (say, 500 files) in .txt format. All image files in folder A are able to find their corresponding description files in folder B. That is to say,... (3 Replies)
Discussion started by: jiapei100
3 Replies

9. Shell Programming and Scripting

Getting a list of filenames of moved files

I'm moving a list of files of some extension and I wish to output the moved filenames into a text file, I tried using the command below, but after all the files are moved, I got a blank file. find /abc/temp -type f -mtime +365 \( -name "*.bak" -o -name "*.log" \) -exec mv -f {} /junk \; >>... (3 Replies)
Discussion started by: chengwei
3 Replies

10. Shell Programming and Scripting

Finding pattern & prepending a line with text

Hello Dudes, I have a task to make a unix shell script that should search for a specific TEXT in a file.If that TEXT is found, shell script should add a comment statement before that TEXT line. Ex : LINE 1 xxxxx LINE 2 xxxx CALL xxxx LINE 3 xxxx PERFORM UNTIL if i... (1 Reply)
Discussion started by: kirrushna
1 Replies

Featured Tech Videos