17
votes
UNIX command to add comma and space
With perl:
perl -pe 's/\n/, / unless eof' IP
Add the -i option to edit the file inplace. Or -i.orig to edit it in place but keep the original as IP.orig.
With the zsh shell, you can also load those ...
13
votes
Accepted
Strange variable scope behavior when calling function recursivly
Do I get it right that you want to count all files in a directory tree, or something like that? I.e. you want the variable count to be global?
Your issue is here:
find $1 | while read line; do
See ...
12
votes
how to format a text file in bash with dots from right
Using printf you could use the single quote ' FLAG for the numbers to be grouped with thousands' grouping characters. In the man pages of printf(3):
For some numeric conversions a radix character ("...
10
votes
Progress indicator when using a for loop to parse txt file
You could store the items in an array first to count them, and then just print the number of processed items and the total on each loop iteration.
E.g. something like this:
items=( $(cat items.txt) )
...
10
votes
Accepted
Reason for processing `systemctl status` via command substitution?
There’s no reason, other than possibly ignoring systemctl’s exit status (that would depend on the surrounding setup). There might be historical factors explaining why the command is written that way, ...
10
votes
how to format a text file in bash with dots from right
Another solution is to use numfmt which is included in Debian variants by default
$ WIDTH=20
$ <file.txt tr ' ' '\n' | LC_ALL=el_GR.UTF-8 numfmt --grouping --padding=$WIDTH
1.941.102.556
...
8
votes
Easy command line method to determine specific ARM architecture string?
In case dpkg is not installed in your system (for example in lean docker container), this command will helps you
$ arch
8
votes
How to sort files by size in bash script?
If switching to zsh is an option, then you can just do:
for file in $directory/**/*.opus(ND.OL); do
something with $file
done
Or just:
for file ($directory/**/*.opus(ND.OL)) something with $file
...
8
votes
How to sort files by size in bash script?
If you want to sort in byte-sizes, you should ask du for the byte size (-b) instead of a human readable form (-h).
find "$directory" -type f \( -name "*.opus" \) -exec du -b {} \; |...
7
votes
Accepted
SSH session termination on script error when sourcing multiple sub-scripts
The reason this is happening is because you are sourcing the master script. I can reproduce the behavior you describe using these two toy examples:
terdon@oregano ~ $ cat ~/scripts/foo.sh
#!/bin/bash
...
7
votes
UNIX command to add comma and space
With the GNU implementation of sed:
sed -z 's/\n*$//;s/\n/, /g;s/$/\n/' IP
7
votes
UNIX command to add comma and space
Perhaps
perl -e 'print join(", ", map { chomp; $_ } <>), "\n"' IP
Or
awk '{ printf "%s%s", c, $0; c = ", " } END { print "" }' IP
Or for a ...
7
votes
Extracting role from string in variable
You could use a case statement:
case $var in
(Role) Role=$var
;;
(*Role) Role=${var%Role}
;;
(*) # do some error handling
;;
esac
Alternatively you could remove the Role suffix ...
6
votes
Progress indicator when using a for loop to parse txt file
Similarly to what ilkkachu suggested, you can just read with a while loop:
#!/bin/bash
# File name of domain list: One FQDN per line in file.
domain_list='domains.txt'
number=$( wc -l < "$...
6
votes
UNIX command to add comma and space
With bash, zsh, or ksh:
( x=$(cat IP) && echo "${x//$'\n'/, }" > IP )
Create a subshell to prevent the x variable from leaking out
Set x to the contents of the IP file, including ...
6
votes
Reason for processing `systemctl status` via command substitution?
In general, echo "$(foo)" is silly and pretty much equivalent to just foo. The difference there is that the command substitution removes all trailing newlines from output of foo, and echo ...
6
votes
Tool(s) for echoing to a bash script only once if script is run more than once
Before I show you how to solve your original question, first let me show you the proper way:
If you have root permissions, you can simply install the files into the directories intended for this ...
6
votes
Accepted
How do I programmatically check if a Flatpak package is installed in a shell script?
Just ask flatpak for info on the package, suppress all output and check the return value:
appid=com.play0ad.zeroad
flatpak info "${appid}" >/dev/null 2>&1 && ...
5
votes
bash script to run a python command with arguments in batch
Replace test with "${filename%.faa}" to get the name of the file with .faa removed. You should also quote "${filename}" to avoid problems in case of filenames with spaces.
#!/bin/...
4
votes
Accepted
How and why does using redirection or writing files within an if statement affect exit code?
$?
In the PARAMETERS SET BY THE SHELL section of man zshparam, or info zsh 'Parameters Set By The Shell' you'll see that, $? is the exit status returned by the last command.
$(...)
In man zshexpn or ...
4
votes
How to improve script for redacting and copying a file
First off, always quote your variables and this goes double if they store file names, since they can have any character except NUL (\0) and /. You're also doing some unnecessary movement and creation ...
4
votes
UNIX command to add comma and space
Using awk:
awk '{printf "%s%s", (NR==1) ? "" : ", ", $0}END{print ""}'
This will prefix every line except for the first with a comma.
4
votes
Accepted
Extracting strings from (BASIC) file
Using Raku (formerly known as Perl_6)
~$ raku -ne '.put for .comb( / \" <-["]>+? \" /);' file
OR:
~$ raku -ne '.put for .comb( / \" ~ \" <-["]>+? /);' ...
4
votes
Extracting strings from (BASIC) file
The -P option stands for Perl, and uses Perl regexes, so where it's not available, you could just use Perl directly...
% perl -lne 'print $& while /".*?"/g' file2.txt
" WE'VE ...
4
votes
Extracting strings from (BASIC) file
Using any awk in any shell on every Unix box:
$ awk -F'"' '{for (i=2; i<NF; i+=2) print FS $i FS}' file
" WE'VE CAPTURED A BIGGER SHIP!"
" WE'RE TRANSFERRING TO IT NOW."
&...
4
votes
Accepted
Zsh: What is the idiomatic way of terminating lines in a script? (semicolon, new-line)
The Lexer/Parser doesn't see these as different.
You never just randomly join lines, that doesn't make the least sense, so you're not really solving a real-world issue there.
So all differences are ...
4
votes
Ensuring Distinguishable Exit Codes for Shell Scripts
Usually one prints an error message along with returning a falsy status:
# a.sh
if ! b.sh foo bar; then
echo >&2 "a.sh: b.sh failed"
exit 1
fi
if something else; then
echo ...
4
votes
how to format a text file in bash with dots from right
Given this input:
$ cat file
1941102556 1750145810 2604905 7000793682 5160065824 3000350768 6449300295 3046118928 12693055728 257664864 13471943769
there's no need to run tr and/or generate a temp ...
4
votes
Accepted
show which of a list of filenames do not exist in a given list of directories
It's a lot easier in zsh:
If you have your list of file names in filenames.txt and list of directories in directories.txt:
#! /bin/zsh -
typeset -U expected=( ${(f)"$(<filenames.txt)"} )
...
3
votes
defining variables inside function
FILE_CORE_NAME="$(/usr/bin/env basename $(/usr/bin/env awk -F "." '{print $1}' <<< "${FILE_TO_PROCESS}"))"; local FILE_CORE_NAME;
Even when reformatted on ...
Only top scored, non community-wiki answers of a minimum length are eligible
Related Tags
scripting × 4504bash × 1547
shell-script × 1161
shell × 734
linux × 664
text-processing × 287
awk × 284
sed × 185
files × 177
command-line × 136
ssh × 135
grep × 135
cron × 104
find × 88
variable × 82
ubuntu × 75
terminal × 73
python × 73
rename × 68
perl × 66
ksh × 63
debian × 61
networking × 56
zsh × 54
regular-expression × 53