LinuxQuestions.org
Review your favorite Linux distribution.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 04-10-2006, 02:51 AM   #1
gsr_kashyap
Member
 
Registered: Jul 2004
Location: india
Distribution: Ubuntu 14.04
Posts: 155

Rep: Reputation: 15
how to add # in a file


hi everyone,
can any one tell me how to add # in a file.if i run the script it should check whether there is # in that line if it isn't then add else skip that line.
thanks in advance
kash
 
Old 04-10-2006, 02:53 AM   #2
acid_kewpie
Moderator
 
Registered: Jun 2001
Location: UK
Distribution: Gentoo, RHEL, Fedora, Centos
Posts: 43,417

Rep: Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985
how to add a hash sign?? erm... just press that key when you are editing it..
 
Old 04-10-2006, 03:38 AM   #3
gsr_kashyap
Member
 
Registered: Jul 2004
Location: india
Distribution: Ubuntu 14.04
Posts: 155

Original Poster
Rep: Reputation: 15
that's great idea but i want a better one. when i execute the script the script should open the file and add hash(#).
 
Old 04-10-2006, 03:41 AM   #4
acid_kewpie
Moderator
 
Registered: Jun 2001
Location: UK
Distribution: Gentoo, RHEL, Fedora, Centos
Posts: 43,417

Rep: Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985
well it helps if you actually explain what you want to do properly...

you can easily add a character to the end of a file, e.g. "echo # >> filename" if that's all you are after.
 
Old 04-10-2006, 03:42 AM   #5
AnanthaP
Member
 
Registered: Jul 2004
Location: Chennai, India
Posts: 952

Rep: Reputation: 217Reputation: 217Reputation: 217
The generic algorithm would be:

foreach line read:
If col1 not eq #, then
Write a #
end If
Write the line.
Move onto the next line.

Its possible with `awk` in any *nix or perl.
End
 
Old 04-10-2006, 03:50 AM   #6
gsr_kashyap
Member
 
Registered: Jul 2004
Location: india
Distribution: Ubuntu 14.04
Posts: 155

Original Poster
Rep: Reputation: 15
not exactly at the end of the file but starting of everyline.
 
Old 04-10-2006, 03:52 AM   #7
AnanthaP
Member
 
Registered: Jul 2004
Location: Chennai, India
Posts: 952

Rep: Reputation: 217Reputation: 217Reputation: 217
Kash.
You must have seen that the loop was <for each line read> ... <move on to the the next line>.

Whose your teacher?

end
 
Old 04-10-2006, 04:37 AM   #8
gsr_kashyap
Member
 
Registered: Jul 2004
Location: india
Distribution: Ubuntu 14.04
Posts: 155

Original Poster
Rep: Reputation: 15
i know the algorithm as i am not good at programing thats why i amd asking for the script.
btw who ever is my teacher doesnt mind here!!
 
Old 04-11-2006, 08:06 AM   #9
gsr_kashyap
Member
 
Registered: Jul 2004
Location: india
Distribution: Ubuntu 14.04
Posts: 155

Original Poster
Rep: Reputation: 15
ok everyone i at last i got the solution and want to share it .hope one understands wht i ment

#!/usr/bin/bash

/usr/bin/awk '{
if ($0 in stored_lines)
x=1
else
print "#" $0
stored_lines[$0]=1
}' <full path filename>

Last edited by gsr_kashyap; 04-11-2006 at 08:09 AM.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
How to add swap file tooparam Linux - General 4 03-17-2006 12:22 PM
How to add some bytes to a binary file? kornerr Linux - General 6 02-28-2006 11:23 AM
add a file to... Redrook Linux - Newbie 4 11-10-2003 09:40 PM
How may I add a file to an ISO image? geraldomanaus Linux - Newbie 6 04-24-2003 05:27 PM
add timestamp to name of file how? supafly Linux - General 3 11-14-2001 11:06 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 04:31 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration