how to insert a '#' in the first position of all the files based on a certain condition.
I tried this:
cat /bin/user/input_file.txt | while read a
do
b=`sed 's/.*song=good.*/\#&/g' $a `
echo $b > /bin/user/new/output_file.txt
done
input_file.txt has list of names of 10 files.. the sed condition will check all the files and search for the condition ( song=good ) and then inserts a # and then redirects the output to a new directory and a new file output_file.txt..
when i ran this,i am not able to get the desired output.
Its not in the problem of the code r the command..
and thats not only one file.. it has a set of files which is passed in the loop..the same name in which the file is given the output shud be sent in the same name to a different directory.. thats the prob i have.. moreover will it be different from linux and unix environment..
When you use the $(command) and `command` forms of command substitution, the trailing <newline> character (if there is one) will be dropped from the output. There doesn't seem to be any need to assign the output of sed to a variable; just let sed write directly into your output file. The following would seem to do what you want:
sed 's/.*song=good.*/\#&/' $(cat /bin/user/input_file.txt) > /bin/user/new/output_file.txt
This should work on any Linux or UNIX System as long as you are using a shell (such as a Korn shell [ksh] or bash) that recognizes minimal requirements specified by the POSIX standards and the Single UNIX Specifications as long as the pathnames stored in input_file.txt do not contain any whitespace characters.
Note also that using:
echo $b
to print a line of output will produce different output when using different versions of echo if the variable b contains any tab characters, any occurrences of multiple adjacent spaces, starts with a minus sign, or contains any backslash characters. If you want to copy the contents of a variable to a file adding a <newline> character to the end of it (without altering spacing, leading minus signs, and backslash characters), you could replace the echo command with:
printf "%s\n" "$b"
And, finally, note that using:
echo $b > /bin/user/new/output_file.txt
will only save the output from the last file processed. Each time you use > as a redirection operator in the shell, all previous output in the destination file will be replaced.