How to echo "#" to files?

Thread Tools Search this Thread
Top Forums Shell Programming and Scripting How to echo "#" to files?
# 1  
Old 10-16-2013
How to echo "#" to files?

I'm writing a shell script that's going to copy the contents of some files to other files line by line. Suppose there're lines starting with #, such as #include<stdio.h> in .c files, when I tried to extracte that line with sed and echo it to another file, it couldn't work. I tried echo "#include<stdio.h>" >> myls.c in the shell, it couldn't write that content into myls.c. I think it's a parsing problem. Anyone know how to fix it?

PS: Suppose due to some requirements, I have to sed the lines and pipe them to echo, with which I write the extracted lines to another file.
# 2  
Old 10-16-2013
For me it works fine :
$ echo "#include<stdio.h>" >> myls.c
$ cat myls.c

On which OS are you running?
Please provide a full example that demonstrate the issue
This User Gave Thanks to ctsgnb For This Post:
# 3  
Old 10-16-2013
Hi, I also tested it today and it works fine. But here I actually want to make it automatic, which means once take a file, we can go through each line of it, extract the line and echo it to another file. Here's the code:

           echo "#!/bin/bash" >  
           number=`wc -l < $i`  #count the number of lines
           while [ $count -le $number ]
                 tmp=`sed -n "$count,1p" $i`   #extract the $count-th line
                 echo "echo $tmp" >> $i" >>

As you can see, the above code generates commands that will recreate file, and exports these commands to another script called So when is run, is supposed to be generated. but if we encountered some special characters such as # and ; in (the file to be recreated somewhere else), the second echo in echo "echo $tmp >> $i" >> won't be able to pass $tmp to $i because this command outputs: echo #contents or contents; >>, codes after # will be interpreted as comments and ; will be interpreted as end of the line. Haven't found a way to figure it out yet. Many thanks.

Last edited by Yongfeng; 10-16-2013 at 04:28 PM..
# 4  
Old 10-16-2013
Try echo "echo \"${tmp}\" >> $i" >>

EDIT: But if a line in has double-quotes in it you're still going to have a problem. You could transform the line first to escape any double-quotes (e.g. tmp=$(sed -n "$count,1p" $i | sed 's/"/\\"/g')).

Last edited by CarloM; 10-16-2013 at 08:10 PM.. Reason: Quotes
This User Gave Thanks to CarloM For This Post:
# 5  
Old 10-16-2013
Moderator's Comments:
Mod Comment This is the second homework violation for part of the same homework project. As you have been told in private mail twice, homework must be filed in the Homework and Coursework Questions forum and must follow the rules specified here.

This thread is closed.
This User Gave Thanks to Don Cragun For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Bash script - Print an ascii file using specific font "Latin Modern Mono 12" "regular" "9"

Hello. System : opensuse leap 42.3 I have a bash script that build a text file. I would like the last command doing : print_cmd -o page-left=43 -o page-right=22 -o page-top=28 -o page-bottom=43 -o font=LatinModernMono12:regular:9 some_file.txt where : print_cmd ::= some printing... (1 Reply)
Discussion started by: jcdole
1 Replies

2. Shell Programming and Scripting

Delete all log files older than 10 day and whose first string of the first line is "MSH" or "<?xml"

Dear Ladies & Gents, I have a requirement to delete all the log files in /var/log/test directory that are older than 10 days and their first line begin with "MSH" or "<?xml" or "FHS". I've put together the following BASH script, but it's erroring out: for filename in $(find /var/log/test... (2 Replies)
Discussion started by: Hiroshi
2 Replies

3. Shell Programming and Scripting

tcsh - understanding difference between "echo string" and "echo string > /dev/stdout"

I came across and unexpected behavior with redirections in tcsh. I know, csh is not best for redirections, but I'd like to understand what is happening here. I have following script (called out_to_streams.csh): #!/bin/tcsh -f echo Redirected to STDOUT > /dev/stdout echo Redirected to... (2 Replies)
Discussion started by: marcink
2 Replies

4. AIX

echo $varibla | mail -s "subject" "" not ruuning as expected

Hi Folks, As per the subject, the following command is not working as expected. echo $variable | mail -s "subject" "" Could anyone figure it out whats wrong with this. I am using AIX box. Regards, (2 Replies)
Discussion started by: gjarms
2 Replies

5. Shell Programming and Scripting

awk command to replace ";" with "|" and ""|" at diferent places in line of file

Hi, I have line in input file as below: 3G_CENTRAL;INDONESIA_(M)_TELKOMSEL;SPECIAL_WORLD_GRP_7_FA_2_TELKOMSEL My expected output for line in the file must be : "1-Radon1-cMOC_deg"|"LDIndex"|"3G_CENTRAL|INDONESIA_(M)_TELKOMSEL"|LAST|"SPECIAL_WORLD_GRP_7_FA_2_TELKOMSEL" Can someone... (7 Replies)
Discussion started by: shis100
7 Replies

6. Shell Programming and Scripting

With that logic this echoes "echo". Question about echo!

echo `echo ` doesn't echoes anything. And it's logic. But echo `echo `echo ` ` does echoes "echo". What's the logic of it? the `echo `echo ` inside of the whole (first) echo, echoes nothing, so the first echo have to echo nothing but echoes "echo" (too much echoing :P):o (2 Replies)
Discussion started by: hakermania
2 Replies

7. Shell Programming and Scripting

"Join" or "Merge" more than 2 files into single output based on common key (column)

Hi All, I have working (Perl) code to combine 2 input files into a single output file using the join function that works to a point, but has the following limitations: 1. I am restrained to 2 input files only. 2. Only the "matched" fields are written out to the "matched" output file and... (1 Reply)
Discussion started by: Katabatic
1 Replies

8. Shell Programming and Scripting

Delete files older than "x" if directory size is greater than "y"

I wrote a script to delete files which are older than "x" days, if the size of the directory is greater than "y" #!/bin/bash du -hs $1 while read SIZE ENTRY do if ; then find $1 -mtime +$2 -exec rm -f {} \; echo "Files older than $2 days deleted" else echo "free Space available"... (4 Replies)
Discussion started by: JamesCarter
4 Replies

9. Shell Programming and Scripting

Difference between using "echo" builtin and /bin/echo

So in my shell i execute: { while true; do echo string; sleep 1; done } | read line This waits one second and returns. But { while true; do /bin/echo string; sleep 1; done } | read line continues to run, and doesn't stop until i kill it explicitly. I have tried this in bash as well as zsh,... (2 Replies)
Discussion started by: ulidtko
2 Replies

10. Shell Programming and Scripting

"sed" to check file size & echo " " to destination file

Hi, I've modified the syslogd source to include a thread that will keep track of a timer(or a timer thread). My intention is to check the file size of /var/log/messages in every one minute & if the size is more than 128KB, do a echo " " > /var/log/messages, so that the file size will be set... (7 Replies)
Discussion started by: jockey007
7 Replies
Login or Register to Ask a Question