Home > Software engineering >  How to replace a content on specific patter using sed without losing info?
How to replace a content on specific patter using sed without losing info?

Time:10-30

I have a text file with a bunch of data. I was able to extract exactly what I want using sed; but I need to replaced only the specific pattern I searched without losing the other content from the file.

Im using the following sed command; but I need to the replacement; but dont know how to do it. cat file.txt | sed -rn '/([a-z0-9]{2}\s){6}/p' > output.txt

The sed searches for the following pattern: ## ## ## ## ## ##, but I want to replace that pattern like this: ######-######.

cat file.txt | sed -rn '/([a-z0-9]{2}\s){6}/p' > output.txt
Output:

1         | ec eb b8 7b e3 c0  47                
9         | 90 20 c2 f6 3d c0  1/1/1             
25        | 00 fd 45 3d a7 80  31

Desired Output:

1         | ecebb8-7be3c0  47                
9         | 9020c2-f63dc0  1/1/1             
25        | 00fd45-3da780  31

Thanks

CodePudding user response:

With your shown samples please try following awk program.

awk '
BEGIN{ FS=OFS="|" }
{
  $2=substr($2,1,3) substr($2,5,2) substr($2,8,2)"-" substr($2,11,2) substr($2,14,2) substr($2,17,2) substr($2,19)
}
1
'  Input_file

Explanation: Simple explanation would be, setting FS and OFS as | for awk program. Then in 2nd field using awk's substr function keeping only needed values as per shown samples of OP. Where substr function works on method of printing specific indexes/position number values(eg: from which value to which value you need to print). Then saving required values into 2nd field itself and printing current line then.

CodePudding user response:

With awk:

awk '{$3=$3$4$5"-"$6$7$8; print $1"\t",$2,$3,$NF}' file
1        | ecebb8-7be3c0 47
9        | 9020c2-f63dc0 1/1/1
25       | 00fd45-3da780 31

CodePudding user response:

If you want to extract specific substrings, you'll need to write a more specific regex to pull out exactly those.

sed -rn '/([a-z0-9]{2})\s([a-z0-9]{2})\s([a-z0-9]{2})\s([a-z0-9]{2})\s([a-z0-9]{2})\s([a-z0-9]{2})\s/\1\2\3-\4\5\6/' file.txt > output.txt

Notice also how easy it is to avoid the useless cat.

\s is generally not portable; the POSIX equivalent is [[:space:]].

CodePudding user response:

This might work for you (GNU sed):

sed -E 's/ (\S\S) (\S\S) (\S\S)/ \1\2\3/;s//-\1\2\3/' file

Pattern match 3 spaced pairs twice, removing the spaces between the 2nd and 3rd pairs and replacing the first space in the 2nd match by -.

  • Related