从字符串内删除多余的空格(Remove excess whitespace from within a string)
我从数据库查询中收到一个字符串,然后在将其放入CSV文件之前删除所有HTML标签,回车符和换行符。 只有这样,我找不到一种方法来从字符串之间移除多余的空格。
什么是删除内部空格字符的最佳方法?
I receive a string from a database query, then I remove all HTML tags, carriage returns and newlines before I put it in a CSV file. Only thing is, I can't find a way to remove the excess white space from between the strings.
What would be the best way to remove the inner whitespace characters?
原文:https://stackoverflow.com/questions/1703320
最满意答案
您可以使用从命令行传入的变量:第一个
$2
,第二$2
为$2
等。看起来这里有两个变量,file.csv
和DataAudit.txt
如果用
$1
替换file.csv
,用$2
替换file.csv
,现在可以通过执行以下操作来执行脚本:./audit.sh myotherfile.csv MyOtherAudit.txt
或者为了提高可读性,通常将它们分配到脚本顶部的命名变量中:
INPUTFILE=$1 OUTPUTFILE=$2
然后,在您的代码中,您可以使用
$INPUTFILE
和$OUTPUTFILE
引用它们You can use variables that are passed in from the command line:
$1
for the first,$2
for the second, etc. It looks like you have two variables here, thefile.csv
andDataAudit.txt
If you replace
file.csv
with$1
andDataAudit.txt
with$2
, you can now execute your script by doing:./audit.sh myotherfile.csv MyOtherAudit.txt
Alternatively for more readability, it is common to assign these into named variables at the top of your script:
INPUTFILE=$1 OUTPUTFILE=$2
Then, in your code you can reference these with
$INPUTFILE
and$OUTPUTFILE
相关问答
更多-
在黑暗中修改单个值的野生刺伤: sed -c -i "s/\($TARGET_KEY *= *\).*/\1$REPLACEMENT_VALUE/" $CONFIG_FILE 假设目标键和替换值不包含任何特殊的正则表达式字符,并且键值分隔符为“=”。 请注意,-c选项取决于系统,您可能需要省略此选项才能执行sed。 有关如何进行类似替换的其他技巧(例如,当REPLACEMENT_VALUE中有'/'字符时), 这里有一些很好的例子 。 A wild stab in the dark for modifyi ...
-
您可以使用从命令行传入的变量:第一个$2 ,第二$2为$2等。看起来这里有两个变量, file.csv和DataAudit.txt 如果用$1替换file.csv ,用$2替换file.csv ,现在可以通过执行以下操作来执行脚本: ./audit.sh myotherfile.csv MyOtherAudit.txt 或者为了提高可读性,通常将它们分配到脚本顶部的命名变量中: INPUTFILE=$1 OUTPUTFILE=$2 然后,在您的代码中,您可以使用$INPUTFILE和$OUTPUTFIL ...
-
根据注释,您确定wget命令的输出包含wget 生成的文本,而不是您指定的URL中的文件内容。 根据wget man页面: -o logfile --output-file=logfile Log all messages to logfile. The messages are normally reported to standard error. 这不是你想要的,所以是的,这是一个wget使用错误。 相反,您可能要么使用curl ,它以您期望的方式使用-o选 ...
-
帮助Bash脚本(Help with Bash script)[2024-02-11]
只需删除read line周围的[] - 它们应该用于执行测试(文件存在,字符串为空等)。 Just remove the [] around read line - they should be used to perform tests (file exists, string is empty etc.). -
BASH grep脚本(BASH grep script)[2022-03-01]
在/etc/passwd或/etc/group中查找包含输入用户名的任何行: #!/bin/bash USERLOOK=$(grep -h "$1" /etc/passwd /etc/group) if [ -z "$1" ] || [ -z "${USERLOOK}" ]; then echo "User not found." else echo "$USERLOOK" fi Finds any lines in /etc/passwd or /etc/group that con ... -
然后马克贝克! 回答: /media/mes_test/my_script.bash: #!/bin/bash export path1="/media/mes_test/2.txt" link=`/usr/bin/php << EOF EOF` echo "$link" /media/mes_test/index.php: 结果在屏幕上: /me ...
-
ls -la 告诉你关于他们的一切,你只需要检查第一个字符。 例如: lrwxrwxrwx 1 kent kent 12 Apr 8 18:54 imlink -> /fpath/foo -rw-r--r-- 1 kent kent 242 Apr 10 16:40 file drwxr-xr-x 2 kent kent 40 Apr 10 17:21 d/ 在上面的例子中 l: link -: file d: dir 你也可以写这个awk行: ls -la | awk '{t=(/^ ...
-
Bash脚本引用问题(Bash script quote issues)[2022-12-06]
Bash永远不会将数据作为代码进行评估,但它会做一些可以让你认为它做的事情(即分词和通配)。 由于\是shell语法的一部分,因此在展开变量时不会将其重新解释为转义序列。 这是你应该怎么做的: DIR=~/"Folder/With Spaces" # ~ doesn't expand in quotes, and there's no "\". CMD=(find "$DIR" -type f) # use an array to keep track of words # Print the w ... -
1)给出php文件的完整路径 2)传递第二个和第三个参数并知道输出和返回值。 $cmd = "(touch $run_file && java -jar $trimmomatic SE -threads 8 $file $trimmed_file HEADCROP:5 && rm $run_file && php -f /var/www/cloud/confirm.php)"; exec($cmd . " 2>&1", $output, $return_var); print_r($output); ...
-
Gnuplot + bash脚本(Gnuplot + bash script)[2023-07-06]
咱们试试吧 for i in `cat list` ; do cat plot.gnu | sed "s/\(INPUTFILE\|OUTPUTFILE\)/$i/g" | gnuplot done 干杯 Let's try for i in `cat list` ; do cat plot.gnu | sed "s/\(INPUTFILE\|OUTPUTFILE\)/$i/g" | gnuplot done Cheers