sed command not working for me to change text in a file


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting sed command not working for me to change text in a file
# 1  
Old 02-02-2017
sed command not working for me to change text in a file

UNIX gurus

I need your help with the following (The server is an AIX box).

I have a text file with the following information:

Code:
********************************************************
SOME LINES
case
:WORD1
SOME LINES
:WORD2
SOME LINES
:WORD3
SOME LINES
esac
SOME LINES
*********************************************************

I need to change the lines starting with : and replace them with *). For example:
Code:
********************************************************
SOME LINES
case
WORD1*)
SOME LINES
WORD2*)
SOME LINES
WORD3*)
SOME LINES
esac
SOME LINES
*********************************************************

I wrote the following code to get this done, but it's not working, only the last line gets changed, From :WORD3 to WORD3*), the other two (:WORD1 and :WORD2) don't get changed.

Code:
#!/bin/ksh
# ======================================
TWSDATADIR=/opt/apps/TWA/TWS/TWS
TWSDATAFILE=$TWSDATADIR/martin.ABEND
TWSTARGETFILE1=$TWSDATADIR/martin1.ABEND
TWSTARGETFILE2=$TWSDATADIR/martin2.ABEND
TWSTARGETFILE3=$TWSDATADIR/martin3.ABEND
# List the bad lines
# ===========================
sed -e '1,/case $JOBNAME/d' -e '/esac/,$d' $TWSDATAFILE |grep : > $TWSTARGETFILE1
BADLINES=`cat $TWSTARGETFILE1`
for line in $BADLINES
do
TEXT="*)"
good_line_temp=`echo $line | cut -d: -f2`
echo "Bad line: $line"
good_line=`echo "$good_line_temp$TEXT"`
echo "Good line: $good_line"
echo $good_line >> $TWSTARGETFILE3
# sed "s/$line/$good_line/g" $TWSDATAFILE
sed -e "s/${line}/${good_line}/g" $TWSDATAFILE > $TWSTARGETFILE2
done

*********************************************************
I believe the last "sed" command is missing something. Up to that point, the script works fine as shown below. However, when I change on the target file ($TWSTARGETFILE2) as explained above only the last occurrence (:WORD3) gets changed (to WORD3*), but not the 1st (:WORD1) or 2nd (:WORD) occurrence.

Code:
Bad line: :WORD1
Good line: WORD1*)
Bad line: :WORD2
Good line: WORD2*)
Bad line: :WORD3
Good line: WORD3*)

Thanks for your help on this.

Martin.

Last edited by Scrutinizer; 02-02-2017 at 01:19 AM.. Reason: code tags
# 2  
Old 02-02-2017
I believe this would more or less do what you require:
Code:
sed '/case/,/esac/{s/^:\(.*\)/\1*)/;}' file

But in order to turn it into shell case statement you also need to introduce proper indentation and closing ;; after every command list.
This User Gave Thanks to Scrutinizer For This Post:
# 3  
Old 02-02-2017
Quote:
Originally Posted by Scrutinizer
I believe this would more or less do what you require:
Code:
sed '/case/,/esac/{s/^:\(.*\)/\1*)/;}' file

But in order to turn it into shell case statement you also need to introduce proper indentation and closing ;; after every command list.
Concur, but AIX-sed is a little bit peculiar about formatting programs:

Code:
sed '/case/,/esac/ {;s/^:\(.*\)/\1*)/;}' file

@thread-op:

Instead of this:
Code:
sed -e '1,/case $JOBNAME/d' -e '/esac/,$d' $TWSDATAFILE |grep : > $TWSTARGETFILE1
BADLINES=`cat $TWSTARGETFILE1`
for line in $BADLINES
do
[...]

This does the same with less effort (if you even need the intermediary file $TWSTARGETFILE1 as it is not used in the script at all - if you don't, remove the tee-command from the pipeline):
Code:
sed -n '/case/,/esac/ /^:/p' | tee $TWSTARGETFILE1 | while read line ; do
[...]

bakunin

Last edited by bakunin; 02-02-2017 at 03:48 PM..
# 4  
Old 02-02-2017
Quote:
Originally Posted by bakunin
Concur, but AIX-sed is a little bit peculiar about formatting programs:

Code:
sed '/case/,/esac/ {;s/^:\(.*\)/\1*)/;}' file

[..]
Hi Bakunin,

Just tried it on AIX 7, but it did not seem to make a difference:
Code:
$ sed '/case/,/esac/{s/^:\(.*\)/\1*)/;}'  infile
********************************************************
SOME LINES
case
WORD1*)
SOME LINES
WORD2*)
SOME LINES
WORD3*)
SOME LINES
esac
SOME LINES
*********************************************************
$ sed '/case/,/esac/ {;s/^:\(.*\)/\1*)/;}' infile
********************************************************
SOME LINES
case
WORD1*)
SOME LINES
WORD2*)
SOME LINES
WORD3*)
SOME LINES
esac
SOME LINES
*********************************************************

