Replace only if the keyword is the first word in every line


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Replace only if the keyword is the first word in every line
# 1  
Old 06-03-2009
Question Replace only if the keyword is the first word in every line

How do I replace only if the keyword is at the begining of a line?
Code:
--
a = “This is a print statement”
print a
--
What if I want to replace print by #print only in the second line i.e only if the line starts with that keyword.
Please help me out. I'm new to SED.

-----Post Update-----

I need a SED command. Please help. I couldn't find about it anywhere and had to start a new thread.
# 2  
Old 06-03-2009
Code:
echo "print a" | sed 's/^print/#&/'

# 3  
Old 06-04-2009
For some reason the above code does not seem to work.

This is my code:

<--START

a = "This is a print statement
print a
b = "This contains print statement"
print b

END-->

DESIRED:
<--START

a = "This is a print statement
#print a
b = "This contains print statement"
#print b

END-->

Line 4 of the code (print b) should also be commented i.e tabs and space before the first word should be taken care of.

I'm trying to use:
sed 's/[^][pP][rR][iI][nN][tT]/#print/'
sed 's/^[pP][rR][iI][nN][tT]/#print/'
But nothing happens. No part of the code is commented.

If I use:
sed 's/[pP][rR][iI][nN][tT]/#print/'
The first occurence is commented correctly but I want it to be commented only if the line starts with the keyword.

SmilieSmilie
# 4  
Old 06-04-2009
> cat a.txt
a = "This is a print statement
print a
b = "This contains print statement"
print b
>
>
>
> sed 's/^[pP][rR][iI][nN][tT]/#print/' a.txt
a = "This is a print statement
#print a
b = "This contains print statement"
#print b
# 5  
Old 06-04-2009
Same problem.
For some reason,

sed 's/^[pP][rR][iI][nN][tT]/#print/' <filename>

is not commenting when the ‘print' statement starts with a tab/space (See the second encircled code.)

Can someone please give some pointers.
Check the attached screen shot.
Replace only if the keyword is the first word in every line-sedjpg
# 6  
Old 06-04-2009
sed 's/^\s*[pP][rR][iI][nN][tT]/#print/' a.txt

here '\s' represents any blank space, tab.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Replace only First Word in Line

I have query to replace the first word in line using below code but its replace the middle word too sed -i 's/load /# LOAD/' /tmp/check.sql Query 1 : UPDATE accheadcon_data_last_upload SET last_upload_date = '2017-07-23' Replace to UPDATE accheadcon_data_last_up# LOAD SET... (1 Reply)
Discussion started by: kaushik02018
1 Replies

2. Shell Programming and Scripting

Need to extract the word after a particular keyword throughout the file..

Hi Everyone, Need help in extracting the hostname from the below output. Expected output: DS-TESTB-GDS-1.TEST.ABC.COM DS-TESTB-GDS-2.TEST.ABC.COM .... ... /tmp $ cat -n /tmp/patchreport 1 /usr/bin/perl /admin/bin/patch/applyPatches.pl --apply_patches... (4 Replies)
Discussion started by: thiyagoo
4 Replies

3. Shell Programming and Scripting

Search for a Keyword in file and replace another keyword or add at the end of line

Hi I want to implement something like this: if( keyword1 exists) then check if(keyword2 exists in the same line) then replace keyword 2 with New_Keyword else Add New_Keyword at the end of line end if eg: Check for Keyword JUNGLE and add/replace... (7 Replies)
Discussion started by: dashing201
7 Replies

4. Shell Programming and Scripting

How to catch a two word keyword which may contain a new line(may include spaces or tab) in it?

How to catch a two word keyword which may contain a new line(may include spaces or tab) in it. for example there is a file a.txt. $more a.txt create view as (select from ......... .......... ( select .... ( select ...... .. select only no ((( number ( select end (12 Replies)
Discussion started by: neelmani
12 Replies

5. Shell Programming and Scripting

sed command to replace a word with new line and /

Hi, I have been trying to replace the key word "SQL> spool off " with "/ show errors" with out double quotes in all the files in a directory. above show erros should be displayed next line Could you please help me how to do that. I have tried something like this... (3 Replies)
Discussion started by: pointers
3 Replies

6. Shell Programming and Scripting

How to get the next word which falls just after a keyword?

Hi friends, i just want to know the command though which i can get the next word which comes just after a particluar keyword. For example: suppose text.out is file which contains a pl/sql procedure . i want to find out the word which falls just after the "table1" keyword. Thank... (7 Replies)
Discussion started by: neelmani
7 Replies

7. Shell Programming and Scripting

perl: replace multiple word on a line

Hi All, If I have a line as following: ( MA "vertical" ) How can I convert it to as below: ( BC "horizontal" ) Thanks, --Michael (6 Replies)
Discussion started by: mxn731
6 Replies

8. Shell Programming and Scripting

Need to replace the first word of a line if it occurs again in the next line(shell)

Hi folks, have a look into the attachment, i am not familiar with unix, can you please help me in this regard. thanks in advance, :) regards, Geeko (4 Replies)
Discussion started by: geeko
4 Replies

9. Shell Programming and Scripting

replace only 1st word of a line if it comes in the subsequent lines at same postion.

I have a file like this.. Maharastra Mumbai worli Maharastra Mumbai navy maharatra Pune Maharastra Nagpur Karnataka Bangalore Karnataka Mysore Karnataka Mangalore Punjab Amritsar punjab Jalandar my expected outcome should be like this Maharastra Mumbai worli ---------- ... (9 Replies)
Discussion started by: geeko
9 Replies

10. Shell Programming and Scripting

How to replace a word at a parcitular line

Could someone tell me how to replace a word at a particular line by a single SED or AWK command? e.g. I have a file with the contents below: $ cat file1 111 AAA 333 CCC 222 BBB 444 CCC I want to replace the word "CCC" with a blank to get the desired output below: 111 AAA 333 CCC... (3 Replies)
Discussion started by: stevefox
3 Replies
Login or Register to Ask a Question