我有带有周期表元素的文本文件,如下所示
1 H *Name of element in 1 language* *Second l-ge* *Third* etc
2 He *Name of element in 1 language* *Second l-ge* *Third* etc
etc(用不同人类语言的元素名称)
我需要删除除1种目标语言之外的所有单词,为此,我需要知道如何将字符串与字符数组进行比较(这样,如果没有一个数组字母与文本文件中的比较词匹配,这个单词就不会被打印出来)。
有人能帮忙吗?
更多细节/我如何试图解决这个问题:
我遇到的第一个问题是,我不知道如何将VAR与字符数组进行比较:
我写了这样的东西:
#!/bin/sh
#PeriodicTable1
counter=0
while [ "$counter" -le "$#" ] ; do
counter=$(( $counter+1 )) ;
if [ "${$counter}" == *[1234567890abcdefghigklmn and etc, so here all the characters from the language that is not removing right now]* ] ; then
echo "${$counter}" ;
done运行./
G 211。
因此,如果文件中的文本是这样的:1 H Hydrogen Wasserstoff Hydrogène 氢,如果我只需要保存中文,那么我将输入终端./PeriodicTable1.sh 1 H Hydrogen Wasserstoff Hydrogène 氢,并期望输出如下:通过删除单词(作为命令的选项)中的所有其他东西的1 H 氢字符将不匹配汉字->不会被打印出来。
然后我写了这样的东西
#!/bin/sh
#PeriodicTable2
for WORD in (and here is the whole text) ; do
if [ "$WORD" == *[straight line of characters]* ; echo "$WORD" ;
done然后
#!/bin/sh
#PeriodicTable3
counter=1 ;
save={1,2,3,4,5,6,7,8,9,0,a,b,c,d, etc} ;
while [ "$counter" -le "$#" ] ; case "${$counter}" in
*"$save"*)
echo ${$counter} ;
counter=$(( $counter+1 )) ;
;
esac然后
#!/bin/sh
#PeriodicTable4
counter=1 ;
save={a,b, etc}
while [ "${0+$counter}" -le "$#" ] ; {
if [ "${0+$counter}" == *"$save"* ] echo "${0+$counter}" ;
counter=$(( $counter+1 )) ;
}但是这些脚本例程中的任何一个都不起作用。
第二个问题是,如果需要保存一个nonEnglish语言,那么元素的字母就会消失;但是我想我可以用它来处理,如果第一个问题解决方案是建立在……
更新:
解决方案仍未找到,但我发现该任务已经可以由
awk '{print $number_of_word1_in_line, $n2, $n3}'
tr -c a-zA-Z '\n' < someFile.txt | sed '/^$/d'
发布于 2022-11-03 21:27:42
我认为你是在寻找符合我在下面所写内容的逻辑。注意:我对输入文件进行了重新格式化,因为最好将一个固定字符(在任何字段中都不会使用)作为分隔符。
作为练习,我创建了自己的原始表,填充了更多的语言(在LibreOffice ODS文件中,然后用条形图导出;如果需要,我可以提供),我从该表重新构建了修改后的表,其中使用了垂直栏(\)作为安全分隔符。
逻辑还被扩展为使用第一行将输入格式值分配给"cols“数组,这样做允许脚本适应输入文件的格式(列赋值/内容的变化),但仍然使用- language标志根据指定的语言输出。
如果感兴趣,我提取的信息来源于相应的页面。
最后,波斯语是right-to-left,,与你桌子上的其他人相反,它来自left-to-right.打印语句根据列位置执行上下文驱动的,但是可以在第二个标题行上提供该信息,并相应地对输出格式进行解析。
希望这能满足你的需要!
#!/bin/sh
### QUESTION: https://stackoverflow.com/questions/74234471/linux-bash-how-to-compare-var-with-characters-pool
DBG=0
while [ $# -gt 0 ]
do
case $1 in
--language ) tLANG=$2 ; shift ; shift ;;
--debug ) DBG=1 ; shift ;;
* ) echo "\n\t Invalid option specified on the command line. Only valid: [ --language {} ] \n Bye!\n" ; exit 1 ;;
esac
done
if [ -z "${tLANG}" ] ; then echo "\n\t Language specification required on the command line. \n\n\t Usage: $0 --language [ English | Italian | Greek | Polish | French | Persian ]\n" ; exit 1 ; fi
### Format of ELEMENTS_TABLE
#Z Symbol English Italian Greek Polish French Persian
ELEMENTS_TABLE="Elements_table.csv"
### Format of ELEMENTS_RAW
#1 |2 |3___________________
#____|4 |5 |6 |7 |8 |9 |10 |11 |12 |13 |14 |15 |16 |17 |18 ||20 |21 |22 |23
#Atomic Number|Symbol|Atomic Mass (amu, g/mol)|Latin|ALTERNATE|English|Greek|German|French|Italian|Spanish|Polish|Turkish|Russian|Afrikaans|Sesotho|Chinese|Hindi||Japanese|Hebrew|Arabic|Persian
### Position Mapping for Requestor: 1 2 6 10 7 12 9 23
#ELEMENTS_RAW="Elements_details.csv"
#awk -F \| '{
# printf("%s|%s|%s|%s|%s|%s|%s|%s|\n", $1, $2, $6, $10, $7, $12, $9, $23 ) ;
#}' < "${ELEMENTS_RAW}" >"${ELEMENTS_TABLE}"
### Match on language position
#echo $tLANG
awk -F \| -v lang="${tLANG}" -v debug="${DBG}" '\
BEGIN{
getline header ;
if( debug == 1 ){
print header ;
} ;
n=split( header, cols, "|") ;
if( debug == 1 ){
for( pos=1 ; pos <= 8 ; pos++ ){
print cols[pos] ;
} ;
} ;
#cols[1]="Z" ;
#cols[2]="Symbol" ;
#cols[3]="English" ;
#cols[4]="Italian" ;
#cols[5]="Greek" ;
#cols[6]="Polish" ;
#cols[7]="French" ;
#cols[8]="Persian" ;
lCOL=0 ;
for( pos=1 ; pos <= 8 ; pos++ ){
if( debug == 1 ){
print "cols[", pos, "] = ", cols[pos] ;
} ;
if( cols[pos] == lang ){
lCOL=pos ;
} ;
} ;
if( lCOL == 8 ){
printf("%-4s %-6s %15s\n", cols[1], cols[2], cols[lCOL] ) ;
}else{
printf("%-4s %-6s %-s\n", cols[1], cols[2], cols[lCOL] ) ;
} ;
}{
#printf("%-4s %-3s %-s %-10.7f\n", $1, $2, $lCOL, $3 ) ;
if( lCOL == 8 ){
printf("%-4s %-6s %15s\n", $1, $2, $lCOL ) ;
}else{
printf("%-4s %-6s %-s\n", $1, $2, $lCOL ) ;
} ;
}' <${ELEMENTS_TABLE}https://stackoverflow.com/questions/74234471
复制相似问题