Shell combat: Get column content in a variety of ways

Source: Internet
Author: User

I am not a professional Linux, usually do not work in Linux programming, self-learning some Linux shell programming, forget to learn, learn to forget, efficiency is not high. Today is the right to review it.

Think of a scenario where you get a partial column value for a row.

Consider this input: "Root:x:0:0:root:/root:/bin/bash", now we get user and shell information. 5 ways of realizing what you think of yourself temporarily.

#!/bin/bash############################################## #第二种实现   General circulation ##################################### ######## #line = "Root:x:0:0:root:/root:/bin/bash"; oldifs= $IFS; ifs= ":";d eclare -i count=0; #count =0;for  item in  $line; do[  $count  -eq 0 ] && user= $itemif  [[  $count  -eq 6 ]]; thenshell= $itemfi #let count++count=$[  $count +1  ] #count =$ (($count + 1) #count = ' expr  $count +1 ' #count =$ (expr  $count + 1);d oneifs= $oldIFS; echo 1 :  $user \ ' s shell is   $shell  by process:$$;################################# ############# #第二种实现   with sed command ############################################# #user = ' echo  $line  |sed   ' s/:.*$//'; shell=$ (echo  $line  |sed  ' s/^.*://');echo 2:  $user \ ' S shell  is   $shell  by process:$$;############################################## #第3种实现   with the cut command; ############################################# #user = ' echo  $line  |cut -d: -f1 '; shell=$ (echo  $line  |CUT -D: -F7);echo 3:  $user \ ' s shell is   $shell  by  process:$$;############################################## #第4种实现   xargs using ############################ ################ #echo   $line  |xargs -d: -n 1 |tr -s  ' \ n ' >  File4user= ' cat  file4 | head -n 1 ' shell= ' cat file4 | tail -n  1 ';echo 4:  $user \ ' s shell is   $shell  BY PROCESS:$$;RM -RF  file4 2>>/dev/null############################################## #第5种实现   awk using ###### ####################################### echo  $line  |awk  -F  ': '    ' { print  "5:"  $1  " " s  "  "  shell is " $7 } '

Analysis: The pros and cons of 5 different ways are thrown away. Here are some things to know.

    1. Text Processing tool: SED,TR

    2. Text Column selection Tool: Cut,awk

    3. Text line selection Tool: Head,tail

    4. Several ways to count operations: $[],$ (()), '

    5. Variable declaration: Declare

    6. Parameter handling: Xargs


In fact, in the process of writing, even their own frequently used commands, they can not remember. It seems to be not the same as proficiency.

This article is from a "simple" blog, so be sure to keep this source http://dba10g.blog.51cto.com/764602/1914312

Shell combat: Get column content in a variety of ways

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.