issue with substituting using sed


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting issue with substituting using sed
# 1  
Old 02-27-2010
issue with substituting using sed

have a fileA containing about 260 lines wherein i have to match 2 lines

fileA
Code:
blah blah 
OF 90 DAYS DOCS PERIOD 12/06/0"
Pairs_Id 52006
Amount1 -300000.0
Amount2 15091500.10
Codifiers_Id 0
OriginalId 0
EOT
--blah blah blah

TBL Tradt_IN
CardRate 0.0
hashAmount -15091500.0
FirstLegType ""

I have to substitute the Amount2 value with the hashAmount value ie (-)15091500.0 with 15091500.0 (without the negative symbol)

hashAmount and Amount2 occur only once in the file

I have tried the following
Code:
$STR=$(awk '/hash/ {print "Amount2" " " $2 * -1}' ins)

echo $STR shows the value
Code:
Amount2 15091500

I have to substitute this in place of the Amount2 line
Code:
sed -e "/$STR/" -e "/Amount2/d" fileA > fileB

i am getting the following error
Code:
sed: -e expression #1, char 19: missing command


further substitution to be done only

1) when hashAmount > 0
2) and when the difference between hashAmount and Amount2 is not more than 2 (i.e only when there is difference in decimal value
3) There are some 50 files on which i have to repeat this action

Regards

Image

Last edited by Scott; 02-27-2010 at 03:40 PM.. Reason: Please use code tags
# 2  
Old 02-28-2010
Quote:
Originally Posted by sunnyboy
...
I have tried the following
Code:
$STR=$(awk '/hash/ {print "Amount2" " " $2 * -1}' ins)

You probably mean:
Code:
STR=$(awk '/hash/ {print "Amount2" " " $2 * -1}' ins)


Quote:
echo $STR shows the value
Code:
Amount2 15091500

I have to substitute this in place of the Amount2 line
Code:
sed -e "/$STR/" -e "/Amount2/d" fileA > fileB

i am getting the following error
Code:
sed: -e expression #1, char 19: missing command

That's because you haven't told sed what to do when the patter "$STR" matches (missing command).
"/$STR/" matches the pattern, but you have to decide what to do after the match - print it ? delete it ? substitute it ?
"char 19" tells you that the command should've been at the 19th char position; check the characters in the expansion => /Amount2 15091500/

You probably wanted to do something like this -

Code:
$ 
$ # show the contents of the file
$ cat -n ins
     1  blah blah 
     2  OF 90 DAYS DOCS PERIOD 12/06/0"
     3  Pairs_Id 52006
     4  Amount1 -300000.0
     5  Amount2 15091500.10
     6  Codifiers_Id 0
     7  OriginalId 0
     8  EOT
     9  --blah blah blah
    10  TBL Tradt_IN
    11  CardRate 0.0
    12  hashAmount -15091500.0
    13  FirstLegType ""
$ 
$ # set the value of shell variable STR
$ STR=$(awk '/hash/ {print "Amount2" " " $2 * -1}' ins)
$ echo $STR
Amount2 15091500
$ 
$ # substitute using sed
$ sed -e "s/^Amount2.*$/$STR/" ins
blah blah 
OF 90 DAYS DOCS PERIOD 12/06/0"
Pairs_Id 52006
Amount1 -300000.0
Amount2 15091500
Codifiers_Id 0
OriginalId 0
EOT
--blah blah blah
TBL Tradt_IN
CardRate 0.0
hashAmount -15091500.0
FirstLegType ""
$ 
$

Quote:
further substitution to be done only

1) when hashAmount > 0
2) and when the difference between hashAmount and Amount2 is not more than 2 (i.e only when there is difference in decimal value
3) There are some 50 files on which i have to repeat this action
Here's a Perl solution to perform this processing.
I've assumed that the files of interest are the ones that end with ".txt" extension. It needn't be ".txt"; it can be any appropriate regular expression in the glob function.

