2

I have some set of files with character strings in lines such that there is a folder containing

file1
file2
file3

and within those files there a variable length lists of strings of characters such that file 1 may read

file1.itemA
file1.itemB
file1.itemC

While file 2 may only contain

file2.itemA
file2.itemB

I want to add a file specific code to every line within each file such that

code1.file1.itemA
code1.file1.itemB
code1.file1.itemC

And

code2.file2.itemA
code2.file2.itemB

How can I do this within unix? I am using the OSX terminal to execute commands.

colin
  • 2,606
  • 4
  • 27
  • 57

1 Answers1

3

I'm not near a terminal to test, but, how about:

cd /path/to/your/files
word='code'
base=1
for file in *; do sed -i -e "s/^/$word$base/" "${file}"; base=$(( $base + 1 )); done

The $word variable is the constant you want. The $base variable holds the count that is incremented on each file, initially set to 1.

bishop
  • 37,830
  • 11
  • 104
  • 139
  • Gotcha. can you clarify- what if the file names are not simple file1, file2, etc. but rather 'file01_rep01_other.meaningless.stuff.fastq' 'file02_rep02_other.meaningless.stuff.fastq' – colin Sep 09 '14 at 14:54
  • Should be fine. The file name is not used in the calculation of the code to add, and the file name argument to `sed` is quoted properly. If you get a specific error, post the specific error and as much of the actual files as you are allowed. – bishop Sep 09 '14 at 15:28