0

I need to generate new files by modifying a string in an existing file.

I have a text file called newheader.txt, it looks like this:

@RG     ID:$FILENAME     SM:$FILENAME

I also have a text file called filenames.txt that looks like this:

ZF-150-81_S49
ZF-150-82_S55
ZF-150-83_S59

I would like to have a for loop to go through filenames.txt one line at a time and replace the $FILENAME string in newheader.txt with the line in filenames.txt and make each of these into a new .txt file with that line in the title.

This would create three output files. For example the first output file would be called ZF-150-81_S49.header.txt and inside it should look like this:

@RG     ID:ZF-150-81_S49     SM:ZF-150-81_S49

In other words something like:

for $FILENAME in filename.txt; do replace $FILENAME in newheader.txt > $FILENAME.header.txt ; done

I have done bash for loops but not where it requires replacing a string in a text file. Happy for other solutions such as perl if this makes more sense. Thanks for your assistance.

1
  • sed is the generic replace command. Commented Jan 22, 2019 at 20:47

2 Answers 2

2

something like this... (not tested)

$ while read -r f; do sed 's/$FILENAME/'"$f"'/g' header > "$f".header; done < filename.list
Sign up to request clarification or add additional context in comments.

Comments

2

Use

for f in $(cat filename.txt); do
  sed -e "s/\$FILENAME/${f}/g" newheader.txt >${f}.header.txt
done

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.