-1

I have the following string. I would like to find the first occurrence of digits in a line, use this match to replace the NULL's.

INSERT INTO data VALUES (11,1), (NULL,2), (NULL,3), (NULL,4);

I'm really not familiar with sed or awk at all.

so in the example above this is the output i want.

INSERT INTO data VALUES (11,1), (11,2), (11,3), (11,4);

this is kinda pseudo of the issue i think awk '{ sub(/{digit}/, "NULL", capturegroup }'

while read -r line; do
    name="$line"
    echo $line  |  
    awk '
    match($0,/\([0-9]+/){
      value=substr($0,RSTART+1,RLENGTH-1)
    }
    {
      gsub("NULL",value)
    }
    1' >> converted.sql
done < test.sql
muscleman71
  • 91
  • 2
  • 13
  • 3
    By seeing your profile came to know you never select any answer as correct answer, I would like to suggest you to please give it sometime and try to select an answer as correct one out of all posts. Even for your previous threads too you could do so. – RavinderSingh13 Jan 18 '19 at 14:39

3 Answers3

2

With sed (tested on GNU sed 4.2.2, syntax will vary for different implementations)

$ cat ip.txt
INSERT INTO data VALUES (11,1), (NULL,2), (NULL,3), (NULL,4);
foo (42,14), (4,NULL), (5,3), (NULL,14)

$ sed -E ':a s/([0-9]+)(.*)NULL/\1\2\1/; ta' ip.txt
INSERT INTO data VALUES (11,1), (11,2), (11,3), (11,4);
foo (42,14), (4,42), (5,3), (42,14)
  • -E to use ERE instead of default BRE
  • :a label a
  • ([0-9]+)(.*)NULL first sequence of digits, followed by other text, followed by last occurrence of NULL in the line
  • \1\2\1 as per expected output
  • ta branch to label a if the substitution succeeded
  • See sed in-place editing if you need to change the input file itself
Sundeep
  • 23,246
  • 2
  • 28
  • 103
1

Could you please try following.

echo "INSERT INTO data VALUES (11,1), (NULL,2), (NULL,3), (NULL,4);"  |  
awk '
match($0,/\([0-9]+/){
  value=substr($0,RSTART+1,RLENGTH-1)
}
{
  gsub("NULL",value)
}
1'

Output will be as follows.

INSERT INTO data VALUES (11,1), (11,2), (11,3), (11,4);

As per OP's comment in case you want to perform this operation for an Input_file then do following.(simply pass Input_file name to awk script)

awk '
match($0,/\([0-9]+/){
  value=substr($0,RSTART+1,RLENGTH-1)
}
{
  gsub("NULL",value)
}
1' Input_file
RavinderSingh13
  • 130,504
  • 14
  • 57
  • 93
1

You can try Perl also

$ cat muscleman.txt
INSERT INTO data VALUES (11,1), (NULL,2), (NULL,3), (NULL,4);
INSERT INTO data VALUES (22,1), (NULL,2), (NULL,3), (NULL,4);
INSERT INTO data VALUES (33,1), (NULL,2), (NULL,3), (NULL,4);
$  perl -pe ' /(\d+)/ and $x=$1; s/NULL/$x/g ' muscleman.txt
INSERT INTO data VALUES (11,1), (11,2), (11,3), (11,4);
INSERT INTO data VALUES (22,1), (22,2), (22,3), (22,4);
INSERT INTO data VALUES (33,1), (33,2), (33,3), (33,4);
$

Thanks @Sundeep for another shorter answer!

$ perl -pe '($x)=/(\d+)/; s/NULL/$x/g' muscleman.txt
INSERT INTO data VALUES (11,1), (11,2), (11,3), (11,4);
INSERT INTO data VALUES (22,1), (22,2), (22,3), (22,4);
INSERT INTO data VALUES (33,1), (33,2), (33,3), (33,4);
$
stack0114106
  • 8,534
  • 3
  • 13
  • 38