捕获文件,以及单独列,并添加使用某物阵列 [英] capture a file and separate columns and add sth using arrays

查看:142
本文介绍了捕获文件,以及单独列,并添加使用某物阵列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要管理我的 / etc / passwd文件并为每一列中添加姓名。

I want to manage my /etc/passwd and add names for each column..

我想从阵列中读取数据...

I want to read from array...

我试过如下bash的:

What I tried is the following bash:

#!/bin/bash

FILE="/etc/passwd"
arr=( $(awk < $FILE -F: '{print $1 $5 $6 $7}') )

for Username Realname Homedir Loginshell in "${arr[@]}"; do
    result=$(printf 'Username: %s Realname: %s Homedir: %s Loginshell: %s' "$Username" "$Realname" "$Homedir" "Loginshell")
    echo "$result"
done

但有错误:

./u.sh: line 6: syntax error near unexpected token `Realname'
./u.sh: line 6: `for Username Realname Homedir Loginshell in "${arr[@]}"; do'

第二个问题是:

我怎么能读 $文件在这样的脚本参数......因为当改剧本,如:

How can I read $FILE as an argument in such scripts...because when change the script like:

#!/bin/bash

if [ "$#" -ne "1" ]; then
    echo "Usage: `basename $0` <input-file>"
    exit 1
fi

arr=( $(awk < $1 -F: '{print $1 $2 $6 $7}') )

for Username Realname Homedir Loginshell in "${arr[@]}"; do
    result=$(printf 'Username: %s Realname: %s Homedir: %s Loginshell: %s' "$Username" "$Realname" "$Homedir" "Loginshell")
    echo "$result"
done

这将崩溃...

It would crash...

感谢

推荐答案

该方法避免了 AWK ,但使用bash的 IFS 文件分隔字段

This approach avoids awk but using bash's IFS to separate the fields in FILE:

FILE="/etc/passwd"
while IFS=: read Username pass uid gid Realname Homedir Loginshell; do
    result=$(printf 'Username: %s Realname: %s Homedir: %s Loginshell: %s' "$Username" "$Realname" "$Homedir" "$Loginshell")
    echo "$result"
done <"$FILE"

要提供文件名作为参数(如每题2):

To supply the file name as an argument (as per question 2):

if [ "$#" -ne "1" ]; then
    echo "Usage: `basename $0` <input-file>"
    exit 1
fi
FILE=$1

while IFS=: read Username pass uid gid Realname Homedir Loginshell; do
    result=$(printf 'Username: %s Realname: %s Homedir: %s Loginshell: %s' "$Username" "$Realname" "$Homedir" "$Loginshell")
    echo "$result"
done <"$FILE"

如果你想使用数组,然后:

If you want to use arrays, then:

#!/bin/bash
if [ "$#" -ne "1" ]; then
    echo "Usage: `basename $0` <input-file>"
    exit 1
fi
FILE=$1

#declare -a fields
IFS=:
while read line; do
    fields=($line)
    result=$(printf 'Username: %s Realname: %s Homedir: %s Loginshell: %s' "${fields[0]}" "${fields[4]}" "${fields[5]}" "${fields[6]}")
    echo "$result"
done <"$FILE"

以上一次读取 / etc / passwd文件一行。随着 IFS 设置为一个冒号,每行中使用栏=($线),再转换为一个数组。为了避免意外,您可能希望在执行任何庆典 code依赖前 IFS 重置为默认值在庆典的正常字分离规则。

The above reads /etc/passwd one line at a time. With IFS set to a colon, each line is then converted to an array using fields=($line). To avoid surprises, you may want to reset IFS to its default value before executing any bash code that depends on bash's normal word separation rules.

下面是企图pretty印刷输出:

Here is an attempt at pretty-printing the output:

#!/bin/bash
if [ "$#" -ne "1" ]; then
    echo "Usage: `basename $0` <input-file>"
    exit 1
fi
FILE=$1

{
echo "========:========:=======:=========="
echo "Username:Realname:Homedir:Loginshell"
echo "========:========:=======:=========="

IFS=:
while read line; do
    fields=($line)
    echo "${fields[0]}:${fields[4]}:${fields[5]}:${fields[6]}"
done < "$FILE"
} | column -nts:

在默认情况下,分离用空白领域。然而,实名输出字段可以包含空格。因此,在-s 选项用来使输入使用冒号分隔的字段。为了确保标题排队的数据字段,无论是标题和数据被提交给

By default, column separates fields by whitespace. However, the realname output field can contain spaces. So, the -s: option is used so that column uses colon-separated fields on input. To assure that headings lined up with the data fields, both headings and data are submitted to the same instance of column.

这篇关于捕获文件,以及单独列,并添加使用某物阵列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