English 中文(简体)
在档案中删除以前的重复编号——bash
原标题:Remove preceding duplicate numbers in a file - bash

在以下文本档案中,我将如何删除重复数字,使其看上下文中的“词汇”。 编号为x的“PRODxx”,将保持这一格式。

附录

NET_SalesD_PROD1111,mexico
NET_Sales4_PROD22,newjersy
NET_SalesG_PROD333,bull

词汇表

NET_SalesD_PROD1,mexico
NET_Sales4_PROD2,newjersy
NET_SalesG_PROD3,bull

我曾尝试过像“PROD[1-9]{2,4}”这样的ed鱼集团,但不能让它工作。

最佳回答

利用捕获组收集第一位数字,并参照数据重复。 然后,在替代物中使用同样的背书只生产其中的一种。

sed -E  s/PROD([1-9])1+,/PROD1,/ 
问题回答

假设:

  • all lines contain the string _PROD[0-9]+,
  • we (effectively) want to keep the first number that comes after _PROD

1ed 做法:

$ sed -E  s/(_PROD[0-9])[0-9]*/1/  x
NET_SalesD_PROD1,mexico
NET_Sales4_PROD2,newjersy
NET_SalesG_PROD3,bull

如果有:

  • (_PROD[0-9]) - (first) capture group matches on the string _PROD<single_digit> followed by ...
  • [0-9]* - zero or more digits
  • 1 - replace the match with the (first) capture group

<><><>斯特凡>第1条解决办法:>> 如果你与Perl有这样的ok,那么就使用reg鱼和捕捉团体的能力,并使用贪dy的配对器,然后使用拉齐的配对机实现所需产出。

perl -pe  s|^(.*_)(.*?d).*?(,.*)$|${1}${2}${3}|   Input_file

<><>><>>第2条解决办法:> 使用<代码>wk,使用<代码>match功能,请按代码进行试验。

awk  match($0,/^(.*_[^0-9]+[0-9])[0-9]*(,.*)$/,arr){print arr[1] arr[2]}  Input_file




相关问题
Parse players currently in lobby

I m attempting to write a bash script to parse out the following log file and give me a list of CURRENT players in the room (so ignoring players that left, but including players that may have rejoined)...

encoding of file shell script

How can I check the file encoding in a shell script? I need to know if a file is encoded in utf-8 or iso-8859-1. Thanks

Bash usage of vi or emacs

From a programming standpoint, when you set the bash shell to use vi or emacs via set -o vi or set -o emacs What is actually going on here? I ve been reading a book where it claims the bash shell ...

Dynamically building a command in bash

I am construcing a command in bash dynamically. This works fine: COMMAND="java myclass" ${COMMAND} Now I want to dynamically construct a command that redirectes the output: LOG=">> myfile.log ...

Perform OR on two hash outputs of sha1sum

I want perform sha1sum file1 and sha1sum file2 and perform bitwise OR operation with them using bash. Output should be printable i.e 53a23bc2e24d039 ... (160 bit) How can I do this? I know echo $(( ...

Set screen-title from shellscript

Is it possible to set the Screen Title using a shell script? I thought about something like sending the key commands ctrl+A shift-A Name enter I searched for about an hour on how to emulate ...

热门标签