首页 \ 问答 \ 从字符串内删除多余的空格(Remove excess whitespace from within a string)

从字符串内删除多余的空格(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
更新时间:2023-12-28 10:12

最满意答案

您可以使用从命令行传入的变量:第一个$2 ,第二$2$2等。看起来这里有两个变量, file.csvDataAudit.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, the file.csv and DataAudit.txt

If you replace file.csv with $1 and DataAudit.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选 ...
  • 只需删除read line周围的[] - 它们应该用于执行测试(文件存在,字符串为空等)。 Just remove the [] around read line - they should be used to perform tests (file exists, string is empty etc.).
  • 在/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永远不会将数据作为代码进行评估,但它会做一些可以让你认为它做的事情(即分词和通配)。 由于\是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); ...
  • 咱们试试吧 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

相关文章

更多

最新问答

更多
  • h2元素推动其他h2和div。(h2 element pushing other h2 and div down. two divs, two headers, and they're wrapped within a parent div)
  • 创建一个功能(Create a function)
  • 我投了份简历,是电脑编程方面的学徒,面试时说要培训三个月,前面
  • PDO语句不显示获取的结果(PDOstatement not displaying fetched results)
  • Qt冻结循环的原因?(Qt freezing cause of the loop?)
  • TableView重复youtube-api结果(TableView Repeating youtube-api result)
  • 如何使用自由职业者帐户登录我的php网站?(How can I login into my php website using freelancer account? [closed])
  • SQL Server 2014版本支持的最大数据库数(Maximum number of databases supported by SQL Server 2014 editions)
  • 我如何获得DynamicJasper 3.1.2(或更高版本)的Maven仓库?(How do I get the maven repository for DynamicJasper 3.1.2 (or higher)?)
  • 以编程方式创建UITableView(Creating a UITableView Programmatically)
  • 如何打破按钮上的生命周期循环(How to break do-while loop on button)
  • C#使用EF访问MVC上的部分类的自定义属性(C# access custom attributes of a partial class on MVC with EF)
  • 如何获得facebook app的publish_stream权限?(How to get publish_stream permissions for facebook app?)
  • 如何防止调用冗余函数的postgres视图(how to prevent postgres views calling redundant functions)
  • Sql Server在欧洲获取当前日期时间(Sql Server get current date time in Europe)
  • 设置kotlin扩展名(Setting a kotlin extension)
  • 如何并排放置两个元件?(How to position two elements side by side?)
  • 如何在vim中启用python3?(How to enable python3 in vim?)
  • 在MySQL和/或多列中使用多个表用于Rails应用程序(Using multiple tables in MySQL and/or multiple columns for a Rails application)
  • 如何隐藏谷歌地图上的登录按钮?(How to hide the Sign in button from Google maps?)
  • Mysql左连接旋转90°表(Mysql Left join rotate 90° table)
  • dedecms如何安装?
  • 在哪儿学计算机最好?
  • 学php哪个的书 最好,本人菜鸟
  • 触摸时不要突出显示表格视图行(Do not highlight table view row when touched)
  • 如何覆盖错误堆栈getter(How to override Error stack getter)
  • 带有ImageMagick和许多图像的GIF动画(GIF animation with ImageMagick and many images)
  • USSD INTERFACE - > java web应用程序通信(USSD INTERFACE -> java web app communication)
  • 电脑高中毕业学习去哪里培训
  • 正则表达式验证SMTP响应(Regex to validate SMTP Responses)