LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   Bash - add text at the begining of a line (https://www.linuxquestions.org/questions/linux-newbie-8/bash-add-text-at-the-begining-of-a-line-865529/)

LoDam 02-28-2011 12:05 PM

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
  2 LIST=$(ls)
  3 
  4 for folder in $LIST
  5 do
  6    if [ -d $folder ]
  7        then
  8            cd $folder
  9            cat stddev.dat >> ../stddev
 10            cd ..
 11    fi
 12 done


Stephen Morgan 02-28-2011 01:06 PM

#!/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.

LoDam 02-28-2011 01:37 PM

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:


#!/bin/bash
  2 LIST=$(ls)
  3
  4 if [ -f "stddev.dat" ]
  5    then
  6    rm stddev.dat
  7 fi
  8
  9 for folder in $LIST
 10    do
 11        if [ -d $folder ]
 12            then
 13            cd $folder
 14            line=$(cat stddev.dat)
 15            line=$folder" "$line
 16            echo $line >> ../stddev.dat
 17            cd ..
 18        fi
 19    done

That was quitte simple actually!

Cheers,
Loic.

Stephen Morgan 02-28-2011 03:56 PM

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'
todaywoof

The '<' sign pipes the file into the command. You are assigning a variable and reading the contents of the file into it with the cat command. Say you wanted to replace all the 'a's in a one line file with 'b's, you could do it like this:

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.

kurumi 02-28-2011 04:02 PM

Quote:

Originally Posted by Stephen Morgan (Post 4274240)
#!/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.

don't have to specifically substitute the whole line. s/^/ word/ will do

LoDam 03-01-2011 12:26 AM

Thank's stephen morgan, cristal clear now!

sumeet inani 03-01-2011 03:43 AM

Try this
Code:

find . -type f -iname 'stddev.dat' -printf "\n%h:" -exec 'cat' '{}' ';'
Assuming you are currently in folder containing all subfolders.


All times are GMT -5. The time now is 04:56 AM.