# 5  
Old 02-02-2017
Quote:
Originally Posted by Scrutinizer
Just tried it on AIX 7, but it did not seem to make a difference:
hmm, interesting. Not long ago i had to change exactly this on some 6.1 system because sed refused to accept the command portion without a blank between the range-statements:

Code:
sed '/regexp/,/regexp/{ ...     # did not work
sed '/regexp/,/regexp/ { ...    # worked

As i encountered that peculiarity a few times (usually because of a typo, because i actively avoided that once i had this problem) i was of the impression the AIX-sed is just like this. I guess it has finally been fixed then. Btw. the same was true for negated clauses:

Code:
sed '/regexp/,/regexp/ ! { ...     # did not work, but works with GNU-sed
sed '/regexp/,/regexp/! { ...      # did also not work
sed '/regexp/,/regexp/ !{ ...      # worked


bakunin
# 6  
Old 02-15-2017
First of all, thank you all for your replies and suggestions, unfortunately, it's still not working.

So far per your suggestions, I have tried these:

Code:
 sed '/case/,/esac/{s/^:\(.*\)/\1*)/;}' file
 sed '/case/,/esac/ {s/^:\(.*\)/\1*)/;}' file
 sed '/case/,/esac/ {;s/^:\(.*\)/\1*)/;}' file

So let me explain it easier:

I'm trying to go from this:
=====================================================
Code:
 case $JOBNAME in
  :WORD1
    APP="maestro-global"
    AG="prd-global-ssprodc"
    SERVICE="hcl hcl servers instance service"
    SEV=WARNING
    ;;
  :WORD2
    APP="maestro-global"
    AG="prd-global-ssprodc"
    SERVICE="hcl hcl servers instance service"
    SEV=MINOR
    ;;
  :WORD3
    APP="maestro-global"
    AG="prd-global-ssprodc"
    SERVICE="hcl hcl servers instance service"
    SEV=MINOR
    ;;
  *)
    APP="maestro-global"
    AG="prd-global-ssprodc"
    SERVICE="hcl hcl servers instance service"
    SEV=MINOR
    ;;
esac

=====================================================
to this:
=====================================================
Code:
 case $JOBNAME in
  WORD1*)
    APP="maestro-global"
    AG="prd-global-ssprodc"
    SERVICE="hcl hcl servers instance service"
    SEV=WARNING
    ;;
  WORD2*)
    APP="maestro-global"
    AG="prd-global-ssprodc"
    SERVICE="hcl hcl servers instance service"
    SEV=MINOR
    ;;
  WORD3*)
    APP="maestro-global"
    AG="prd-global-ssprodc"
    SERVICE="hcl hcl servers instance service"
    SEV=MINOR
    ;;
  *)
    APP="maestro-global"
    AG="prd-global-ssprodc"
    SERVICE="hcl hcl servers instance service"
    SEV=MINOR
    ;;
esac

============================================

Please help,

thanks,

curiousmal
Moderator's Comments:
Mod Comment Please use CODE tags (as required by forum rules). Without the CODE tags, it looks like you want to remove the leading colon character instead of replacing it with a <space> character AND it looks like the leading colon is the first character on the line (when it is not).

Last edited by Don Cragun; 02-15-2017 at 11:35 PM.. Reason: Add CODE tags.
# 7  
Old 02-15-2017
Quote:
Originally Posted by curiousmal
First of all, thank you all for your replies and suggestions, unfortunately, it's still not working.

So far per your suggestions, I have tried these:

Code:
 sed '/case/,/esac/{s/^:\(.*\)/\1*)/;}' file
 sed '/case/,/esac/ {s/^:\(.*\)/\1*)/;}' file
 sed '/case/,/esac/ {;s/^:\(.*\)/\1*)/;}' file

So let me explain it easier:

I'm trying to go from this:
=====================================================
Code:
 case $JOBNAME in
  :WORD1
    APP="maestro-global"
    AG="prd-global-ssprodc"
    SERVICE="hcl hcl servers instance service"
    SEV=WARNING
    ;;
  :WORD2
    APP="maestro-global"
    AG="prd-global-ssprodc"
    SERVICE="hcl hcl servers instance service"
    SEV=MINOR
    ;;
  :WORD3
    APP="maestro-global"
    AG="prd-global-ssprodc"
    SERVICE="hcl hcl servers instance service"
    SEV=MINOR
    ;;
  *)
    APP="maestro-global"
    AG="prd-global-ssprodc"
    SERVICE="hcl hcl servers instance service"
    SEV=MINOR
    ;;
esac

=====================================================
to this:
=====================================================
Code:
 case $JOBNAME in
  WORD1*)
    APP="maestro-global"
    AG="prd-global-ssprodc"
    SERVICE="hcl hcl servers instance service"
    SEV=WARNING
    ;;
  WORD2*)
    APP="maestro-global"
    AG="prd-global-ssprodc"
    SERVICE="hcl hcl servers instance service"
    SEV=MINOR
    ;;
  WORD3*)
    APP="maestro-global"
    AG="prd-global-ssprodc"
    SERVICE="hcl hcl servers instance service"
    SEV=MINOR
    ;;
  *)
    APP="maestro-global"
    AG="prd-global-ssprodc"
    SERVICE="hcl hcl servers instance service"
    SEV=MINOR
    ;;