Code:
$ 
$ # show how many txt files are here
$ ls -1 *.txt
f1.txt
f2.txt
f3.txt
$ 
$ # show the contents of each txt file
$ 
$ cat f1.txt
blah blah blah
Amount2 15091500.10
blah blah blah
hashAmount 15091500.0
blah blah blah
$ 
$ cat f2.txt
blah blah blah
Amount2 15
blah blah blah
hashAmount -15
blah blah blah
$ 
$ cat f3.txt
blah blah blah
Amount2 -34
blah blah blah
hashAmount 35
blah blah blah
$ 
$

So,

- Amount2 in f1.txt should be substituted.

- Amount2 in f2.txt should not be substituted because hashAmount < 0 (violation of Rule 1).

- Amount2 in f3.txt should not be substituted because hashAmount - Amount2 = 35 - (-34) = 69 which is > 2 (violation of Rule 2).

Here's the Perl program to do that. The inline script comments should be self-explanatory.

Code:
$ 
$ cat -n swap.pl
     1  #!/usr/bin/perl -w
     2  # Assuming all "*.txt" files are to be scanned and updated
     3  while (defined($f = glob("*.txt"))) {
     4    print "NOW PROCESSING FILE : $f\n";
     5    open (FH, $f) or die "Can't open $f: $!";
     6    while (<FH>) {
     7      chomp;
     8      # Push current line to contents array
     9      push @contents, $_;
    10      # Check for the lines that start with Amount2 or hashAmount
    11      if (/^Amount2/) {
    12        $amtidx = $.-1;
    13        @amt = split;
    14      } elsif (/^hashAmount/) {
    15        $hshidx = $.-1;
    16        @hamt = split;
    17        # Swap only if line with "Amount2" exists, and
    18        # hashAmount > 0, and
    19        # difference between hashAmount and Amount2 is less than or equal to 2
    20        if (defined $amt[1] and $hamt[1] > 0 and $hamt[1] - $amt[1] <= 2) {
    21          $contents[$amtidx] = "$amt[0] $hamt[1]";
    22        } 
    23      }
    24    }
    25    close (FH) or die "Can't close $f: $!";
    26    # print array to temp file
    27    $tmpfile = "$f.tmp";
    28    open (TMP, ">$tmpfile") or die "Can't open $tmpfile: $!";
    29    foreach $item (@contents) {
    30      print TMP "$item\n" or die "Can't print: $!";
    31    }
    32    close (TMP) or die "Can't close $tmpfile: $!";
    33    # and move the temp file back to original
    34    rename $tmpfile, $f or die "File rename from $tmpfile to $f failed: $!";
    35    # now flush all variables before using them for the next file
    36    @contents = ();
    37    $amtidx = "";
    38    $hshidx = "";
    39  }
    40
$ 
$

Here's the execution -

Code:
$ 
$ # execute the Perl program
$ perl swap.pl
NOW PROCESSING FILE : f1.txt
NOW PROCESSING FILE : f2.txt
NOW PROCESSING FILE : f3.txt
$ 
$ # now check the files
$ 
$ cat f1.txt
blah blah blah
Amount2 15091500.0
blah blah blah
hashAmount 15091500.0
blah blah blah
$ 
$ cat f2.txt
blah blah blah
Amount2 15
blah blah blah
hashAmount -15
blah blah blah
$ 
$ cat f3.txt
blah blah blah
Amount2 -34
blah blah blah
hashAmount 35
blah blah blah
$ 
$

HTH,
tyler_durden
# 3  
Old 02-28-2010
Thanks a lot that was greatSmilie
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Nawk sub not substituting

I am trying to use nawk sub to substitute a string in a file. Both the pattern and the replacement I set as variables using bash. here is the code: #!/bin/bash -x ydate=`/usr/local/bin/date +%Y%m%d` echo $ydate test_ca=/home/mdadmin/test_ca for i in `cat ${test_ca}` do if ]; then... (9 Replies)
Discussion started by: smenago
9 Replies

