如何将水平输入更改为垂直输出

时间:2015-03-04 22:37:52

标签: unix grep cut

我正在使用UNIX Korn shell。我正在尝试在Unix中创建一个程序来搜索文本文件,如下所示:

Last Name:First Name:City:State:Class:Semester Enrolled:Year First Enrolled

Gilman:Randy:Manhattan:KS:Junior:Spring:2010
Denton:Jacob:Rochester:NY:Senoir:Fall:2009
Goodman:Joshua:Warren:MI:Freshman:Summer:2014
Davidson:Blair:Snohomish:WA:Sophmore:Fall:2013
Anderson:Neo:Seattle:WA:Senoir:Spring:2008
Beckman:John:Ft. Polk:LA:Freshman:Spring:2014

然后取出该线并将其剪切并垂直显示。所以,如果我搜索吉尔曼。它会产生:

Gilman
Randy
Manhattan
KS
Junior
Spring
2010

但是,除此之外我还应该能够产生以下布局:

Last Name: Gilman
First Name: Randy
City: Manhattan
State: KS
Class: Junior
Semester Enrolled: Spring
Year First Enrolled: 2010

现在我已经想出了大部分内容,我将在下面的代码中显示:

cat<<MENULIST
                         A - Add Student Information 
                         D - Delete Student Information
                         M - Modify Student Information
                         I - Inquiry on a Student
                         X - Exit

MENULIST

    echo -en '\n'
    echo -en '\n'
    echo "                      Pleasa choose one of the following:                      "

    #take input from operation

    read choice

    case $choice in
        a|A) ;; 
        d|D) ;;
        m|M) ;;
        i|I)   
            #Create Inguiry Message
            clear
            echo "                                 Record Inquiry                                 "
            echo -en '\n'
            echo -en '\n'
            echo "What is the last name of the person"
            #Gather search parameter
            read last_name
            grep -i "$last_name" $1 | cut -f 1-7 -d ':' ;;


        x|X) echo "The program is ending" ; exit 0;;
        *) echo -en '\n' ;echo "Not an acceptable entry." ;echo "Please press enter to try again"; read JUNK | tee -a $2 ; continue
    esac

我真的很难将输入重定向到正确的输出。程序还有很多,但这是这个问题的相关部分。任何帮助将不胜感激。

编辑:

好的最终答案如下所示:

while IFS=: read c1 c2 c3 c4 c5 c6 c7 rest ; do
                case "$c1" in
                    "$last_name" )
                        echo -e "Last Name: $c1\nFirst Name: $c2\nCity: $c3\nState: $c4\nClass: $c5\nSemester Enrolled: $c6\nYear First Enrolled: $c7\n\n"
                    ;;
                esac
            done < $1

这正确地执行了预期结果,谢谢大家的帮助。我还有其他一些问题,但我会为此制作一个新主题,所以它不会混杂在一起。

3 个答案:

答案 0 :(得分:2)

替换你的行

 grep -i "$last_name" $1 | cut -f 1-7 -d ':' ;;

  awk -F: -vnameMatch="$last_name" \
   '$1==nameMatch{
      printf("LastName:%s\nFirstName:%s\nCity:%s\nState:%s\nClass:%s\nSemester Enrolled:%s\nYear First Enrolled:%s\n\n", \
             $1, $2, $3, $4, $5, $6, $7)
    }' $1 
;;

在ksh中它的想法几乎相同。

while IFS=: read c1 c2 c3 c4 c5 c6 c7 rest ; do
  case "$c1" in 
    "$last_name" )
        printf "LastName:%s\nFirstName:%s\nCity:%s\nState:%s\nClass:%s\nSemester Enrolled:%s\nYear First Enrolled:%s\n\n",
             "$c1" "$c2" "$c3" "$c4" "$c5" "$c6" "$c7"
      ;;
   esac
  done < $1

我认为我已经掌握了所有语法,但是今晚没有能量来测试: - / ...如果你可以使用它,并且有问题,发表评论我会清理它起来。

IHTH。

修改

#Case statement for conducting an inquiry of the file
        i|I)   
            #Create Inguiry Message
            clear
            echo "                                 Record Inquiry                                 "
            echo -en '\n'
            echo -en '\n'
            echo "What is the last name of the person:"
            #Gather search parameter
            read last_name
            while IFS=: read c1 c2 c3 c4 c5 c6 c7 rest ; do
                case "$c1" in
                    "$last_name" )
                        printf "Last Name:%s\nFirst Name:%s\nCity:%s\nState:%s\nClass:%s\nSemester Enrolled:%s\nYear First Enrolled:%s\n\n", "$c1", "$c2", "$c3", "$c4", "$c5", "$c6", "$c7"
                    ;;
                esac
            done < $2
        #Case statement for ending the program          
        x|X) echo "The program is ending" ; exit 0;;

错误消息是:

./ asg7s:第26行:第93行的语法错误:`)'意外

第93行

x|X) echo "The program is ending" ; exit 0;;

有点奇怪,因为我没有弄乱程序的这一部分所以我知道它必须是我改变的部分。

RLG

添加额外的东西以便我可以在没有其他“批准”的情况下保存RLG编辑

答案 1 :(得分:1)

awk应该:

last_name="Gilman"

awk -F: -v name="$last_name" 'NR==1 {for(i=1;i<=NF;i++) h[i]=$i} $1==name {for(i=1;i<=NF;i++) print h[i]FS,$i}' file
Last Name: Gilman
First Name: Randy
City: Manhattan
State: KS
Class: Junior
Semester Enrolled: Spring
Year First Enrolled: 2010

它将第一行存储在数组h(标题)中 然后,如果找到模式,则打印出数组和数据。

答案 2 :(得分:0)

在此,我在中发布了shell脚本的替代方案:

perl -F':' -lne '
    BEGIN { $name = pop; }
    $. == 1 and do { @header = @F; next; };
    next if m/^\s*$/;
    if ( $F[0] eq $name ) { 
        for ( $i = 0; $i < @F; $i++ ) { 
            printf qq|%s: %s\n|, $header[$i], $F[$i];
        }   
        exit 0;
    }   
' infile Gilman

它使用-F swith来分割带冒号的字段,-l删除最后一个换行符,-n打开输入文件并逐行处理。

该脚本接受两个参数,但我在处理前提取最后一个参数,假设它是您要搜索的名称。

第一行保存在名为@header的数组中,对于下一行,它比较第一个字段,并在匹配中打印每个标题,然后打印当前行的每个字段并中止该程序。

它产生:

Last Name: Gilman
First Name: Randy
City: Manhattan
State: KS
Class: Junior
Semester Enrolled: Spring
Year First Enrolled: 2010