Bash 设置单词的格式
因此,我上周开始学习bash,我必须完成一项任务,即打印此Bash 设置单词的格式,bash,Bash,因此,我上周开始学习bash,我必须完成一项任务,即打印此文件.txt的内容: 1|george|01/02/2042 2|TPS Reports|03/01/2015 3|Go clubbing this weekend| 4|Metting with family|03/08/2015 5|Help Rose with dating boys| 6|Update hacking software for hacking StackExchange|09/30/2015 我编写了以下代码:
文件.txt的内容:
1|george|01/02/2042
2|TPS Reports|03/01/2015
3|Go clubbing this weekend|
4|Metting with family|03/08/2015
5|Help Rose with dating boys|
6|Update hacking software for hacking StackExchange|09/30/2015
我编写了以下代码:
while IFS='' read -r line || [[ -n $line ]]; do
IFS='|' read -ra ADDR <<< "$line"
echo -e "${ADDR[0]}: ${ADDR[1]} \t\t\t ${ADDR[2]}"
done < "$HOME_DIRECTORY_FILE"
你可能认为这是正确的,但我的导师说日期必须在彼此下方,如下所示:
1: george 01/02/2042
2: TPS Reports 03/01/2015
3: Go clubbing this weekend
4: Metting with family 03/08/2015
5: Help Rose with dating boys
6: Update hacking software for hacking StackExchange 09/30/2015
这在bash中可以实现吗?还是我应该放手?我的指导老师给出了准确的输出示例,以这种方式隔开,并说“输出的格式应整齐(间隔)。使用列
命令。它正是你想要的
例如:
$ cat input.txt
1|george|01/02/2042
2|TPS Reports|03/01/2015
3|Go clubbing this weekend|
4|Metting with family|03/08/2015
5|Help Rose with dating boys|
6|Update hacking software for hacking StackExchange|09/30/2015
$ column --separator \| --table input.txt
1 george 01/02/2042
2 TPS Reports 03/01/2015
3 Go clubbing this weekend
4 Metting with family 03/08/2015
5 Help Rose with dating boys
6 Update hacking software for hacking StackExchange 09/30/2015
您需要进行一些预格式化,以使您的数字具有:
,但这应该是最简单的部分(您可以将修改后的文件导入列
)。使用列
命令。它正是你想要的
例如:
$ cat input.txt
1|george|01/02/2042
2|TPS Reports|03/01/2015
3|Go clubbing this weekend|
4|Metting with family|03/08/2015
5|Help Rose with dating boys|
6|Update hacking software for hacking StackExchange|09/30/2015
$ column --separator \| --table input.txt
1 george 01/02/2042
2 TPS Reports 03/01/2015
3 Go clubbing this weekend
4 Metting with family 03/08/2015
5 Help Rose with dating boys
6 Update hacking software for hacking StackExchange 09/30/2015
您需要进行一些预格式化,以使您的数字具有:
,但这应该是简单的部分(您可以将修改后的文件导入列中)。您也可以使用printf
,尽管它需要您猜测中间列的宽度(其中列为您计算)
而IFS=''读取-r行| |[[-n$line]];做
如果s='|'read-ra ADDR您也可以使用printf
,尽管它需要您猜测中间列的宽度(其中列为您计算)
而IFS=''读取-r行| |[[-n$line]];做
如果S=“|”读-拉地址好答案。您不需要在单引号内转义管道,也不需要从file@glennjackman-好的观点,修正了。我还将-t
替换为--table
以保持一致。它给了我以下信息:column:非法选项---用法:column[-tx][c columns][s sep][file…]@GeorgeChalhoub-看起来您可能使用了不同版本的column
,它不喜欢长选项。只需用-s
替换-分隔符
,用-t
替换-表格
。不过,对于第六个,它给出了错误列:行太长,无法打印。回答得好。您不需要在单引号内转义管道,也不需要从file@glennjackman-好的观点,修正了。我还将-t
替换为--table
以保持一致。它给了我以下信息:column:非法选项---用法:column[-tx][c columns][s sep][file…]@GeorgeChalhoub-看起来您可能使用了不同版本的column
,它不喜欢长选项。只需用-s
替换--separator
,用-t
替换--table
。不过,对于第六个,它给出了这个错误列:行太长,无法打印。
while IFS='' read -r line || [[ -n $line ]]; do
IFS='|' read -ra ADDR <<< "$line"
printf "%d: %-30s %s\n" "${ADDR[0]}" "${ADDR[1]}" "${ADDR[2]}"
done < "$HOME_DIRECTORY_FILE"