Bash - add text at the begining of a line
Hello LQ,
A very simple question for you: Here is the situation. I have a lot a folders, each named by a number, and in each of these folders I have a specific file (stddev.dat) containing a single line (of numbers) I need to have a single file with each line being one of the stddev.dat (no matter if it is sorted or not), and also I need to add at the begining of each line the number of the folder it comes from. I 'm no bash expert, and the "add at the begining of the line" is a bit of problem to me". Here is what I've come up with so far, just to put everything in one file, can you help me with the rest (and also if you know a better/more elegant way to do the same thing I've done, I'm listening ;) ) Code:
|
#!/bin/bash
LIST=$(ls) for folder in "$LIST"; do if [ -d "$folder" ]; then cd "$folder" sed 's/^.*/^'"$folder"'&/1' < stddev.dat >> ../stddev cd .. fi done # I think that'll work. |
Thank you.
Could you explain the sed command? I don't understand the &1 < stddev.dat part. Otherwise I came up with a solution without sed (I don't like using solutions I don't really understand ^^) Code:
Cheers, Loic. |
I'm no expert on sed, but '^' denotes the start of the line, '&' the string searched for, '.' any character and '*' anything, or nothing. So s/^.*/today&/ ought to substitute the start of the line followed by at least one character (so as to ignore blank lines), replacing it with the same thing but with "today" in front of it. The one at the end, I think, means to only do it once, but I'm not so sure about that. As all your files are meant to have one line, I believe you said, that would do the trick but be unneccessaru./
Code:
grim@21:54:16:~$ echo woof | sed 's/^.*/today&/1' cat file.in | sed 's/a/b/g' >file.out or like this: sed 's/a/b/g' < file.in >> file.out Or by assigning a variable: var="$(cat file.in)" echo "$var" | sed 's/a/b/g' >> file.out All do the same thing. |
Quote:
|
Thank's stephen morgan, cristal clear now!
|
Try this
Code:
find . -type f -iname 'stddev.dat' -printf "\n%h:" -exec 'cat' '{}' ';' |
All times are GMT -5. The time now is 09:44 AM. |