bash脚本中grep单行的shell脚本

时间:2021-08-10 00:11:46

I have a file with the following format:

我有一个格式如下的文件:

123 2 3 48 85.64 85.95
Park ParkName Location
12 2.2 3.2 48 5.4 8.9

Now I could like to write a shell script to extract lines from this file. The first item from each line is a kind of flag. For different flags, I will make different process. See my code below:

现在我想写一个shell脚本来从这个文件中提取行。每行的第一项是一种旗帜。对于不同的旗帜,我会做出不同的过程。请参阅下面的代码:

head= ` echo "$line" | grep -P "^\d+" `    
if [ "$head" != "" ]; then  
 (do something...)  
fi  
head=` echo "$line" | grep -P "^[A-Z]+" `  
if [ "$head" != "" ]; then  
 (do something...)  
fi

The code works. But I dislike the complicated way of writing 2 "if". I would like to have something simple like:

代码有效。但我不喜欢写“2”的复杂方式。我想要一些简单的东西:

if [ "$head" != "" ]; then  
 (do something...)  
elif [ "$head" != "" ]; then  
 (do something...)  
fi

Any thoughts?

有什么想法吗?

2 个解决方案

#1


1  

How about pure bash solution? Bash has a built-in regexp functionality, that you trigger with ~ character. Be aware though that processing huge files with bash read line will not yield in optimal performance..

纯粹的bash解决方案怎么样? Bash有一个内置的正则表达式功能,你用〜字符触发。请注意,使用bash读取行处理大型文件不会产生最佳性能。

#!/bin/bash

file=$1

while read line
do
  echo "read [$line]"
  if [[ $line =~ ^[0-9] ]]; then
    echo '  Processing numeric line'

  elif [[ $line =~ ^[A-Za-z]  ]]; then
    echo '  Processing a text line'
  fi
done < $file

#2


0  

How about this. I guess it would fulfill your requirement

这个怎么样。我想它会满足你的要求

file

文件

123 2 3 48 85.64 85.95

123 2 3 48 85.64 85.95

Park ParkName Location

Park ParkName位置

12 2.2 3.2 48 5.4 8.9

12 2.2 3.2 48 5.4 8.9

script.sh

script.sh

while read line

do

echo $line | grep -qP "^\d+" && echo "Line starts with Numbers"    
echo $line | grep -qP "^[a-zA-Z]+" && echo "Line Starts with String"

done < file

Output:-

输出: -

bash script.sh

Line starts with Numbers

行以Numbers开头

Line Starts with String

行以字符串开头

Line starts with Numbers

行以Numbers开头

#1


1  

How about pure bash solution? Bash has a built-in regexp functionality, that you trigger with ~ character. Be aware though that processing huge files with bash read line will not yield in optimal performance..

纯粹的bash解决方案怎么样? Bash有一个内置的正则表达式功能,你用〜字符触发。请注意,使用bash读取行处理大型文件不会产生最佳性能。

#!/bin/bash

file=$1

while read line
do
  echo "read [$line]"
  if [[ $line =~ ^[0-9] ]]; then
    echo '  Processing numeric line'

  elif [[ $line =~ ^[A-Za-z]  ]]; then
    echo '  Processing a text line'
  fi
done < $file

#2


0  

How about this. I guess it would fulfill your requirement

这个怎么样。我想它会满足你的要求

file

文件

123 2 3 48 85.64 85.95

123 2 3 48 85.64 85.95

Park ParkName Location

Park ParkName位置

12 2.2 3.2 48 5.4 8.9

12 2.2 3.2 48 5.4 8.9

script.sh

script.sh

while read line

do

echo $line | grep -qP "^\d+" && echo "Line starts with Numbers"    
echo $line | grep -qP "^[a-zA-Z]+" && echo "Line Starts with String"

done < file

Output:-

输出: -

bash script.sh

Line starts with Numbers

行以Numbers开头

Line Starts with String

行以字符串开头

Line starts with Numbers

行以Numbers开头