How to insert a '#' in the first position of all the files?

Thread Tools Search this Thread
Top Forums Shell Programming and Scripting How to insert a '#' in the first position of all the files?
# 1  
Old 08-10-2013
Reference How to insert a '#' in the first position of all the files?

Hi All,

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 
b=`sed 's/.*song=good.*/\#&/g' $a `
echo $b > /bin/user/new/output_file.txt

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.

required output:

file 1:
1.elvis presly 1,5,6,song=bad;

output which i am getting:

1.elvis presly 1,5,6,song=bad; #2.richie,2,6,8,song=good; 3.benaud,3,9,8,song=bad;

please help me on this

Last edited by Don Cragun; 08-11-2013 at 12:22 AM.. Reason: Add CODE and ICODE tags.
# 2  
Old 08-10-2013
sed 's/.*song=good;/#&/' input.txt > output.txt

# 3  
Old 08-10-2013
Using awk
awk '/song=good/ {$0="#"$0} 1' infile

# 4  
Old 08-10-2013

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..
# 5  
Old 08-11-2013
Do you want the output of all the files in input_file.txt to be in one single file? output_file.txt?

The following code will store the files after modification to a new directory with its respective file names suffixed with the extension "new"
while read line
  sed 's/.*song=good;/#&/' $line > /bin/user/new/$
done < /bin/user/input_file.txt

# 6  
Old 08-11-2013
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.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

How to insert a string and variable at specified position in command in bash?

I currently have a loop that reads all .bam files in a directory (wont always be 4 like in this example, into $id. What I am trying to do, unsucessfully, is create specific new lines in an exsisting command using each $id. Each new line would be: --bam ${id} \ Tried p=$dir... (8 Replies)
Discussion started by: cmccabe
8 Replies

2. UNIX for Dummies Questions & Answers

Using sed to insert at position x and then every interval y for each line

Thanks to help from Don Cragun in post 302924174, I'm off and getting into trouble on my own (finally) with sed. Here is my goal - insert \\r\n at the 60th character on each line and then every 76th character thereafter: Input:... (3 Replies)
Discussion started by: gusbrown
3 Replies

3. Shell Programming and Scripting

Insert | in specific position

Hi , I have a file which has line similar to below 13123324234234234234234234234234234 3454546456dfhgfhgh454645654asdasfsdsddfgdgdfg 345345345mnmnbmnb346mnb4565464564564645645 Not for each line for specific position I need to insert some '|' Positions are fixed. Like 3,5,9,11 So the... (5 Replies)
Discussion started by: Anupam_Halder
5 Replies

4. Shell Programming and Scripting

insert character in particular position.

I want to insert space in 7th position of all the lines usign vi editor or sed command Input file 12345689010 abcdefghijk . . Output file 123456 89010 abcdef ghijk . . (7 Replies)
Discussion started by: Jairaj
7 Replies

5. Shell Programming and Scripting

Insert character in a specific position of a file

Hi, I need to add Pipe (|) at 5th and 18th position of all records a file. How can I do this? I tried to add it at 5th position using the below code. It didnt work. Please help!!! awk '{substr($0,5,1) ~ /|/}{print}' $input_file > $temp_file (1 Reply)
Discussion started by: gpaulose
1 Replies

6. Shell Programming and Scripting

search a line and insert string into specific at position

Hi, guys. I have one question: How can I search for a line with certain string in it and then insert a string into this line? For example: There is a file called shadow, the contents of it are below: ************************** ... yuanz:VIRADxMsadfDF/Q:0:0:50:7:::... (9 Replies)
Discussion started by: daikeyang
9 Replies

7. Shell Programming and Scripting

insert server name in position 1 on each line

Hello, It has been a long time since I have written unix code and I need to insert a variable into the first position of each line in a file. Below is an example of the script and the desired output file Here is my short script server="$(hostname)" df -kg | awk '{print $1, $2, $3, $4,... (3 Replies)
Discussion started by: rlc198842
3 Replies

8. Shell Programming and Scripting

How to insert at a particular position in flat file

Hi All, I have a flat file with ~ as de-limiter (e.g: aaa~ba a~caa~0~d~e) What I want is check if the 4th character is 0 and replace it with say 4. So now it becomes : aaa~ba a~caa~4~d~e. I have to do this for the whole file, but the delimiter position remains the same, not the... (10 Replies)
Discussion started by: akdwivedi
10 Replies

9. HP-UX

find the position in a file and insert the data there

Hi, I have a EDI data file ARROWTEST of size 18246 characters. And I want to insert some data after 4200 position in the file. How I can find the position 4200 in that file....Please advise. Regards, (5 Replies)
Discussion started by: isingh786
5 Replies

10. Shell Programming and Scripting

How to insert strings at certain position

Hi, I need to insert strings "0000 00" at the each line within the file. The postion is 37 to 42. ex. name1 name2 0000 00 nam name 0000 00 The "0000 00" in two lines should be lined up. I don't know why it's not lined up when I posted it. Can anyone help? (14 Replies)
Discussion started by: whatisthis
14 Replies
Login or Register to Ask a Question