我有一个未格式化的文件,我想在每100个字符后放置一个换行符并删除其中的任何其他新行,以便文件看起来具有一致的宽度和可读性
此代码段有助于阅读所有行
while read LINE
do
len=${#LINE}
echo "Line length is : $len"
done < $file
Run Code Online (Sandbox Code Playgroud)
但我如何为角色做同样的事情
想法是这样的:( 只是一个例子,它可能有语法错误,尚未实现)
while read ch #read character
do
chcount++ # increment character count
if [ "$chcount" -eq "100" && "$ch"!="\n" ] #if 100th character and is not a new line
then
echo -e "\n" #echo new line
elif [ "$ch"=="\n" ] #if character is not 100th but new line
then
ch=" " $replace it with space
fi
done < $file
Run Code Online (Sandbox Code Playgroud)
我正在学习bash …
我想像这样创建窗格tmux:
$ tmux new-session -d -s mysession "while true; do sleep 1; ls; done"
$ tmux split-window -h "while true; do sleep 1; ls -l; done"
Run Code Online (Sandbox Code Playgroud)
以这种方式运行,当我取消在窗格(窗口)中运行的命令时,我立即关闭它.如何避免这种行为?
我想grep文件夹中的多个文件.除了像pcap文件和gziped文件这样的大文件之外,我想要大肆渲染它们.所以我想:
$ grep foo !({*pcap*,*gz*})
Run Code Online (Sandbox Code Playgroud)
但这不起作用.因为虽然它避免了gziped文件,但!({*pcap*,*gz*})实际上shell扩展会返回pcap文件.知道如何包含除pcap和gziped文件之外的所有文件吗?
如何获取运行我的shell脚本的终端的窗口ID?
背景:我需要根据运行的虚拟桌面编写脚本来执行某些操作.要获取虚拟桌面(不是当前桌面,我可以在脚本运行时更改它),我需要终端的窗口ID脚本正在运行.如果脚本没有从终端运行,它将失败并显示错误.
我有两个文件,一个数据文件和一个查找文件.
必须通过值更改数据文件的一个字段,该值可以在查找文件中找到.
数据文件如下所示:
2013-04-24;1;0.1635;1.4135
2013-04-24;1;0.9135;1.4135
2013-04-24;2;0.9135;1.4135
Run Code Online (Sandbox Code Playgroud)
查找文件如下所示:
1;2ab1e4c0-de4d-11e2-a934-0f0479162b1b
2;2ab21e90-de4d-11e2-9ce8-d368d9512bad
3;2ab2582e-de4d-11e2-bb5f-6b1f6c4437f8
Run Code Online (Sandbox Code Playgroud)
结果必须是:
2013-04-24 2ab1e4c0-de4d-11e2-a934-0f0479162b1b 0.1635 1.4135
2013-04-24 2ab1e4c0-de4d-11e2-a934-0f0479162b1b 0.9135 1.4135
2013-04-24 2ab21e90-de4d-11e2-9ce8-d368d9512bad 0.9135 1.4135
Run Code Online (Sandbox Code Playgroud)
我知道如何使用awk读取数据文件并转换字段分隔符.
awk 'BEGIN { FS = ";"; OFS = " " } ;
{ print $1, $2, #3, $4 }' $1 > $1.updated
Run Code Online (Sandbox Code Playgroud)
但我不知道在shell脚本中查找变量$ 2的智能方法,并用UUID替换原始值.
查找文件永远不会很大,在极端情况下最多会有1000条记录.
任何bash或perl的解决方案也会受到赞赏.
我想知道是否有一种ls文件在没有给定扩展名的文件夹中?例如,如果我有一个文件夹.cpp和.h文件,我想看看是否有其他类型的文件(如配置文件和make文件),我该如何列出或查找?
谢谢,
我想创建一个postgres备份脚本,但我不想使用postgres用户,因为我所使用的unix系统几乎没有限制.我想要做的是在crontab上以unix系统(网络)的常规用户身份运行此脚本,并在脚本中使用数据库管理员帐户.
为了在unix系统(网络)中明确我有用户foo,现在我们有postgres默认postgres用户的用户,然后对于数据库我们有一个数据库管理员用户my_db_admin.我编写脚本以foo使用my_db_admin脚本中的用户和凭据作为网络用户执行.
这是我开始的,但问题是当我执行脚本时它会给出一个错误,说没有提供密码.
#!/bin/bash
export PASSWORD="MyPassword"
backup_dir="/BACKUP/LOCATION"
# name of the backup file has the date
backup_date=`date +%d-%m-%Y`
# only keep the backup for 30 days (maintain low storage)
number_of_days=30
pg_dump -Fc -Umy_db_admin MyDatabase -w > $backup_dir\_$backup_date
find $backup_dir -type f -prune -mtime +$number_of_days -exec rm -f {} \;
Run Code Online (Sandbox Code Playgroud)
这是错误:
pg_dump: [archiver (db)] connection to database "MyDatabase" failed: fe_sendauth: no password supplied
Run Code Online (Sandbox Code Playgroud) 如何在linux中检查文件所有者
我正在尝试运行此bash文件
#!/bin/bash
uname2=$(ls -l $1 | awk '{print $3}');
if [ $uname2 == $USER ]
then echo owner
else echo no owner
fi
Run Code Online (Sandbox Code Playgroud)
它给出了错误=='一元运算符的预期.怎么了?ubuntu服务器10.04.
我是shell脚本的新手,我想知道管道和过滤器之间的区别.他们似乎都做同样的事情.甚至格式也是一样的.
ls -l | grep user |cat >user
Run Code Online (Sandbox Code Playgroud)
管道还是过滤器?
我想只列出隐藏的文件和文件夹(以dot开头).当我使用命令
ls .??*
Run Code Online (Sandbox Code Playgroud)
我得到了输出
.gitignore
.git:
COMMIT_EDITMSG FETCH_HEAD HEAD ORIG_HEAD branches config description hooks index info logs objects packed-refs refs
Run Code Online (Sandbox Code Playgroud)
我不想要文件夹中的内容.相反,我正在寻找仅列出文件夹的输出
.gitignore
.git [different color for the folders, like the normal ls]
Run Code Online (Sandbox Code Playgroud)