Sample Header Ad - 728x90

Unix & Linux Stack Exchange

Q&A for users of Linux, FreeBSD and other Unix-like operating systems

Latest Questions

0 votes
1 answers
1953 views
Include spaces and tabs in "awk" search and replace
Another user helped me earlier to fix something I'm doing with awk, where I search for a string at any point in all files and replace two numbers in the same line when I find it. awk -i inplace '/^gene_height/{ $3=sprintf("%.0f",(169+rand()*51));$5=sprintf("%.0f",(169+rand()*51)) }1' * This worked i...
Another user helped me earlier to fix something I'm doing with awk, where I search for a string at any point in all files and replace two numbers in the same line when I find it. awk -i inplace '/^gene_height/{ $3=sprintf("%.0f",(169+rand()*51));$5=sprintf("%.0f",(169+rand()*51)) }1' * This worked in the test files that I made (much fewer tags to read), but then in the actual files I'm trying to change for a Crusader Kings mod it's getting blocked because each line in the config file starts with a space then two tabs. I tried removing the "^" before gene_height and that kind of works, but it removes the space and two tabs from the file which might mess up the format and break the mod. Does anyone know how I can get the above script to read files that start with a space, two tabs, THEN the string "gene_height", and keep the space and two tabs when doing the replacement?
the_pocket_of_big_noob (11 rep)
Jun 26, 2022, 12:43 AM • Last activity: Jun 25, 2025, 08:02 AM
0 votes
2 answers
65 views
One-liner piping from find/xargs with paths including spaces
The following question likely does not relate specifically to Vim. I use a Vim example, as this is where I encounter the issue. Working on Ubuntu, I often open multiple files in Vim using tab pages: ``` $ vim --help | grep tab -p[N] Open N tab pages (default: one for each file) ``` I also use `find`...
The following question likely does not relate specifically to Vim. I use a Vim example, as this is where I encounter the issue. Working on Ubuntu, I often open multiple files in Vim using tab pages:
$ vim --help | grep tab
   -p[N]		Open N tab pages (default: one for each file)