esac

============================================

Please help,

thanks,

curiousmal
Moderator's Comments:
Mod Comment Please use CODE tags (as required by forum rules). Without the CODE tags, it looks like you want to remove a colon character when it is the first character on the line (when it is not).
None of the sed substitute command you are using can work because they are looking for a colon anchored at the start of a line (which would have been perfectly reasonable since you didn't use CODE tags and normal HTML text processing discards leading <space> and <tab> characters and coalesces all combinations of <space> and <tab> characters not at the start of a line into a single <space> character.

Try the following instead:
Code:
sed '/case/,/esac/ { s/^\([[:space:]]*\):\(.*\)/\1\2*)/; }' file

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

sed command not working

Hello There - Iam trying to get this expdp running for oracle backup. And this is the code below: ### Run the export. ### Comment out any LOGFILE parameters in the .par file. if grep -i "Logfile" /<Path>$1_$2_$3.par; then ## Comment out any LOGFILE... (7 Replies)
Discussion started by: bkilaru
7 Replies

2. Shell Programming and Scripting

sed command not working

cat bipin.txt Unix is an OS Unix has its own commmands Unix is a user friendly OS Unix is platform independent Unix is a time sharing OS the best OS to learn is Unix Abinitio uses Unix in backend this is my file when i use sed 's/Unix/Linux/' bipin.txt all the occurences are getting... (0 Replies)
Discussion started by: Bipin_1991
0 Replies

3. Shell Programming and Scripting

sed working on command line but file unchanged when execute with Shell script

I have a simple task to replace unix line feed end of line characters with carriage returns. When I run the following “change file in place” sed instruction from the command line all the Line feeds are successfully replaced with Carriage returns. sed -i 's/$/\r/' lf_file.txt But that same... (1 Reply)
Discussion started by: hawkman2k
1 Replies

4. Shell Programming and Scripting

sed command not working

Hi All, I am trying to run a sed command to replace a string in a file. sed -i -e "s/$Job_status_old ,$line/Job_status_new ,$line/g" stat.txt The command wen run from the command promt works fine. But the same command does not work when its put in a script. The script is not failing... (3 Replies)
Discussion started by: samyamkrishna
3 Replies

5. Shell Programming and Scripting

Why is this command not working? (sed)

Hi guys, the command is echo "Online Memory : 32768 MB" | sed 's/.*\(+\).*/\1/' I would expect it to print 32768, it cuts off any character to the first digit, then gets all digits in 1, cuts off the rest after the digits, and should print 32768, instead it... (4 Replies)
Discussion started by: funksen
4 Replies

6. Shell Programming and Scripting

sed command: change only twice

Hello, I recently sought help on another thread about how to prefix 2 words in a file with 'pack/'. This is the command: sed --in-place 's/"\(libraries\|objects\)"/"pack\/\1"/g' Background: I have a .json file with the word 'libraries' and 'objects' in it. However, 'libraries' occurs twice;... (6 Replies)
Discussion started by: AJ Ruckman
6 Replies

7. Shell Programming and Scripting

sed change text

Hello, I have sed to change improperly entered email address such as: blank@blank.co --> blank@blank.com (it should be) I am using this: sed 's/blank.co/blank.com/g' Problem is it makes good ones already blank.com becomes blank.comm which is incorrect..... It should only match *@.co... (3 Replies)
Discussion started by: holyearth
3 Replies

8. Shell Programming and Scripting

Need help please with Grep/Sed command to extract text and numbers from a file

Hello All, I need to extract lines from a file that contains ALPHANUMERIC and the length of Alphanumeric is set to 16. I have pasted the sample of the lines from the text file that I have created. My problem is that sometimes 16 appears in other part of the line. I'm only interested to... (14 Replies)
Discussion started by: mnassiri
14 Replies

9. Shell Programming and Scripting

Need to insert new text and change existing text in a file using SED

Hi all, I need to insert new text and change existing text in a file. For that I used the below line in the command line and got the expected output. sed '$a\ hi... ' shell > shell1 But I face problem when using the same in script. It is throwing the error as, sed: command garbled:... (4 Replies)
Discussion started by: iamgeethuj
4 Replies

10. UNIX for Dummies Questions & Answers

Sed command not working

Hi, I have a test file as follows: 1G102119 ^ AA1179291 ^ 06oct2006 09:50:35^ 73.4^ 2^ 13^ 0^ 1493 1G102119 ^ AA1179291 ^ 06oct2006 09:49:45^ 73.4^ 2^ 13^ 0^ 1493 1G102119 ^ AA1179291 ^ 06oct2006 09:48:58^ 73.4^ 2^ 17^ 0^ 2 1G102119 ... (9 Replies)
Discussion started by: shashi_kiran_v
9 Replies
Login or Register to Ask a Question