如何针对这种情况编写 Bash 脚本?

如何针对这种情况编写 Bash 脚本?

我在目录中有这些文件home/acs

TBBN027162G_38d8-2f08-0c40
TBBN027605G_38d8-2f08-1518
TBBN027941G_38d8-2f08-0d1c
TBBN027923G_38d8-2f08-35e4

我想将其读取为TBBN027162Gservice_id存储到我的数据库中。我还想备份该文件以供将来参考。38d8-2f08-0c40mac_address

下面是我尝试编写的脚本,尚未完成。我不知道如何让它工作。

#!/bin/bash

cd /home/acs/

FILES=*

for f in $FILES
do
    #backup file
    echo "$f" >> /home/suriani/acs/data/mac/maclist

    while read $line
    do
      awk -F '_' '{print $1}' /home/suriani/acs/data/mac/maclist
      #read service_id

      awk -F '_' '{print $2}' /home/suriani/acs/data/mac/maclist
      #read mac_address

    done < /home/suriani/acs/data/mac/maclist

done

输出:

TBBN027162G
38d8-2f08-0c40
TBBN027162G
TBBN027605G
38d8-2f08-0c40
38d8-2f08-1518
TBBN027162G
TBBN027605G
38d8-2f08-0c40
38d8-2f08-1518
TBBN027162G
TBBN027605G
TBBN027923G
38d8-2f08-0c40
38d8-2f08-1518
38d8-2f08-35e4
TBBN027162G
TBBN027605G
TBBN027923G
38d8-2f08-0c40
38d8-2f08-1518
38d8-2f08-35e4
TBBN027162G
TBBN027605G
TBBN027923G
38d8-2f08-0c40
38d8-2f08-1518
38d8-2f08-35e4
TBBN027162G
TBBN027605G
TBBN027923G
TBBN027941G
38d8-2f08-0c40
38d8-2f08-1518
38d8-2f08-35e4
38d8-2f08-0d1c
TBBN027162G
TBBN027605G
TBBN027923G
TBBN027941G
38d8-2f08-0c40
38d8-2f08-1518
38d8-2f08-35e4
38d8-2f08-0d1c
TBBN027162G
TBBN027605G
TBBN027923G
TBBN027941G
38d8-2f08-0c40
38d8-2f08-1518
38d8-2f08-35e4
38d8-2f08-0d1c
TBBN027162G
TBBN027605G
TBBN027923G
TBBN027941G
38d8-2f08-0c40
38d8-2f08-1518
38d8-2f08-35e4
38d8-2f08-0d1c

答案1

在 Python 中

在一个(长)班轮中:

python3 -c 'data = [l.split("_") for l in open("f").read().splitlines()]; [print("service_id: "+d[0]+",", "mac_address: "+d[1]) for d in data]'

其中 'f' 是源文件,位于双引号之间。

输出:

service_id: TBBN027162G mac_adrres: 38d8-2f08-0c40
service_id: TBBN027605G mac_adrres: 38d8-2f08-1518
service_id: TBBN027941G mac_adrres: 38d8-2f08-0d1c
service_id: TBBN027923G mac_adrres: 38d8-2f08-35e4

在脚本文件中写出:

#!/usr/bin/env python3
import sys

f = sys.argv[1]

data = [l.split("_") for l in open(f).read().splitlines()]
for d in data:
    print("service_id: "+d[0]+",", "mac_address: "+d[1])
  • 如果您更喜欢脚本版本,请将其复制到一个空文件中,将其保存为extract_data.py,并使用该文件作为参数运行它:
    python3 /path/to/extract_data.py /path/to/sourcefile

详细来说,“缩小”:

open(f).read().splitlines()

读取文件,将其分成几行

[l.split("_") for l in open(f).read().splitlines()]

创建这些行的列表,按分隔符分为两部分_

for d in data:
    print("service_id: "+d[0]+",", "mac_address: "+d[1])

最后将每行的两个部分与字符串合并:

service_id: 

mac_address: 

编辑

一次读取目录中的多个(所有)文件,我们需要在脚本中再添加两行:

#!/usr/bin/env python3
import sys
import os

dr = sys.argv[1]
# list the directory's files, exclude hidden and backup files:
flist = [dr+"/"+f for f in os.listdir(dr) if not any([f.startswith("."), f.endswith("~")])]
# split the lines into sections:
data = sum([[l.split("_") for l in open(f).read().splitlines()] for f in flist], [])
# generate the print command per (split) line, combine it with the naming strings:
[print("service_id: "+d[0]+",", "mac_adrres: "+d[1]) for d in data]

虽然脚本跳过了隐藏文件和以 结尾的文件~,但它假定目录仅包含可读且有效的文件。

完全按照第一个脚本的方式使用,但现在只需要目录作为参数:

python3 /path/to/extract_data.py /path/to/source_directory

示例输出:

service_id: TBBN027162A, mac_adrres: 38d8-2f08-0c21
service_id: TBBN027605A, mac_adrres: 38d8-2f08-1534
service_id: TBBN027941A, mac_adrres: 38d8-2f08-0d8v
service_id: TBBN027923A, mac_adrres: 38d8-2f08-35e4
service_id: TBBN027162G, mac_adrres: 38d8-2f08-0c40
service_id: TBBN027605G, mac_adrres: 38d8-2f08-1518
service_id: TBBN027941G, mac_adrres: 38d8-2f08-0d1c
service_id: TBBN027923G, mac_adrres: 38d8-2f08-35e4

答案2

用于awk创建 SQL 查询:

find . -type f -maxdepth 1 -exec awk -F_ '{print "INSERT INTO table (service_id, mac_adress) VALUES (\""$1"\",\""$2"\");"}' {} \;

示例输出:

INSERT INTO table (service_id, mac_adress) VALUES ("TBBN027162G", "38d8-2f08-0c40");
INSERT INTO table (service_id, mac_adress) VALUES ("TBBN027605G", "38d8-2f08-1518");
INSERT INTO table (service_id, mac_adress) VALUES ("TBBN027941G", "38d8-2f08-0d1c");
INSERT INTO table (service_id, mac_adress) VALUES ("TBBN027923G", "38d8-2f08-35e4");

或者更完整的版本:

#!/bin/bash

find /home/suriani/acs/data/mac/maclist -maxdepth 1 -type f ! -name "*.bak" -print0 | while IFS= read -r -d '' file; do

    # create the queries
    queries=$(awk -F_ 'print "INSERT INTO table (service_id, mac_adress) VALUES (\""$1"\",\""$2"\");"' "$file");

    # fire the queries, echo is a sample output
    echo "$queries"

    # if database update is ok, move the file
    # mv "$file" "/home/suriani/acs/data/mac/maclist/$file.bak"
done

相关内容