2. Shell Programming and Scripting

getting error while substituting variable using sed..

I am using script for substitute one variable with another variable like below... below code works fine... sed 's/'$sub_fun'/'$To_sub'/g' But when i run the same code from the script getting below errors.. sed: -e expression #1, char 7: unterminated `s' command please help....... (2 Replies)
Discussion started by: pamu
2 Replies

3. Shell Programming and Scripting

Trouble with sed and substituting a string with special characters in variable

Hey guys, I know that title is a mouthful - I'll try to better explain my struggles a little better... What I'm trying to do is: 1. Query a db and output to a file, a list of column data. 2. Then, for each line in this file, repeat these values but wrap them with: ITEM{ ... (3 Replies)
Discussion started by: ampsys
3 Replies

4. Shell Programming and Scripting

Substituting a shell variable in sed

Hi guys, I'm trying to figure out how to use a shell variable inside my sed command. I just want to remove a certain part of a path. I've tried three different combinations and none of them work. Here are the three combinations: echo $file | sed 's/'$test'//' echo $file | sed "s/$test//"... (7 Replies)
Discussion started by: chu816
7 Replies

5. Shell Programming and Scripting

Substituting the values

Hi Gurus this is working finee with tested values #!/bin/ksh V_DATE="2007-11-30" V_ID=789 V_NAME="john_${V_ID}_has_${V_DATE}_s" FILE_NAME=`echo ${V_NAME}` echo ${FILE_NAME} Buttt the problem is the first two values will come dynamically and the file will looks like... (2 Replies)
Discussion started by: SeenuGuddu
2 Replies

6. Shell Programming and Scripting

Substituting Characters using SED

Can SED be used to substitute a character (y) with a character (Y) in a specified field? File has 12000 : delimeted rows as; HHC 1 BDE:Lastname, Firstname MI:firstname.mi.lastname@mil:SGT HHC 2 BDE:Lastname, Firstname MI:Firstname.MI.Lastname@mil:SGT I wish to replace the capital letters... (6 Replies)
Discussion started by: altamaha
6 Replies

7. Shell Programming and Scripting

Issue with a sed one liner variant - sed 's/ ; /|/g' $TMP1 > $TMP

Execution of the following segment is giving the error - Script extract:- OUT=$DATADIR/sol_rsult_orphn.bcp TMP1=${OUT}_tmp1 TMP=${OUT}_tmp ( isql -w 400 $dbConnect_OPR <<EOF select convert(char(10), s.lead_id) +'|' + s.pho_loc_type, ";", s.sol_rsult_cmnt, ";", +'|'+ s.del_ind... (3 Replies)
Discussion started by: kzmatam
3 Replies

8. Programming

substituting one string for another

I have a Linux C program I'm writing that has one section where, within a large string, I need to substitute a smaller string for another, and those probably won't be the same size. For instance, if I have a string: "Nowisthetimeforallgoodmen" and I want to substitute 'most' for 'all' the... (2 Replies)
Discussion started by: cleopard
2 Replies

9. Shell Programming and Scripting

substituting

Hello please how can i change this infrormation within a file dynamically without using vi " || $6 ~ /^229*/ " the * means any number within the file has this content : cat lec|awk -F '|' 'length($6) >= 12 || length($6) <= 10' |awk -F '|' '$6 ~ /^24/ || $6 ~ /^22924/ &&$7 ~... (1 Reply)
Discussion started by: neyo
1 Replies

10. Shell Programming and Scripting

Substituting with value of variable in Sed

Hi, I have a program in which i have to substitute a TAG in a file with the value of a variable. Code Snippet: ---------------- myvar=1234 sed 's/_TAG_/$myvar/' infile outfile When I run this command, the _TAG_ in the "infile" is substituted with "$myvar" but NOT the value "1234"... (1 Reply)
Discussion started by: jyotipg
1 Replies
Login or Register to Ask a Question