Regex Sed替换字符串,基于输入文件
想知道是否有更好/更快的方法来实现这一点 基本上,我有一个文件,我需要根据它的一个字段向其中添加更多信息。e、 g 要编辑的文件:Regex Sed替换字符串,基于输入文件,regex,bash,shell,awk,sed,Regex,Bash,Shell,Awk,Sed,想知道是否有更好/更快的方法来实现这一点 基本上,我有一个文件,我需要根据它的一个字段向其中添加更多信息。e、 g 要编辑的文件: USER|ROLE user1|role1 user1|role2 user2|role1 user2|role11 输入文件: Role|Application role1|applicationabc role2|application_qwerty role3|application_new_app_new role4|qwerty_abc_123 role
USER|ROLE
user1|role1
user1|role2
user2|role1
user2|role11
输入文件:
Role|Application
role1|applicationabc
role2|application_qwerty
role3|application_new_app_new
role4|qwerty_abc_123
role11|applicationabc123
最后,我想留下这样的东西:
USER|ROLE|Application
user1|role1|applicationabc
user1|role2|application_qwerty
user2|role11|applicationabc123
user2|role3|application_new_app_new
我的想法是:
cat inputfile | while IFS='|' read src rep
do
sed -i "s#\<$src\>#$src\|$rep#" /path/to/file/filename.csv
done
cat inputfile |而IFS='|'读取src rep
做
sed-i“s\\\\\$src\\$rep”/path/to/file/filename.csv
完成
我写的东西在某种程度上是有效的,但速度很慢。此外,如果它在行中的任何位置找到匹配项,它将替换它。例如,对于user2和role11,脚本将在匹配role11之前匹配role1
因此,我的问题是:
带有
awk的蛋糕
:
$ cat file1
USER|ROLE
user1|role1
user1|role2
user2|role1
user2|role11
$ cat file2
ROLE|Application
role1|applicationabc
role2|application_qwerty
role3|application_new_app_new
role4|qwerty_abc_123
role11|applicationabc123
$ awk -F'\\|' 'NR==FNR{a[$1]=$2; next}; {print $0 "|" a[$2]}' file2 file1
USER|ROLE|Application
user1|role1|applicationabc
user1|role2|application_qwerty
user2|role1|applicationabc
user2|role11|applicationabc123
带有
awk的蛋糕
:
$ cat file1
USER|ROLE
user1|role1
user1|role2
user2|role1
user2|role11
$ cat file2
ROLE|Application
role1|applicationabc
role2|application_qwerty
role3|application_new_app_new
role4|qwerty_abc_123
role11|applicationabc123
$ awk -F'\\|' 'NR==FNR{a[$1]=$2; next}; {print $0 "|" a[$2]}' file2 file1
USER|ROLE|Application
user1|role1|applicationabc
user1|role2|application_qwerty
user2|role1|applicationabc
user2|role11|applicationabc123
通过
加入
:
join -i -t "|" -1 2 -2 1 <(sort -t '|' -k2b,2 file) <(sort -t '|' -k 1b,1 input)
通过
加入
:
join -i -t "|" -1 2 -2 1 <(sort -t '|' -k2b,2 file) <(sort -t '|' -k 1b,1 input)
请尝试以下操作:
awk 'FNR==NR{A[$1]=$2;next}s=$2 in A{ $3=A[$2] }s' FS='|' OFS='|' file2 file1
或:
解释
awk '
# FNR==NR this is true only when awk reading first file
FNR==NR{
# Create array A where index = field1($1) and value = field2($2)
A[$1]=$2
# stop processing and go to next line
next
}
# Here we read 2nd file that is file1 in your case
# var in Array returns either 1=true or 0=false
# if array A has index field2 ($2) then s will be 1 otherwise 0
# whenever s is 1 that is nothing but true state, we create new field
# $3 and its value will be array element corresponds to array index field2
s=$2 in A{
$3=A[$2]
}s
# An awk program is a series of condition-action pairs,
# conditions being outside of curly braces and actions being enclosed in them.
# A condition is considered false if it evaluates to zero or the empty string,
# anything else is true (uninitialized variables are zero or empty string,
# depending on context, so they are false).
# Either a condition or an action can be implied;
# braces without a condition are considered to have a true condition and
# are always executed if they are hit,
# and any condition without an action will print the line
# if and only if the condition is met.
# So finally }s at the end of script
# it executes the default action for every line,
# printing the line whenever s is 1 that is true
# which may have been modified by the previous action in braces
# FS = Input Field Separator
# OFS = Output Field Separator
' FS='|' OFS='|' file2 file1
请尝试以下操作:
awk 'FNR==NR{A[$1]=$2;next}s=$2 in A{ $3=A[$2] }s' FS='|' OFS='|' file2 file1
或:
解释
awk '
# FNR==NR this is true only when awk reading first file
FNR==NR{
# Create array A where index = field1($1) and value = field2($2)
A[$1]=$2
# stop processing and go to next line
next
}
# Here we read 2nd file that is file1 in your case
# var in Array returns either 1=true or 0=false
# if array A has index field2 ($2) then s will be 1 otherwise 0
# whenever s is 1 that is nothing but true state, we create new field
# $3 and its value will be array element corresponds to array index field2
s=$2 in A{
$3=A[$2]
}s
# An awk program is a series of condition-action pairs,
# conditions being outside of curly braces and actions being enclosed in them.
# A condition is considered false if it evaluates to zero or the empty string,
# anything else is true (uninitialized variables are zero or empty string,
# depending on context, so they are false).
# Either a condition or an action can be implied;
# braces without a condition are considered to have a true condition and
# are always executed if they are hit,
# and any condition without an action will print the line
# if and only if the condition is met.
# So finally }s at the end of script
# it executes the default action for every line,
# printing the line whenever s is 1 that is true
# which may have been modified by the previous action in braces
# FS = Input Field Separator
# OFS = Output Field Separator
' FS='|' OFS='|' file2 file1
这还将为您提供标题应用程序
'NR==FNR{a[tolower($1)]=$2;next};{print$0“|”a[tolower($2)]}
^^True。。我入侵了其中一个文件,假设是打字错误…;-)@你能给我解释一下这个命令吗?它到底是如何工作的另外,在测试之后,它似乎只在末尾添加了一个管道,而没有添加应用程序名称。另外,我可以指定它应该匹配哪个字段吗?也就是说,在文件1中,将有多个字段,而不仅仅是示例数据中显示的两个字段(但始终在最后一个字段中)。这还将为您提供标题应用程序'NR==FNR{a[tolower($1)]=$2;next};{print$0“|”a[tolower($2)]}
^^True。。我入侵了其中一个文件,假设是打字错误…;-)@你能给我解释一下这个命令吗?它到底是如何工作的另外,在测试之后,它似乎只在末尾添加了一个管道,而没有添加应用程序名称。另外,我可以指定它应该匹配哪个字段吗?也就是说,在file1中,将有多个字段,而不仅仅是示例数据中显示的两个字段(但始终在最后一个字段中)。我本来打算发布此内容,但看起来有点混乱,+1:)我收到一个错误,说file1未按排序顺序排列。@StevenFalzon file1看起来如何?它必须在连接字段上排序。我本来打算发布这个,但看起来有点乱,尽管是+1:)我收到一个错误,说file1没有按排序顺序排列。@StevenFalzon file1看起来怎么样?它必须按连接字段排序。您能解释一下这些命令及其工作原理吗?您能解释一下这些命令及其工作原理吗?