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

Unix 如何将水平输入更改为垂直输出,unix,grep,cut,perl,Unix,Grep,Cut,Perl,我正在使用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:Fres

我正在使用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
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
然后把那条线剪下来垂直显示。所以如果我搜索吉尔曼。它将产生:

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

cat在这里,我发布了一个
shell
脚本的替代方案,内容如下:

替换你的线路

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

这与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
有点奇怪,因为我没有弄乱节目的那部分,所以我知道那部分一定是我改的

激光陀螺


添加一些额外的内容,以便我可以保存RLG编辑,而无需其他人的“批准”

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
last\u name=“吉尔曼”

awk-F:-v name=“$last_name””NR==1{for(i=1;不管怎么说,这与korn有关,我有明确的指示,说除了korn不能做任何事?@RandyGilman:我想是的,但是我的
korn
技能还不够好,所以只要等一些大师进来帮你使用你想要的工具就行了。我更新了代码,但是我仍然在程序的一部分中得到一个错误代码。我将edi不要在你的帖子上给你看。@RandyGilman:你没有一个
关闭你的
i | i)
案例。(放在
完成
行之后)。解决这个问题,看看你是否收到不同的错误消息。祝你好运。
  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 
;;
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
#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;;
x|X) echo "The program is ending" ; exit 0;;
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