我在文件中有这一行:
ULNET-PA,client_sgcib,broker_keplersecurities
,KEPLER
Run Code Online (Sandbox Code Playgroud)
我试图摆脱^ M(回车)字符,所以我使用:
sed 's/^M//g'
Run Code Online (Sandbox Code Playgroud)
但是这确实删除了^ M后的所有内容:
[root@localhost tmp]# vi test
ULNET-PA,client_sgcib,broker_keplersecurities^M,KEPLER
[root@localhost tmp]# sed 's/^M//g' test
ULNET-PA,client_sgcib,broker_keplersecurities
Run Code Online (Sandbox Code Playgroud)
我想要获得的是:
[root@localhost tmp]# vi test
ULNET-PA,client_sgcib,broker_keplersecurities,KEPLER
Run Code Online (Sandbox Code Playgroud) 我在下面有这段代码:
echo " \033[33mTitle of the Program\033[0m"
它将颜色变为黄色.
如何使文本"程序的标题"闪烁?
Using the pattern match !("file1") does not work within a bash script but will work on the command line.
For example:
ls !("file1"|"file2")
Run Code Online (Sandbox Code Playgroud)
This will list all files in directory except file1 and file2.
When that line is executed in a script this error is displayed:
./script.sh: line 1: syntax error near unexpected token `('
./script.sh: line 1: ` ls !("file1"|"file2") '
Run Code Online (Sandbox Code Playgroud)
Regardless what is used rm -v !("file1"). The same error takes place. What is going on …
如何测试是否在Bash中声明了关联数组?我可以测试一个变量,如:
[ -z $FOO ] && echo nope
Run Code Online (Sandbox Code Playgroud)
但我似乎不适用于关联数组:
$unset FOO
$declare -A FOO
$[ -z $FOO ] && echo nope
nope
$FOO=([1]=foo)
$ [ -z $FOO ] && echo nope
nope
$ echo ${FOO[@]}
foo
Run Code Online (Sandbox Code Playgroud)
编辑:
谢谢你的答案,两者似乎都有效,所以我让速度决定:
$ cat test1.sh
#!/bin/bash
for i in {1..100000}; do
size=${#array[@]}
[ "$size" -lt 1 ] && :
done
$ time bash test1.sh #best of five
real 0m1.377s
user 0m1.357s
sys 0m0.020s
Run Code Online (Sandbox Code Playgroud)
和另外一个:
$ cat test2.sh
#!/bin/bash
for i in {1..100000}; do
declare …Run Code Online (Sandbox Code Playgroud) 在 vim 中,如果我通过按 进入搜索模式/,我可以按Ctrl+p来查找上次搜索的字符串,然后再次按该组合来查找更早搜索过的字符串,依此类推。我可以在 emacs 模式下在命令行上做同样的事情。
我想对less命令执行相同的操作(即,按/,然后按Ctrl+p查找我之前搜索过的内容,然后按Enter再次搜索该术语。我可以这样做吗?我该怎么做?
编辑:重新措辞以使问题更清楚。
我正在尝试使用sql-mode通过Emacs连接到PostgreSQL数据库.我启动Emacs,命令M-x sql-postgres并提示用户,数据库和服务器,但不提示密码.打开一个空缓冲区,无论我写什么,我得到:
the Password for user james:
psql: FATAL: password authentication failed for user "james"
Run Code Online (Sandbox Code Playgroud)
不过我可以使用psql登录.我在Linux Antergos中运行GNU Emacs 24.4.1,PostgreSQL是9.3.5.
谢谢..
有没有办法在awk-gawk中最有可能将记录分隔符设置RS为空值,以将字符串的每个字符作为单独的记录处理?有点像设置FS为空以分隔其自己的字段中的每个字符:
$ echo abc | awk -F '' '{print $2}'
b
Run Code Online (Sandbox Code Playgroud)
但要将它们分别作为单独的记录分开,例如:
$ echo abc | awk -v RS='?' '{print $0}'
a
b
c
Run Code Online (Sandbox Code Playgroud)
最明显的一个:
$ echo abc | awk -v RS='' '{print $0}'
abc
Run Code Online (Sandbox Code Playgroud)
没有奖励我(因为那个显然是为了每个GNU awk文档的其他东西).
我基本上卡住使用for等?
编辑:
@ xhienne的回答是我正在寻找的,但即使使用它(20个字符和一个可疑的变量A:):
$ echo abc | awk -v A="\n" -v RS='(.)' -v ORS="" '{print(RT==A?NR:RT)}'
abc4
Run Code Online (Sandbox Code Playgroud)
不会帮我缩短我之前使用的代码length.然后,我怎么能赢得Pyth代码+Qfql+Q:: D.
我有一个文件1.txt
$ cat 1.txt
page1
?age1
Run Code Online (Sandbox Code Playgroud)
但:
$ head -n1 1.txt | file -i -
/dev/stdin: text/plain; charset=us-ascii
$ head -n2 1.txt | tail -n1 | file -i -
/dev/stdin: text/plain; charset=utf-8
Run Code Online (Sandbox Code Playgroud)
字符串有不同的字符集.因为它我不能用我知道的方法得到唯一的字符串:
$ cat 1.txt | sort | uniq -c | sort -rn
1 ?age1
1 page1
Run Code Online (Sandbox Code Playgroud)
那么,你能帮助我找到在我的情况下如何获得唯一字符串的方法吗?PS Prefer解决方案只能使用linux命令行/ bash/awk.但是如果你有另一种编程语言的解决方案,我也会喜欢它.
UPD.awk '!a[$0]++' Input_file不工作,pic:
我试图滥用asort()(只是因为)将数组复制src到数组dest,没有问题:
$ awk 'BEGIN {
split("first;second;third",src,";") # make src array for testing
asort(src, dest, "@ind_num_asc") # copy array to dest
for(i in dest)
print i, src[i], dest[i] # output
}'
1 first first
2 second second
3 third third
Run Code Online (Sandbox Code Playgroud)
但有没有办法使用多维数组作为dest数组?就像是:
asort(src, dest[src[1]], "@ind_num_asc") # or dest[src[1]][]
(前者产生second argument not an array,后者syntax error
实际上第一个论点split是$0,我正在尝试将记录分组.)
当然我可以使用for循环但我的大脑仍然坚持测试这个解决方案.