I want to insert "#" at specific line in file


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting I want to insert "#" at specific line in file
# 1  
Old 01-18-2014
I want to insert "#" at specific line in file

input
i am good

output
#i am good
# 2  
Old 01-18-2014
Try :
Code:
$ awk 'FNR==specific{$0="#"$0}1' specific="25" file

change specific="xx" for the line you want
This User Gave Thanks to Akshay Hegde For This Post:
# 3  
Old 01-18-2014
Code:
sed 's/i am good/#&/' file

This User Gave Thanks to balajesuri For This Post:
# 4  
Old 01-18-2014
a sed to put the # at the beginning of the line..
Code:
sed 's/.*i am good/#&/'

or
Code:
sed '/i am good/s/^/#/'

This User Gave Thanks to Scrutinizer For This Post:
# 5  
Old 01-18-2014
And, of course, if by "specific" you mean a line number:

Code:
sed "25s/^/#/" file

This User Gave Thanks to Scott For This Post:
# 6  
Old 01-20-2014
thanks eveyone but i want to save the changes in file

if i do
vi client.cc

and want to comment "using namespace std;" it should be saved

Code:
using namespace std;

Code:
int main(int argc, char *argv[])
{
    int sockfd, portno, n;
    struct sockaddr_in serv_addr;

    char buffer[256];
    sockfd = socket(AF_INET, SOCK_STREAM, 0);
    if (sockfd < 0)
        cout<<"ERROR opening socket"<<endl;
    memset((char *)&serv_addr,0, sizeof(serv_addr));
    serv_addr.sin_family = AF_INET;
    serv_addr.sin_port = htons(2014);
    serv_addr.sin_addr.s_addr = inet_addr("");
    if (connect(sockfd,(struct sockaddr *) &serv_addr,sizeof(serv_addr)) < 0)
{

OUTPUT I Want:
Again if i open file
i.e. vi client.cc than output should be
Code:
#using namespace std;

int main(int argc, char *argv[])
{
    int sockfd, portno, n;
    struct sockaddr_in serv_addr;

    char buffer[256];
    sockfd = socket(AF_INET, SOCK_STREAM, 0);
    if (sockfd < 0)
        cout<<"ERROR opening socket"<<endl;
    memset((char *)&serv_addr,0, sizeof(serv_addr));
    serv_addr.sin_family = AF_INET;
    serv_addr.sin_port = htons(2014);
    serv_addr.sin_addr.s_addr = inet_addr("192.168.56.1");
    if (connect(sockfd,(struct sockaddr *) &serv_addr,sizeof(serv_addr)) < 0)
{


Last edited by Scrutinizer; 01-20-2014 at 01:20 PM.. Reason: code tags
# 7  
Old 01-20-2014
Just redirect to a new file
Code:
> new file

? Or use GNU or BSD sed -i option...

In vi itself, you could also try:
Code:
/using namespace/s/^/#/

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

Failure: if grep "$Var" "$line" inside while read line loop

Hi everybody, I am new at Unix/Bourne shell scripting and with my youngest experiences, I will not become very old with it :o My code: #!/bin/sh set -e set -u export IFS= optl="Optl" LOCSTORCLI="/opt/lsi/storcli/storcli" ($LOCSTORCLI /c0 /vall show | grep RAID | cut -d " "... (5 Replies)
Discussion started by: Subsonic66
5 Replies

3. Shell Programming and Scripting

In Vi "sed" substitute word on a specific line

i need to substitute word on a specific line. I was able to do it on command line like below but it is not working in vi. command line like below: sed -e '8s/table_name/schema.table_name/' file_name. in vi table_name and schema are my positional parameters that i pass into the script. ... (5 Replies)
Discussion started by: pimmit22043
5 Replies

4. UNIX for Dummies Questions & Answers

Using "mailx" command to read "to" and "cc" email addreses from input file

How to use "mailx" command to do e-mail reading the input file containing email address, where column 1 has name and column 2 containing “To” e-mail address and column 3 contains “cc” e-mail address to include with same email. Sample input file, email.txt Below is an sample code where... (2 Replies)
Discussion started by: asjaiswal
2 Replies

5. Shell Programming and Scripting

Find lines with "A" then change "E" to "X" same line

I have a bunch of random character lines like ABCEDFG. I want to find all lines with "A" and then change any "E" to "X" in the same line. ALL lines with "A" will have an "X" somewhere in it. I have tried sed awk and vi editor. I get close, not quite there. I know someone has already solved this... (10 Replies)
Discussion started by: nightwatchrenba
10 Replies

6. Shell Programming and Scripting

Substituting comma "," for dot "." in a specific column when comma"," is a delimiter

Hi, I'm dealing with an issue and losing a lot of hours figuring out how i would solve this. I have an input file which looks like this: ('BLABLA +200-GRS','Serviço ','TarifaçãoServiço','wap.bla.us.0000000121',2985,0,55,' de conversão em escada','Dia','Domingos') ('BLABLA +200-GRR','Serviço... (6 Replies)
Discussion started by: poliver
6 Replies

7. UNIX for Dummies Questions & Answers

replace "," with "." only in specific columns of a file?

Hi all, I have this text file containing 9 columns separated by space. The 8th columns contains the numbers. C1 C2 C3 C4 C5 C6 C7 C8 C9 er rt yt gh iu nk il 0.07 xs yt lr ty bg iu zk nh 0,0005 lt ...etc. I want to replace the comma with full stop only in 8th coloumn. the output... (8 Replies)
Discussion started by: Unilearn
8 Replies

8. 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

9. Shell Programming and Scripting

Can "sed" substitute word on a specific line?

Hello experts, I know line number of the word I want to replace. Can "sed" substitute word on a specific line? As well, can sed substitute words inside a specific patten. ex. <word>lalala</word> #replace anything between <word> and </word> minifish (2 Replies)
Discussion started by: minifish
2 Replies

10. UNIX for Dummies Questions & Answers

Explain the line "mn_code=`env|grep "..mn"|awk -F"=" '{print $2}'`"

Hi Friends, Can any of you explain me about the below line of code? mn_code=`env|grep "..mn"|awk -F"=" '{print $2}'` Im not able to understand, what exactly it is doing :confused: Any help would be useful for me. Lokesha (4 Replies)
Discussion started by: Lokesha
4 Replies
Login or Register to Ask a Question