Comment based on a condition


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Comment based on a condition
# 1  
Old 11-16-2010
Comment based on a condition

I want to comment 2 lines based on a condition. If THEN occurs immediately after WHEN then i have to comment both the lunes

For example :


Code:
 
$cat file1.txt
CASE
   WHEN
        THEN  1
   WHEN
         c1= 'I'
       AND c2= '0'
        THEN  2

So in this example i want to comment the lines as shown below.

SO the required output :

Code:
 
 
$cat file1.txt
CASE
  # WHEN
   #    THEN  1
   WHEN
             c1= 'I'
       AND c2= '0'
       THEN  2

# 2  
Old 11-16-2010
thru sed.. (note: after \n there are 8 spaces, which is equal to a TAB space)
Code:
sed '/WHEN/{N;s/WHEN\n        THEN/#WHEN\n\t#THEN/g}' inputfile > outfile

With the assumption that 'THEN 1' is typed after a TAB space in the inputfile/file1.txt

or
Code:
sed '/WHEN/{N;s/WHEN\n *THEN/#WHEN\n\t#THEN/}' inputfile > outfile


Last edited by michaelrozar17; 11-16-2010 at 05:24 AM.. Reason: replaced with \t
# 3  
Old 11-16-2010
Code:
sed '/WHEN/{N;/WHEN[[:space:]]*THEN/s/[ \t]*[WT]HEN/# &/g;}' infile

Code:
sed '/WHEN/{N;s/\(WHEN[[:space:]]*\)THEN/# \1# THEN/;}' infile

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Trigger script based on condition

Hi Guys, I am having below code which runs based on condition, Is it possible to check condition at the time of trigger code=$1 if ;then nohup sh script.sh $val 1 & fi I need to trigger if the $code = JP then only to trigger nohup sh script.sh $val 1 & My try but wanted... (4 Replies)
Discussion started by: Master_Mind
4 Replies

2. Shell Programming and Scripting

Copy down based on condition

Hello: I need to copy down some data from the previous record in to the next record based on the below conditions If position 41- 59 of the current record is same as the previous record and the value of position 62 is not equal to 1 then copy the previous records value for positions... (1 Reply)
Discussion started by: techedipro
1 Replies

3. UNIX for Dummies Questions & Answers

Condition based on Timestamp (Date/Time based) from logfile (Epoch seconds)

Below is the sample logfile: Userids Date Time acb Checkout time: 2013-11-20 17:00 axy Checkout time: 2013-11-22 12:00 der Checkout time: 2013-11-17 17:00 xyz Checkout time: 2013-11-19 16:00 ddd Checkout time: 2013-11-21 16:00 aaa Checkout... (9 Replies)
Discussion started by: asjaiswal
9 Replies

4. Shell Programming and Scripting

Print certain lines based on condition

Hi All, I have following listing Filesystem GB blocks Free Used Iused Iused Mounted on /dev/hd2 4.00 0.31 93 63080 43 /usr Filesystem GB blocks Free Used Iused Iused Mounted on Filesystem GB blocks Free Used Iused Iused... (11 Replies)
Discussion started by: ckwan
11 Replies

5. Shell Programming and Scripting

Arithmetic (number-based) if condition

Hi Folks I'm looking for help with if statement. I'm reading the file with header (starts with 0 on position 1 in the line) and data (starts with 1 on position 1 in the line). I have to check if the number from header (should be number of data rows) equal actual count of the data rows. ... (4 Replies)
Discussion started by: viallos
4 Replies

6. UNIX for Dummies Questions & Answers

moving files based on condition

hi i have to move files and send an email and attached the bad files to inform the developer about that. #!/bin/ksh BASE_DIR=/data/SrcFiles cd $BASE_DIR ## finding the files from work directory which are changed in 1 day find -type f -name "*.csv" –ctime 0 > /home/mydir/flist.txt ##... (14 Replies)
Discussion started by: awais290
14 Replies

7. Shell Programming and Scripting

Condition based concatenation.

Hello, I am looking for concatenating the lines based on conditions. Below are the contents of the file: Infile: ----- Test1.PO_Itm COLUMN GAC_DT. Test1.PO_Itm COLUMN (PRODTCD ,PLNTCD). Test1.PO_Itm COLUMN PLNTCD. Test1.PO_Itm COLUMN ACTVIND. Test2.RgnToTerrtryGPI COLUMN... (3 Replies)
Discussion started by: indrajit_u
3 Replies

8. Shell Programming and Scripting

transpose based on condition

Hi, I have the oracle table coulns in an order like date, state1, state2....state9 and i need to prepare data from the script output for loading in to this table The script is #!/bin/ksh /usr/xpg4/bin/awk -F"-" '{print $2,$4}' /aemu/ErrorLogs/data/MissingCGIcount.txt |... (5 Replies)
Discussion started by: aemunathan
5 Replies

9. Shell Programming and Scripting

Spliting file based on condition

Hi, I have a comma separated file with millions of records in it. I have a requirement to split the file based on the value in a one of the columns. Suppose i have a text file with columns like C1, C2,C3,C4 Column C4 can hold the values either 01 or 02 03 or 04. I nned to extract... (2 Replies)
Discussion started by: Raamc
2 Replies

10. Shell Programming and Scripting

Read file based on condition

Hi Friends, Can any one help with this: I have a huge file with the format as A SAM 4637 B DEPT1 4758 MILAN A SMITH 46585 B DEPT2 5385 HARRYIS B SAMUL 63547 GEORGE B DANIEL 899 BOISE A FRES 736 74638 I have to read this file and write only the records that starts with "B" only ... (5 Replies)
Discussion started by: sbasetty
5 Replies
Login or Register to Ask a Question