用于翻译由行中的制表符分隔的查找第三个单词的Shell脚本

时间:2013-09-24 01:28:44

标签: regex perl shell unix

有人可以帮我写ascript翻译每行中的第三个单词,单词用标签分隔。

示例输入:

Hello how Are You
Iam Fine how about

示例输出:

Hello how Ziv You
Iam Fine sld about

每行中的第三个单词应翻译为使用:tr '[abcdefghijklmnopqrstuvqxyz]' '[zyxwvutsrqponmlkjihgfedcba]'

5 个答案:

答案 0 :(得分:3)

鉴于以下内容:

[somedude@dev7 ~]# cat so.txt 
Hello   how Are You
Iam Fine    how about
[somedude@dev7 ~]# 

我跑了:

[somedude@dev7 ~]# cat so.sh 
#!/bin/bash

_INPUT="Hello how Are You
Iam Fine how about"

# read each line from config file
while read -r l 
do

  _GET_THIRD_WORD=$(echo $l | awk '{print $3}')

  echo $_GET_THIRD_WORD | sed -i "s,$_GET_THIRD_WORD,SOMETHINGTOTRANSLATEWITH," 

done < so.txt
[somedude@dev7 ~]# 

这会将每个已翻译的行回显为标准输出。

希望这有帮助!

答案 1 :(得分:2)

只是打击:

#!/bin/bash
while read -ra A; do
    printf "%s\t%s" "${A[0]}" "${A[1]}"
    printf "\t%s" "$(echo "${A[2]}" | tr '[ABCDEFGHIJKLMNOPQRSTUVQXYZabcdefghijklmnopqrstuvqxyz]' '[ZYXWVUTSRQPONMLKJIHGFEDCBAzyxwvutsrqponmlkjihgfedcba]')" "${A[@]:3}"
    echo
done

运行方式:

bash script.sh < input_file

输出:

Hello   how     Aiv     You
Iam     Fine    slw     about

如果您将\t更改为空格():

Hello how Ziv You
Iam Fine slw about

另一个版本:

#!/bin/bash
F=({A..Z} {a..z}) R=({Z..A} {z..a})
while read -ra A; do
    printf "%s\t%s" "${A[0]}" "${A[1]}"
    printf "\t%s" "$(IFS=''; echo "${A[2]}" | tr "[${F[*]}]" "[${R[*]}]")" "${A[@]:3}"
    echo
done

答案 2 :(得分:1)

这非常糟糕,但可以完成工作(在bash shell中)。它在整个输入文件中使用sed的y音译操作符。这通过进程替换传递给awk和存储在数组中的第三个字段。然后,Awk循环遍历原始文件,并用音译值替换第三个字段的每个实例。

awk -F'\t'  -v OFS='\t' 'NR == FNR{a[NR]=$3; next};{$3=a[FNR]; print}' \
<(sed -e 'y/abcdefghijklmnopqrstuvqxyz/zyxwvutsrqponmlkjihgfedcba/' \
-e 'y/ABCDEFGHIJKLMNOPQRSTUVQXYZ/ZYXWVUTSRQPONMLKJIHGFEDCBA/' file) file

答案 3 :(得分:1)

像这样的AWK脚本

#!/usr/bin/awk -f
BEGIN{
    IFS="\t"                                        #input field separator as tab
    CHARSET = "abcdefghijklmnopqrstuvwxyz"      
}

{
    rep_str=""                                      #replacement string
    # loop in through each char of third word
    for(i=1;i<=length($3);i++){
        char = substr($3,i,1)
        loc = index(CHARSET,tolower(char))

        #check to see if the character is actually an alphabet
        if(loc>0){

            #get the reverse location of char in the CHARSET                               
            rep_char = substr(CHARSET,27-loc,1)    

            #change the replacement character to upper case if the original char is uppercase
            if(char~/[A-Z]/){
                rep_char = toupper(rep_char)   
            }
        }else{
            rep_char = char
        }
        rep_str=rep_str rep_char                       #final replacement sting formed by concatenation of replaced char rep_char 
    }
    $3 = rep_str                                           
    print $0    
}

答案 4 :(得分:0)

perl -F'\t' -lane '$F[3] =~ tr/ABCDEFGHIJKLMNOPQRSTUVQXYZabcdefghijklmnopqrstuvqxyz/ZYXWVUTSRQPONMLKJIHGFEDCBAzyxwvutsrqponmlkjihgfedcba/ ; print "@F"' Filename