Home > Blockchain >  Using sed command in shell script for substring and replace position to need
Using sed command in shell script for substring and replace position to need

Time:05-10

I’m dealing data on text file and I can’t find a way with sed to select a substring at a fixed position and replace it.

This is what I have:

X|001200000000000000000098765432|1234567890|TQ

This is what I need:

‘X’,’00000098765432’,’1234567890’,’TQ’

The following code in sed gives the substring I need (00000098765432) but not overwrites position to need

echo “ X|001200000000000000000098765432|1234567890|TQ” | sed “s/ *//g;s/|/‘,’/g;s/^/‘/;s/$/‘/“

Could you help me?

CodePudding user response:

Rather than sed, I would use awk for this.

echo "X|001200000000000000000098765432|1234567890|TQ" | awk 'BEGIN {FS="|";OFS=","} {print $1,substr($2,17,14),$3,$4}'

Gives output:

X,00000098765432,1234567890,TQ

Here is how it works:

FS = Field separator (in the input)

OFS = Output field separator (the way you want output to be delimited)

BEGIN -> think of it as the place where configurations are set. It runs only one time. So you are saying you want output to be comma delimited and input is pipe delimited.

substr($2,17,14) -> Take $2 (i.e. second field - awk begins counting from 1 - and then apply substring on it. 17 means the beginning character position and 14 means the number of characters from that position onwards)

In my opinion, this is much more readable and maintainable than sed version you have.

CodePudding user response:

If you want to put the quotes in, I'd still use awk.

$: awk -F'|' 'BEGIN{q="\047"} {print  q $1 q","q substr($2,17,14) q","q $3 q","q $4 q"\n"}' <<< "X|001200000000000000000098765432|1234567890|TQ"
'X','00000098765432','1234567890','TQ'

If you just want to use sed, note that you say above you want to remove 16 characters, but you are actually only removing 14.

$: sed -E "s/^(.)[|].{14}([^|] )[|]([^|] )[|]([^|] )/'\1','\2','\3','\4'/" <<< "X|0012000000000000000098765432|1234567890|TQ"
'X','00000098765432','1234567890','TQ'

CodePudding user response:

Using sed

$ sed "s/|\(0[0-9]\{15\}\)\?/','/g;s/^\|$/'/g" input_file
'X','00000098765432','1234567890','TQ'

CodePudding user response:

awk -v d1="\047" \
    -v d2="," \
    -v s="3" \
    -v l="17" \
    '{
         gsub(substr($0,s 1,l),"");
         gsub(/[\|]/,d1 d2 d1);
         print d1$0d1
    }' input_file

'X',00000098765432','1234567890','TQ'
  • Related