Home PC Games Linux Windows Database Network Programming Server Mobile  
           
  Home \ Programming \ shell script: the number of characters in the text to print no more than 6 words     - RedHat Linux 5.5 installation process SVN Service Notes (Server)

- SSH security note (Linux)

- Linux user status query, and to unlock locked user (Linux)

- The new task parallel library feature in .NET 4.6 (Programming)

- The bulk batch multiple rows of data collect (Database)

- Nginx + ownCloud + PHP + MySQL to build personal private cloud under CentOS7 (Server)

- How to use secure FTP file transfer (Server)

- Linux system started to learn: Teaches you install Fedora 22 on VirtualBox (Linux)

- Linux systems dmesg command processing failures and system information collected seven usage (Linux)

- Security enhancements in Ubuntu ssh service (Linux)

- The strings in Python reside (Programming)

- Unsafe reboot start (Linux)

- Linux-based Heartbeat high availability configuration httpd service (Server)

- Ubuntu security settings and tools (Linux)

- High-performance JavaScript DOM programming (Programming)

- Try debugfs restore the deleted files ext3 file system (Linux)

- CentOS build JDK environment (Linux)

- CentOS 5.10 installed Oracle 11G R2 (Database)

- Yii2 Advanced Version Copy New Project Problem Solved (Programming)

- Linux and Unix systems really do network more secure (Linux)

 
         
  shell script: the number of characters in the text to print no more than 6 words
     
  Add Date : 2016-05-04      
         
         
         
  Title: shell script: print text characters no more than 6 words, is a business interview questions, topics are as follows:

for loop prints the following sentence is not more than the number of letters in the word. 6;
I am oldboy teacher welcome to oldboy traning class

method 1:

Using an array a, store the text in a word; for cyclic polling, if judging the length of each word printed word or less 6;

[Root @ localhost anglea] # cat 1.sh
#! / Bin / bash
#written by linuxidc @ 2015-10-28
a = (I am oldboy teacher welcome to oldboy traning class)
for ((i = 0; i <$ {# a [*]}; i ++))
do
    if [$ {# a [$ i]} -le 6]
      then
        echo $ {a [$ i]}
    fi
done

The above can also determine if expr length determine string length;

[Root @ localhost anglea] # cat 1.sh
#! / Bin / bash # written by linuxidc @ 2015-10-28
a = (I am oldboy teacher welcome to oldboy traning class)
for ((i = 0; i <$ {# a [*]}; i ++))
do
    if [ `expr length $ {a [$ i]}` -le 6]
    then
        echo $ {a [$ i]}
    fi
done

Another method of the array: directly read the array elements determine the length of the element;

arr = (I am oldboy teacher welcome to oldboy traning class)
for file in $ {arr [@]}
do
    if [$ {# file} -le 6]
    then
        echo $ file
    fi
done

Execution results are as follows:

[Root @ localhost anglea] # sh 1.sh
I
am
oldboy
to
oldboy
class

Method 2:

Use for polling loop word, use wc -L judgment word length, and make a judgment;

[Root @ localhost anglea] # cat 2.sh
#! / Bin / bash # written by linuxidc @ 2015-10-28
for f in I am oldboy teacher welcome to oldboy traning class
do
    [ `Echo $ f | wc -L` -le 6] && echo $ f
done

Execution results are as follows:

[Root @ localhost anglea] # sh 2.sh
I
am
oldboy
to
oldboy
class

Method 3: awk usage of length

[Root @ localhost anglea] # echo "I am oldboy teacher welcome to oldboy traning class" | awk '{for (i = 1; i <= NF; i ++) if (length ($ i) <= 6) print $ i } '
I
am
oldboy
to
oldboy
class

Is there a better way to welcome everyone to learn together and share.
     
         
         
         
  More:      
 
- Linux server security - the web server configuration (Linux)
- C language sorting instance (select, bubble, insert, binary, fast) (Programming)
- Make full use of the Raspberry Pi SD card space (Linux)
- C ++ casts New Standard Comments (Programming)
- Use ISO document production OpenStack used CoreOS mirror (Linux)
- Ubuntu 14.04 Trusty Tahr User How to install Banshee 2.9.1 (Linux)
- Ubuntu Tutorial - Manually install Oracle Java JDK 8 (Linux)
- Python implementation Bursa transition model (Programming)
- Oracle Database High-Risk Vulnerability Warning (Database)
- Python in os.path Magical (Programming)
- Design and implementation of environment sniffer running under Linux (Linux)
- C ++ Object Model Comments (Programming)
- Github with .gitignore ignore specified file (Linux)
- Role Object of registerNatives () method (Programming)
- OpenSUSE installation on CentOS6 (GUI) (Linux)
- MySQL function: group_concat () function (Database)
- C language print various graphic (Programming)
- Android memory optimization of the disk cache (Linux)
- Linux operating system security tools of the Logs (Linux)
- GAMIT10.5 install and update failed Solution (Linux)
     
           
     
  CopyRight 2002-2022 newfreesoft.com, All Rights Reserved.