Replace a whole line with "#"


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Replace a whole line with "#"
# 1  
Old 04-27-2013
Replace a whole line with "#"

how can i replace the whole line which start with a specific pattern such as "???", with a desired string such as "#" i.e:

in-file:
Code:
TAB 20120201 21:02:02
MRD 20121301 22:21:01
??? 201 256
SAS 20123010 12:25:41
MMT 20120102 11:01:12
POP 20120306 02:01:01
??? TAV 2021 333

out-file:
Code:
TAB 20120201 21:02:02
MRD 20121301 22:21:01
#
SAS 20123010 12:25:41
MMT 20120102 11:01:12
POP 20120306 02:01:01
#

# 2  
Old 04-27-2013
Code:
$ sed "s/^???.*/#/" input
TAB 20120201 21:02:02
MRD 20121301 22:21:01
#
SAS 20123010 12:25:41
MMT 20120102 11:01:12
POP 20120306 02:01:01
#

This User Gave Thanks to hanson44 For This Post:
# 3  
Old 04-28-2013
Thank you very much.
# 4  
Old 04-28-2013
With awk
Code:
awk '/^?\?\?/ {$0="#"} 1'

This User Gave Thanks to Jotne For This Post:
# 5  
Old 04-28-2013
Quote:
Originally Posted by Jotne
With awk
Code:
awk '/^?\?\?/ {$0="#"} 1'

Hi Jotne,
I don't see the need for the backslashes? Why not just:
Code:
awk '$1=="???"{$0="#"}1' in-file > out-file
    or
awk '/^???/{$0="#"}1' in-file > out-file

Hi saeed.soltani,
This can also be done directly in a shell using only shell built-ins (without awk or sed):
Code:
while read -r f1 rest
do      if [ "$f1" = "???" ]
        then    echo "#"
        else    printf "%s %s\n" "$f1" "$rest"
        fi
done < in-file > out-file

This will work with any shell that recognizes basic Bourne shell syntax (such as bash and ksh; but not csh and its derivatives).
This User Gave Thanks to Don Cragun For This Post:
# 6  
Old 04-28-2013
@don: Since awk uses ERE when using matching, ? has a special meaning and needs to be escaped, except for the first ? since it follows a leading caret sign and is therefore not special..
These 2 Users Gave Thanks to Scrutinizer For This Post:
# 7  
Old 04-28-2013
Quote:
Originally Posted by Scrutinizer
@don: Since awk uses ERE when using matching, ? has a special meaning and needs to be escaped, except for the first ? since it follows a leading caret sign and is therefore not special..
Yes, obviously. I obviously didn't get enough sleep before sending that message.

For anyone who hasn't followed this discussion, the awk command:
Code:
/^???/{$0="#"}

matches any line with zero or one ? at the start of the line followed by one ? and changes any matching line to an octothorpe (#).

==============
The above analysis is incorrect. As Scrutinizer points out later, the first question mark above produces undefined results unless it is escaped.

The 1st awk script I suggested and the shell script I suggested in message #5 in this thread were correct, but the 2nd awk script should have been:
Code:
awk '/^\?\?\?/{$0="#"}1' in-file > out-file


Last edited by Don Cragun; 04-28-2013 at 03:35 PM..
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

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

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

Move a line containg "char" above line containing "xchar"

Okay, so I have a rather large text file and will have to process many more and this will save me hours of work. I'm not very good at scripting, so bear with me please. Working on Linux RHEL I've been able to filter and edit and clean up using sed, but I have a problem with moving lines. ... (9 Replies)
Discussion started by: rex007can
9 Replies

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

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

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

7. UNIX for Dummies Questions & Answers

shell script to replace a line contain an unkown pattern starting with "aaa, bbb"

Hello, can any one help me on this? I have a /etc/exports file, it may contain a line (I can not remember exactly). Let me use an a sample file myfile.txt which contains a line * mypattern uncertain key words I want this line (with any possible combination of the uncertain key words to be... (2 Replies)
Discussion started by: Dingrong
2 Replies

8. Shell Programming and Scripting

How to remove "New line characters" and "spaces" at a time

Dear friends, following is the output of a script from which I want to remove spaces and new-line characters. Example:- Line1 abcdefghijklmnopqrstuvwxyz Line2 mnopqrstuvwxyzabcdefghijkl Line3 opqrstuvwxyzabcdefdefg Here in above example, at every starting line there is a “tab” &... (4 Replies)
Discussion started by: anushree.a
4 Replies

9. Shell Programming and Scripting

replace '"|\n" with new line in shell script

Hi All, How can i replace '"|\n" with new line in shell script? Thanks! vaskar (8 Replies)
Discussion started by: vaskarbasak
8 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