I also use find with xargs and grep -l to obtain a list of files.
find . -type f -name "*.txt" | xargs grep -l "zod"
I can then quickly review the files output by find in vim:
vim -p find . -type f -name "*.txt" | xargs grep -l "zod"
The earlier grep command would fail if there are spaces in the files or directories, so -print0 can be added to the arguments to find; and -0 can be added to the arguments to xargs. The following creates a MWE set of sample files and directories including spaces:
echo zod > xx.txt && echo zod > 'x x.txt' && mkdir aa && echo zod > aa/xx.txt && echo zod > 'aa/x x.txt' && mkdir 'a a' && echo zod > 'a a/xx.txt' && echo zod > 'a a/x x.txt'
The following will then list the 6 text files we expect:
find . -type f -name "*.txt" -print0 | xargs -0 grep -l "zod"
But if I then try to pass the output of this command to vim tab pages (as below), the paths including spaces are split, and opened as 2 existent, and 9 non-existent files. Is there a way to get past the problem?
vim -p find . -type f -name "*.txt" -print0 | xargs -0 grep -l "zod"
I am keen to avoid side-effects such intermediate files or shell/environment variables (such as used in the top answer to a similar question [here](https://unix.stackexchange.com/a/597765)) ; and so I am looking specifically for a single-line command.
user7543 (274 rep)
May 21, 2025, 03:11 PM • Last activity: May 22, 2025, 01:23 PM
419 votes
22 answers
840639 views
How do I trim leading and trailing whitespace from each line of some output?
I would like to remove all leading and trailing spaces and tabs from each line in an output. Is there a simple tool like `trim` I could pipe my output into? Example file: test space at back test space at front TAB at end TAB at front sequence of some space in the middle some empty lines with differi...
I would like to remove all leading and trailing spaces and tabs from each line in an output. Is there a simple tool like trim I could pipe my output into? Example file: test space at back test space at front TAB at end TAB at front sequence of some space in the middle some empty lines with differing TABS and spaces: test space at both ends
rubo77 (30435 rep)
Nov 21, 2013, 01:07 AM • Last activity: May 15, 2025, 08:01 AM
2 votes
1 answers
311 views
How can I take a sub-array in bash of the first N elements of a string array with elements containing spaces?
This question is similar to [this one][1] but it differs from it: Consider this array with string elements which may contain spaces: a@W:$ arr=("eins" "zwei" "eins plus zwei" "vier" "fünf") a@W:$ echo ${#arr[@]} # ok, 5 elements, as expected 5 a@W:$ echo ${arr[3]} # the fourth one, indexing sta...
This question is similar to this one but it differs from it: Consider this array with string elements which may contain spaces: a@W:$ arr=("eins" "zwei" "eins plus zwei" "vier" "fünf") a@W:$ echo ${#arr[@]} # ok, 5 elements, as expected 5 a@W:$ echo ${arr} # the fourth one, indexing starts at 0 vier a@W:$ echo ${arr} # the third one, which contains two blancs eins plus zwei a@W:$ ar2=${arr[@]:0:3} # I only want the firs three of them a@W:$ echo ${ar2[@]} eins zwei eins plus zwei a@W:$ echo ${#ar2[@]} # but they are all glued together into one element 1 a@W:$ **What mus I do to prevent this gluing them all together?** The string containing spaces, "eins plus zwei" shall stay the third element.
Adalbert Hanßen (303 rep)
Mar 24, 2025, 12:47 PM • Last activity: Mar 24, 2025, 11:53 PM
2 votes
1 answers
1918 views
Change to a directory containing a space
I'm using a MacBook with OS X installed. I'm on Terminal trying to go to a directory. So I'm in Home and I use the command `ls` and there is a `VirtualBox VMs` directory. But when I try to do `cd VirtualBox VMs` it says "No such file or directory" Why? Is it because of the space bar in the word? How...
I'm using a MacBook with OS X installed. I'm on Terminal trying to go to a directory. So I'm in Home and I use the command ls and there is a VirtualBox VMs directory. But when I try to do cd VirtualBox VMs it says "No such file or directory" Why? Is it because of the space bar in the word? How can I fix this?
GrangerObliviate (21 rep)
Apr 18, 2016, 12:20 AM • Last activity: Nov 14, 2024, 10:14 PM
1 votes
1 answers
81 views
How can I iterate over the white space separated words returned by a command substition?
I have the following simple shell command: for x in $(echo one two three); do echo $x; done When executed, it simply prints one two three on *one* line, i. e. the result of the command substitution is evaluated as though I'd quote `one two three`, like so: for x in "one two three"; do echo $x; done...
I have the following simple shell command: for x in $(echo one two three); do echo $x; done When executed, it simply prints one two three on *one* line, i. e. the result of the command substitution is evaluated as though I'd quote one two three, like so: for x in "one two three"; do echo $x; done However, I'd rather want the returned text of the command substitution to be returned as individual words, much like for x in one two three; do echo $x; done so that it prints one two three Is this somehow possible?
René Nyffenegger (2321 rep)
Sep 13, 2024, 03:02 PM • Last activity: Sep 14, 2024, 06:45 AM
34 votes
4 answers
23571 views
How to automatically strip trailing spaces on save in Vi and Vim?
Is there a `.vimrc` setting to automatically remove trailing whitespace when saving a file? Ideally (to be safe) I would like to only have this functionality for certain files, e.g. `*.rb`
Is there a .vimrc setting to automatically remove trailing whitespace when saving a file? Ideally (to be safe) I would like to only have this functionality for certain files, e.g. *.rb
Michael Durrant (43563 rep)
May 11, 2013, 12:50 PM • Last activity: Aug 24, 2024, 05:43 PM
3 votes
1 answers
157 views
Showing whitespace in Ed
In Ed I can test for whitespace with this regex: `g/ *$/p`. I don't suppose there is a way of showing whitespace, perhaps by passing the contents of the buffer to another shell command?
In Ed I can test for whitespace with this regex: g/ *$/p. I don't suppose there is a way of showing whitespace, perhaps by passing the contents of the buffer to another shell command?
edman (588 rep)
Jul 25, 2024, 06:16 AM • Last activity: Jul 25, 2024, 09:30 AM
11 votes
4 answers
37654 views
How to print strings separated by TAB in bash?
I am trying to print two string separated by a TAB. I have tried: echo -e 'foo\tbar' printf '%s\t%s\n' foo bar Both of them print: foo bar Where the whitespace between the two is actually 5 spaces (as per selecting the output with mouse in Putty). I have also tried using CTRL+V and pressing TAB when...
I am trying to print two string separated by a TAB. I have tried: echo -e 'foo\tbar' printf '%s\t%s\n' foo bar Both of them print: foo bar Where the whitespace between the two is actually 5 spaces (as per selecting the output with mouse in Putty). I have also tried using CTRL+V and pressing TAB when typing the command, with the same result. What is the correct way to force tab being printed as tab, so I can select the output and copy it to somewhere else, with tabs? And the secondary question: why is bash expanding tabs into spaces? **Update**: Apparently, this is a problem of Putty: https://superuser.com/questions/656838/how-to-make-putty-display-tabs-within-a-file-instead-of-changing-them-to-spaces
Asu (225 rep)
Dec 15, 2018, 04:10 PM • Last activity: Jul 11, 2024, 09:06 AM
5 votes
1 answers
1461 views
zsh/oh-my-zsh: how to make tab completion add no trailing space for non-folder files?
I installed zsh and oh-my-zsh. By default, when using tab completion zsh adds a trailing space for non-folder files. For demonstration I created the following folders/files in a folder named `test_files` and `cd`ed into `test_files`: test_files ├── dir1 │   └── file4 ├── dir2 │  ...
I installed zsh and oh-my-zsh. By default, when using tab completion zsh adds a trailing space for non-folder files. For demonstration I created the following folders/files in a folder named test_files and cded into test_files: test_files ├── dir1 │   └── file4 ├── dir2 │   └── file5 ├── file1 ├── file11 └── file111 First, I pressed l, s, Space, Tab, Tab, Tab, *, Enter in turn and got ls dir2/* as the 1st command. Second, I pressed l, s, Space, Tab, Tab, Tab, Tab, *, Enter in turn and got ls file1 * as the 2nd command. Please see the gif picture below: zsh ls commands For the 2nd command, since "file1" is not a folder, a space was added between "file1" and "*". I don't want the space added between "file1" and "*", because when I mean to delete files staring with "file1" in their names I'll possibly press r, m, Space, -, r, f, Space, Tab, Tab, Tab, Tab, *, Enter in turn, and I think the command will be rm -rf file1*, but the command will actually be rm -rf file1 * and sometimes I may press 'y' without noticing the added space, so I may delete all the folders and files by mistake. Please see the gif picture below: zsh rm command I used to delete many folders and files by mistake. I googled many days for removing that space but have not succeed. I found add-space from zsh document pretty looks what I want, then I tried zstyle ':completion:*' add-space false, but it seems that nonthing changed. I tried only place the following lines in my .zshrc, it still adds that space: autoload -U compinit && compinit zmodload -i zsh/complist zstyle ':completion:*' menu select zstyle ':completion:*' add-space false If I remove zstyle ':completion:*' menu select, the space is not added, but the cursor won't traverse the folders/files when pressing the tab key, so I want to keep menu select in configs. Please see the gif picture below: zsh rm command without menu select I also tried many other configs. Thanks in advance.
user3716849 (53 rep)
Dec 12, 2021, 02:52 PM • Last activity: Jan 6, 2024, 10:34 PM
1 votes
1 answers
61 views
How to use unix `mv` to rename files with unicode spaces(not U+20)?
``` $ ls cn* cn blah blah.txt $ ls cn\ * ls: cannot access 'cn *': No such file or directory $ ls cn*|hexdump -C 00000000 63 6e e2 80 85 62 6c 61 68 c2 a0 62 6c 61 68 2e |cn...blah..blah.| 00000010 74 78 74 0a |txt.| $ mv cn blah blah.txt 'cn blah blah.txt' mv: 'cn blah blah.txt' and...
$ ls cn*
cn blah blah.txt
$ ls cn\ *
ls: cannot access 'cn *': No such file or directory
$ ls cn*|hexdump -C
00000000  63 6e e2 80 85 62 6c 61  68 c2 a0 62 6c 61 68 2e  |cn...blah..blah.|
00000010  74 78 74 0a                                       |txt.|
$ mv cn blah blah.txt 'cn blah blah.txt'
mv: 'cn blah blah.txt' and 'cn blah blah.txt' are the same file
Note: 0xe28085 is the UTF-8 encoding for U+2005(FOUR-PER-EM SPACE) and 0xC2A0 is the UTF-8 encoding for U+A0(NO-BREAK SPACE), and I copy pasted the first argument to mv and that is why 's are not needed around the file name, because there are *no* normal spaces(U+20) in the file name. How do I rename the file so that I can type the file's name with a regular space(i.e. U+20)?
Remi Arntzen (13 rep)
Dec 3, 2023, 07:34 PM • Last activity: Dec 3, 2023, 07:44 PM
7 votes
4 answers
2680 views
Why does a backslash at the end of the line place undue whitespace?
I wanted: ```lang-bash #!/bin/bash cmd --options \ option=value,\ option=value,\ option=value,\ option=value ``` But running with `bash -x` I got: ```sh cmd --options option=value, option=value, option=value, option=value ``` That causes an error. How can I do this so bash doesn't automatically plac...
I wanted:
-bash
#!/bin/bash
cmd --options \
    option=value,\
    option=value,\
    option=value,\
    option=value
But running with bash -x I got:
cmd --options option=value, option=value, option=value, option=value
That causes an error. How can I do this so bash doesn't automatically place this blank space?
rhuanpk (413 rep)
Nov 16, 2023, 01:00 PM • Last activity: Nov 18, 2023, 10:49 AM
0 votes
1 answers
46 views
How to make emacs start in bash by visiting a find command result containing whitespace in its name and double quotes in path?
I'm confronted with an escape challenge: The process: Step 1 - the file retrieval: Within bash, find command will be invoked. Find commands output is 1 single file with a path like this: ```./vacation/the united states/"best times of our lives"/persona notes.org``` Note: • Within the path - 2 times...
I'm confronted with an escape challenge: The process: Step 1 - the file retrieval: Within bash, find command will be invoked. Find commands output is 1 single file with a path like this:
./vacation/the united states/"best times of our lives"/persona notes.org
Note: • Within the path - 2 times double quote signs
"
appear, enclosing the phrase
times of our lives
- Most sub directories contain 1 or more whitespace characters • Within the file name - also, 1 or more whitespace characters appear Step 2 - Emacs launch in bash by visiting the found 1 file by refering to the corresponding find command result: Doing this by only passing the tricky path would work like this
$ emacs -nw './vacation/the united states/"best times of our lives"/persona notes.org'
emacs launches in bash, what is displayed on the screen switches from bash content to an Emacs buffer containing the file content of said file. The final question now is: How to acchieve exactly this with giving the emacs command within bash the find command output mentioned at the beginning? In general substitution is the way to go:
$ emacs -nw $(find ./vacation/ -mmin -700 -type f -iname *.org)
but as the substitute literally is
./vacation/the united states/"best times of our lives"/persona notes.org
the resulting line
$ emacs -nw ./vacation/the united states/"best times of our lives"/persona notes.org
is in need of escaping the whitespace characters and the both double quotes. But how? •
$ emacs -nw '$(find ./vacation/ -mmin -700 -type f -iname *.org)'
- Doesn't do the trick: Emacs starts in bash, but sets up a buffer with the name
-mmin -700 -type f -iname *.org)
Such a file doesn't exist, so the buffer is empty • Replacing the single quotes with double quotes also doesn't work -
$ emacs -nw "$(find ./vacation/ -mmin -700 -type f -iname *.org)"
- *trying it out*… *works* I'm baffled! It DOES work - Well, you know what? That's exactly why I've taken one step back from hacking on it and took another approach - turning to you with this issue, expaining it to you in search of solving advice - so, take all of this as both, specifically as presentation of a certain problem and its solution, and in general as inspiration how to solve a problem ahead of you, which put you in a deadlock - taking a step back, trying different approaches, among others seeking advice of others, explaining the problem to others, questioning oneself, doing the stuff excluded by you in the first place is very helpful, is a way for you to overcome your current limitations. - You may have asked yourself: 'Why not simply opening that found file with extension of the respective find invocation by a fitting -exec option?' The answer: Then the process would be find invocation, not the emacs invocation, and as a result you have worse access to emacs. For example aliases set up by you to resume the current emacs session suspended before wouldn't work, because the job listing just contains the find job - containing emacs job - but the alias relies on an emacs job to be stand-alone.
starquake
Oct 12, 2023, 01:06 PM • Last activity: Oct 12, 2023, 07:25 PM
55 votes
4 answers
144602 views
Removing all spaces, tabs, newlines, etc from a variable?
This is the error I am getting and it's failing because of a variable whose value is supposed to be 2 (I am getting this using a `select * from tabel`). I am getting spaces in that variable. + 0 != 2 ./setjobs[19]: 0: not found. How do I remove all those spaces or a newline from that variable? Can `...
This is the error I am getting and it's failing because of a variable whose value is supposed to be 2 (I am getting this using a select * from tabel). I am getting spaces in that variable. + 0 != 2 ./setjobs: 0: not found. How do I remove all those spaces or a newline from that variable? Can tr, sed, or anything help? This what I am doing: set_jobs_count=$(echo "set heading off; select count(*) from oppar_db where ( oppar_db_job_name, oppar_db_job_rec ) in ($var) ;" | \ sqlplus -s ${OP_ORA_USER}/${OP_ORA_PASS}@$OPERATIONAL_DB_NAME) --- This works as suggested: | sed 's/[[:space:]]//g' But I still obtain a value like : set_jobs_count= 2
munish (8227 rep)
Feb 24, 2012, 12:28 PM • Last activity: Oct 9, 2023, 10:33 AM
0 votes
3 answers
1931 views
Remove new line, space from file
I have many files in a directory each like so: AAA AA AAAAAA A AAAA I want to end up with this: AAAAAAAAAAAAAAAA So that when I run: find ./ -name '*' -exec wc -m {} + I get back 16, not 20+ depending on how many new line/spaces are counted. Basically, I want to remove EVERYTHING from a file unless...
I have many files in a directory each like so: AAA AA AAAAAA A AAAA I want to end up with this: AAAAAAAAAAAAAAAA So that when I run: find ./ -name '*' -exec wc -m {} + I get back 16, not 20+ depending on how many new line/spaces are counted. Basically, I want to remove EVERYTHING from a file unless it is a letter.
Chris (1 rep)
Apr 10, 2019, 04:06 PM • Last activity: Jul 10, 2023, 05:44 PM
0 votes
3 answers
3019 views
sed: remove extra whitespace to single whitespace between strings while leaving leading tabs intact
I have a code: 1 /** 2 a b c 3 **/ 4 int main() { 5 int x; 6 if ( condition) { 7 return x; 8 } 9 } I need to change multiple whitespaces between tokens or strings to single whitespace eg in line 7 but comments (line 2) should not be affected nor the leading tabs in the code. So, the output should be...
I have a code: 1 /** 2 a b c 3 **/ 4 int main() { 5 int x; 6 if ( condition) { 7 return x; 8 } 9 } I need to change multiple whitespaces between tokens or strings to single whitespace eg in line 7 but comments (line 2) should not be affected nor the leading tabs in the code. So, the output should be: 1 /** 2 a b c 3 **/ 4 int main() { 5 int x; 6 if ( condition) { 7 return x; 8 } 9 } I tried using 'tr': ~$ tr -s " " < file but it changed line 2 as well as removed leading tabs in line 5 to line 8. Can it be done using sed?
Windy Day (33 rep)
Sep 24, 2018, 03:27 AM • Last activity: Jun 20, 2023, 01:57 AM
1 votes
1 answers
99 views
Expand tabs in file with utf8 characters
I use `expand` to expand tabs to spaces. For `utf8` files `expand` doesn't work correctly. E.g. in `ć\ta` tab is expanded to 6 spaces while in `a\ta` to 7 spaces. How do I make it work for `utf8` files?
I use expand to expand tabs to spaces. For utf8 files expand doesn't work correctly. E.g. in ć\ta tab is expanded to 6 spaces while in a\ta to 7 spaces. How do I make it work for utf8 files?
Marcin Kr&#243;l (253 rep)
Jun 6, 2023, 04:09 PM • Last activity: Jun 6, 2023, 08:18 PM
7 votes
4 answers
15144 views
Bash for loop with string var containing spaces
In my directory I have two files with space, `foo bar` and `another file`. I also have two files without space, `file1` and `file2`. The following script works: for f in foo\ bar another\ file; do file "$f"; done This script also works: for f in 'foo bar' 'another file'; do file "$f"; done But the f...
In my directory I have two files with space, foo bar and another file. I also have two files without space, file1 and file2. The following script works: for f in foo\ bar another\ file; do file "$f"; done This script also works: for f in 'foo bar' 'another file'; do file "$f"; done But the following script doesn't work: files="foo\ bar another\ file" for f in $files; do file "$f"; done Not even this script works: files="'foo bar' 'another file'" for f in $files; do file "$f"; done But, if the files do not contain space, the script works: files="file1 file2" for f in $files; do file "$f"; done Thanks! ## Edit Code snippet of my script: while getopts "i:a:c:d:f:g:h" arg; do case $arg in i) files=$OPTARG;; # ... esac done for f in $files; do file "$f"; done With files without spaces, my script works. But I would like to run the script passing files with spaces as argument in one of these ways: ./script.sh -i "foo\ bar another\ file" ./script.sh -i foo\ bar another\ file ./script.sh -i "'foo bar' 'another file'" ./script.sh -i 'foo bar' 'another file'
Pedro Siqueira (183 rep)
Apr 8, 2021, 04:07 PM • Last activity: May 24, 2023, 07:42 AM
0 votes
3 answers
753 views
Adding 10 whitespace characters using sed
I have written a script that replaces a string, but I need to add the 10 whitespace characters back. I *can* add it back by pressing space 10 times, and that works, but this looks hideous in my script. My script: `sed -i 's/server.os[ \t]*$freebsd/server.os $linux/g' ~/var/log.txt` I would like to a...
I have written a script that replaces a string, but I need to add the 10 whitespace characters back. I *can* add it back by pressing space 10 times, and that works, but this looks hideous in my script. My script: sed -i 's/server.os[ \t]*$freebsd/server.os $linux/g' ~/var/log.txt I would like to add the 10 whitespace characters between server.os and $linux using something like {whitespace*10} Note: - There are 10 whitespace characters in front of $freebsd which is why I use [ \t]* - $ is not a variable in this case. It's just part of the string. Sample input: server.os $freebsd Desired output: server.os $linux
user898458 (61 rep)
Apr 17, 2023, 06:35 PM • Last activity: Apr 18, 2023, 01:08 PM
378 votes
6 answers
419234 views
Why does my shell script choke on whitespace or other special characters?
… or an introductory guide to robust filename handling and other string passing in shell scripts. I wrote a shell script which works well most of the time. But it chokes on some inputs (e.g. on some file names). I encountered a problem such as the following: * I have a file name containing a space `...
… or an introductory guide to robust filename handling and other string passing in shell scripts. I wrote a shell script which works well most of the time. But it chokes on some inputs (e.g. on some file names). I encountered a problem such as the following: * I have a file name containing a space hello world, and it was treated as two separate files hello and world. * I have an input line with two consecutive spaces and they shrank to one in the input. * Leading and trailing whitespace disappears from input lines. * Sometimes, when the input contains one of the characters \[*?, they are replaced by some text which is actually the names of some files. * There is an apostrophe ' (or a double quote ") in the input, and things got weird after that point. * There is a backslash in the input (or: I am using Cygwin and some of my file names have Windows-style \ separators). What is going on, and how do I fix this?
Gilles &#39;SO- stop being evil&#39; (862317 rep)
May 24, 2014, 03:25 AM • Last activity: Mar 6, 2023, 10:25 PM
Showing page 1 of 20 total questions