我正在尝试使用缩短我的bash提示的功能。我已将其添加到.bash_profile:
function last_two_dirs {
pwd |rev| awk -F / '{print $1,$2}' | rev | sed s_\ _/_
}
export PS1='$(last_two_dirs) $(__git_ps1) ? '
Run Code Online (Sandbox Code Playgroud)
但是bash: rev: command not found每次启动git bash时都会出错。
我设置PATH正确,因为除以外的其他命令都能正常工作rev。是rev不是git bash的一部分?还是有其他方法仅显示bash提示的父目录和当前目录?
作业系统:Windows 10
假设我在bash中有一个字符串-
NAMES="file1 file2 file3"
Run Code Online (Sandbox Code Playgroud)
如何将其映射到以下字符串,然后将其用作命令的一部分?
MAPPED="-i file1.txt -i file2.txt -i file3.txt"
Run Code Online (Sandbox Code Playgroud)
为了确切说明我的意思,下面是等效的python代码-
names = "file1 file2 file3"
mapped = ' '.join("-i " + x + ".txt" for x in names.split())
Run Code Online (Sandbox Code Playgroud) 目前正在解析一些网站以提高我的 Unix Bash 技能。已提取一个具有以下格式的文件
la-que-no-podia-capitulo-1
la-que-no-podia-capitulo-25
la-que-no-podia-capitulo-30
Run Code Online (Sandbox Code Playgroud)
并且想要到达这一步
la-que-no-podia-capitulo-001
la-que-no-podia-capitulo-025
la-que-no-podia-capitulo-030
Run Code Online (Sandbox Code Playgroud)
有人可以帮助我吗?我尝试过不同的方法:
Bash 正则表达式
x='a-que-no-me-dejas-capitulo-10'
re='((([[:alpha:]]+(-))+)[[:digit:]]+)'
if [[ $x =~ $re ]]
then
echo The regex matches!
echo ${BASH_REMATCH[*]}
fi
Run Code Online (Sandbox Code Playgroud)
但不幸的是它没有分割最后一个数字。
AWK
awk -F'-' '{ printf "%04d: \n", $NF }' output_downloads >output_downloads2
head output_downloads2
0001:
0002:
0003:
0004:
0050:
Run Code Online (Sandbox Code Playgroud)
我无法提取第一部分。
Linux bash脚本:
#!/bin/bash
function Print()
{
echo $1
}
var="*"
Print $var
Run Code Online (Sandbox Code Playgroud)
执行结果:
alex@alex-linux:~/tmp$ ./sample-script
sample-script
Run Code Online (Sandbox Code Playgroud)
*被扩展到文件列表,实际上是脚本本身.如何防止这种情况并查看实际变量值?在一般的情况下,var可以比更复杂的*,例如:home/alex/mydir/*.
我使用Matlab代码,并希望在行之间插入Bash命令以选择特定文件.
du --max-depth 1 |
awk -v q='"' '$1 < 30000000 && $2 != "." {sub(/^[0-9\t ]+/, "", $0); print q $0 q}'
Run Code Online (Sandbox Code Playgroud)
这个命令在shell中运行良好,但我很困惑如何在Matlab代码行之间使用它.
我想在Perl脚本中执行下面的一行,以匹配一行与变量的值,owner并type从文件中删除它:
perl -i -ne\"print unless /\b${owner}\b/ and /\b${type}\b/;\" /tmp/test
Run Code Online (Sandbox Code Playgroud)
内容/tmp/test:
node01 A 10.10.10.2
node02 A 10.20.30.1
Run Code Online (Sandbox Code Playgroud)
当我在shell中执行时,这完全正常,但在Perl脚本中也不起作用.
我曾尝试使用反引号,system而且exec.似乎没什么用.
`perl -i -ne\"print unless /\b${owner}\b/ and /\b${type}\b/;\" /tmp/test`
system(q(perl -i -ne\"print unless /\b${owner}\b/ and /\b${type}\b/;\" /tmp/test));
Run Code Online (Sandbox Code Playgroud)
是否可以在Perl脚本中执行Perl一个内衬?
如果是这样,我在这里做错了什么?
注意:我不需要使用sed,grep,awk等从文件中删除一行的解决方案.
我有一个自定义存储,我想实现ListModel以QComboBox. 为简单起见,让我们假设我们有一个int模型数据列表,所以这是我的模型和测试程序的实现:
#include <QApplication>
#include <QDebug>
#include <QAbstractListModel>
#include <QComboBox>
#include <QHBoxLayout>
#include <QPushButton>
QList<int> list;
class MyModel : public QAbstractListModel
{
public:
MyModel( QWidget* parent ):
QAbstractListModel( parent )
{}
~MyModel()
{
qDebug() << __FUNCTION__;
}
QVariant data(const QModelIndex &index, int role) const
{
if ( !index.isValid() )
return QVariant();
if ( ( role == Qt::DisplayRole ) && ( index.row() < list.size() ) )
return QString::number( list.at( index.row() ) );
return QVariant();
} …Run Code Online (Sandbox Code Playgroud) 我遇到了这个问题:
#include <stdio.h>
void change_number(int *x);
int main()
{
int x;
printf("Enter the number x: ")
scanf("%d", &x);
printf("In the main program: x = %d\n", x);
change_number(&x);
printf("In the main program: x = %d\n", x);
return 0;
}
void change_number(int *x)
{
x = *x+3;
printf("In the subroutine: x = %d\n", x);
}
Run Code Online (Sandbox Code Playgroud)
预期产量:
Enter the number x: 555
In the main program: x = 555
In the subroutine: x = 558
In the main program: x = 558
Run Code Online (Sandbox Code Playgroud)
两个笔记: …
我正在尝试创建一个简单的bash脚本,它使用命令行参数作为awk的参数.
文件格式如下:
id|Name|birthday|joinDate
Run Code Online (Sandbox Code Playgroud)
并且命令行输入:
./my_script -f filename.dat -id 1234
Run Code Online (Sandbox Code Playgroud)
当我使用以下代码行时:
awk -v arg ="$4", -F"|" '{if(arg == $1)print $1,$2}' "$2"
Run Code Online (Sandbox Code Playgroud)
我收到一条错误消息:
'awk: 'arg' argument to '-v' not in 'var=value' form'.
Run Code Online (Sandbox Code Playgroud)
这有什么问题?我想要的只是打印给定ID的特定列.
快速的.fasta文件背景,从第一行开始的每一行开始>,之后我们有标题名称.文件中没有其他地方我们可以找到>.由于有时合并2个fasta文件会导致非唯一的标题名称,我想要一个简单的脚本,使每个标题名称都是唯一的.
我有:
for i in {1..4013}; do awk '/>/{c++;if(c=='"$i"'){sub(">",">'"$i"'_")}}1' Combined_Pass_2D_nanocorrect_round1_renamed.fasta > tmp.fasta; \
rm -rf Combined_Pass_2D_nanocorrect_round1_renamed.fasta; \
mv tmp.fasta Combined_Pass_2D_nanocorrect_round1_renamed.fasta; done
Run Code Online (Sandbox Code Playgroud)
你可能猜到这需要很长时间,但它确实可以解决问题.我曾经grep -c找到标题的数量,并确定它是4013.
有更快的方法吗?