如果可能的话,我想与linux命令结合使用,所有以大写字母开头的单词,但不包括该行开头的单词。目标是在这些单词之间创建边缘。 例如:
My friend John met Beatrice and Lucio.
我想要的结果应该是:
我设法通过正则表达式获得了所有以大写字母开头的单词,不包括该行开头的单词。正则表达式为:
*cat gov.json | grep -oP "\b([A-Z][a-z']*)(\s[A-Z][a-z']*)*\b | ^(\s*.*?\s).*" > nodes.csv*
节点设法在列中分别输入它们,即:
现在的目标是在以大写字母开头的名称之间创建可能的组合,并将其放入文件中。有什么建议吗?
答案 0 :(得分:6)
如果输出中对的顺序无关紧要:
$ cat tst.awk
BEGIN { FS="[^[:alpha:]]+"; OFS=", " }
{
for (i=2; i<=NF; i++) {
if ($i ~ /^[[:upper:]]/) {
words[$i]
}
}
}
END {
for (word1 in words) {
for (word2 in words) {
if (word1 != word2) {
print word1, word2
}
}
delete words[word1]
}
}
$ awk -f tst.awk file
Beatrice, Lucio
Beatrice, John
Lucio, John
如果订单很重要,那么:
$ cat tst.awk
BEGIN { FS="[^[:alpha:]]"; OFS=", " }
{
for (i=2; i<=NF; i++) {
if ($i ~ /^[[:upper:]]/) {
if ( !seen[$i]++ ) {
words[++numWords] = $i
}
}
}
}
END {
for (word1nr=1; word1nr<=numWords; word1nr++) {
word1 = words[word1nr]
for (word2nr=word1nr+1; word2nr<=numWords; word2nr++) {
word2 = words[word2nr]
print word1, word2
}
}
}
$ awk -f tst.awk file
John, Beatrice
John, Lucio
Beatrice, Lucio
在上面,file
包含原始输入,例如My friend John met Beatrice and Lucio.
答案 1 :(得分:4)
这是另一个执行任务的<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:card_view="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent">
<RelativeLayout
android:id="@+id/marcas"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:id="@+id/fondo_usuario"
android:layout_width="match_parent"
android:layout_height="120dp"
android:scaleType="centerCrop"
app:srcCompat="@drawable/surf" />
<de.hdodenhof.circleimageview.CircleImageView
android:id="@+id/profile_image"
android:layout_width="100dp"
android:layout_height="100dp"
android:layout_below="@id/fondo_usuario"
android:layout_centerInParent="true"
android:layout_marginLeft="5dp"
android:layout_marginTop="-60dp"
android:src="@drawable/ic_account_circle_black_24dp"
card_view:civ_border_color="#FF000000"
card_view:civ_border_width="2dp" />
<TextView
android:id="@+id/username"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@id/profile_image"
android:layout_marginTop="10dp"
android:text="Marc "
android:textAlignment="center"
android:textSize="24sp"
android:textStyle="bold" />
<TextView
android:id="@+id/ubicacion"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@id/username"
android:layout_marginTop="5dp"
android:text="Barcelona, Spain"
android:textAlignment="center"
android:textSize="18sp"
android:textStyle="bold" />
<TextView
android:id="@+id/mates"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/fondo_usuario"
android:layout_marginLeft="25dp"
android:layout_marginTop="45dp"
android:text="100 mates"
android:textAlignment="center"
android:textSize="14sp"
android:textStyle="bold" />
<ImageView
android:id="@+id/ambassador"
android:layout_width="30dp"
android:layout_height="30dp"
android:layout_below="@id/fondo_usuario"
android:layout_marginLeft="-40dp"
android:layout_marginTop="10dp"
android:layout_toRightOf="@id/profile_image"
card_view:srcCompat="@drawable/home_ambassador" />
<LinearLayout
android:id="@+id/linearsports"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@id/ubicacion"
android:layout_centerHorizontal="true"
android:layout_marginStart="20dp"
android:layout_marginTop="5dp"
android:layout_marginEnd="20dp"
android:layout_marginBottom="20dp"
android:foregroundGravity="clip_horizontal"
android:gravity="center_horizontal"
android:orientation="horizontal">
<ImageView
android:id="@+id/sp1"
android:layout_width="30dp"
android:layout_height="30dp"
android:layout_margin="5dp"
card_view:srcCompat="@drawable/home_ambassador" />
<ImageView
android:id="@+id/sp2"
android:layout_width="30dp"
android:layout_height="30dp"
android:layout_margin="5dp"
card_view:srcCompat="@drawable/home_ambassador" />
<ImageView
android:id="@+id/sp3"
android:layout_width="30dp"
android:layout_height="30dp"
android:layout_margin="5dp"
android:layout_marginLeft="-60dp"
card_view:srcCompat="@drawable/home_ambassador" />
<ImageView
android:id="@+id/sp4"
android:layout_width="30dp"
android:layout_height="30dp"
android:layout_margin="5dp"
android:layout_marginLeft="-60dp"
card_view:srcCompat="@drawable/home_ambassador" />
<ImageView
android:id="@+id/sp5"
android:layout_width="30dp"
android:layout_height="30dp"
android:layout_margin="5dp"
android:layout_marginLeft="-60dp"
card_view:srcCompat="@drawable/home_ambassador" />
<ImageView
android:id="@+id/sp6"
android:layout_width="30dp"
android:layout_height="30dp"
android:layout_margin="5dp"
android:layout_marginLeft="-60dp"
card_view:srcCompat="@drawable/home_ambassador" />
<ImageView
android:id="@+id/sp7"
android:layout_width="30dp"
android:layout_height="30dp"
android:layout_margin="5dp"
android:layout_marginLeft="-60dp"
card_view:srcCompat="@drawable/home_ambassador" />
<ImageView
android:id="@+id/sp8"
android:layout_width="30dp"
android:layout_height="30dp"
android:layout_margin="5dp"
android:layout_marginLeft="-60dp"
card_view:srcCompat="@drawable/home_ambassador" />
</LinearLayout>
<android.support.design.widget.AppBarLayout
android:id="@+id/appbar_layout_id"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@id/linearsports">
<android.support.design.widget.TabLayout
android:id="@+id/tab_layout_id"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@id/linearsports"
app:tabGravity="fill"
app:tabMode="fixed" />
<android.support.v4.view.ViewPager
android:id="@+id/viewpager_id"
android:layout_width="match_parent"
android:layout_height="wrap_content">
</android.support.v4.view.ViewPager>
</android.support.design.widget.AppBarLayout>
</RelativeLayout>
</android.support.design.widget.CoordinatorLayout>
脚本,在读取输入时构建输出。
awk
允许重复的名称。
script.awk
如果不允许重复的名称,则BEGIN {FPAT = " [[:upper:]][[:alpha:]]+"}
{
for (i = 1; i <= NF; i++ ) {
for (name in namesArr) {
namePairsArr[pairsCount++] = namesArr[name] $i;
}
namesArr[namesCount++] = $i;
}
}
END {for (i = 0; i < pairsCount; i++) print namePairsArr[i];}
为:
script.awk
运行
BEGIN {FPAT = " [[:upper:]][[:alpha:]]+"}
{
for (i = 1; i <= NF; i++ ) {
if (nameSeenArr[$i]) continue;
nameSeenArr[$i] = 1;
for (name in namesArr) {
namePairsArr[pairsCount++] = namesArr[name] $i;
}
namesArr[namesCount++] = $i;
}
}
END {for (i = 0; i < pairsCount; i++) print namePairsArr[i];}**
样本输入文件:
awk -f script.awk gov.json > nodes.csv
示例输出:
My friend John met Beatrice and Lucio
My friend Johna met Beatricea and Lucioa
答案 2 :(得分:2)
如果要使用所有可能的名称对,则假设您已创建包含以下内容的 names.txt :
John
Beatrice
Lucio
我会在bash中尝试:
$ for n in `cat names.txt`
> do for m in `cat names.txt`
> do if [ $m != $n ]; then
> echo $n, $m
> fi
> done
> done
John, Beatrice
John, Lucio
Beatrice, John
Beatrice, Lucio
Lucio, John
Lucio, Beatrice
答案 3 :(得分:2)
将结果存储在数组中时,可以循环使用
names=( John Beatrice Lucio )
max=${#names[@]}
for ((i1=0; i1<max; i1++)); do
for ((i2=i1+1; i2<max; i2++)); do
echo "${names[$i1]}, ${names[$i2]}"
done
done
当您不需要太多名称(长度为arglist)时,可以使用
set -- John Beatrice Lucio
for a; do
shift
for b; do
printf "%s, %s\n" "$a" "$b"
done
done
答案 4 :(得分:1)
如果唯一的问题是字符串的第一个单词,则只需在表达式中添加.
即可:
.([A-Z][a-z']+)
否则我们将添加一个空格作为左边界:
(\s+)([A-Z][a-z']+)(\s+)?
我们想要的单词在该捕获组中:
([A-Z][a-z']+)
但是,如果发生以下情况,这将失败:
My friend Alice O'Neal met Beatrice and Lucio
,我们可以为其扩展所需的输出组。
如果不需要此表达式,并且希望对其进行修改,请访问regex101.com上的此链接。
jex.im可视化正则表达式: