Linked Questions
458 questions linked to/from Why is using a shell loop to process text considered bad practice?
3
votes
2
answers
2k
views
Why I should avoid loops in shells? [duplicate]
The following link recommends against using loops in shells.
bash variables in for loop range
Why is this? Here is an example loop I just happened to be looking at when I
came across that answer:
...
0
votes
3
answers
2k
views
Can’t echo variable value=* in script [duplicate]
Content of myfile:
123
**1
**
Script that attempts to display each word:
for i in $(cat $myfile)
do
echo "$i"
done
the result is when echo *, it lists the files in my current ...
422
votes
22
answers
852k
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 ...
252
votes
12
answers
373k
views
How to remove duplicate lines inside a text file?
A huge (up to 2 GiB) text file of mine contains about 100 exact duplicates of every line in it (useless in my case, as the file is a CSV-like data table).
What I need is to remove all the repetitions ...
138
votes
20
answers
361k
views
How to count the number of a specific character in each line?
I was wondering how to count the number of a specific character in each line by some text processing utilities?
For example, to count " in each line of the following text
"hello!"
Thank you!
The ...
220
votes
8
answers
45k
views
Why is looping over find's output bad practice?
This question is inspired by
Why is using a shell loop to process text considered bad practice ?
I see these constructs
for file in `find . -type f -name ...`; do smth with ${file}; done
and
for ...
282
votes
3
answers
57k
views
Security implications of forgetting to quote a variable in bash/POSIX shells
If you've been following unix.stackexchange.com for a while, you
should hopefully know by now that leaving a variable
unquoted in list context (as in echo $var) in Bourne/POSIX
shells (zsh being the ...
95
votes
9
answers
304k
views
Can sed replace new line characters?
Is there an issue with sed and new line character?
I have a file test.txt with the following contents
aaaaa
bbbbb
ccccc
ddddd
The following does not work:
sed -r -i 's/\n/,/g' test.txt
I ...
58
votes
10
answers
24k
views
What's the fastest way to generate a 1 GB text file containing random digits?
I tried a bash script, but it took too long to create a simple 1 MB file. I think the answer lies in using /dev/random or /dev/urandom, but other posts here only show how to add all kinds of data to a ...
115
votes
4
answers
162k
views
Understanding "IFS= read -r line"
I obviously understand that one can add value to internal field separator variable. For example:
$ IFS=blah
$ echo "$IFS"
blah
$
I also understand that read -r line will save data from stdin to ...
99
votes
5
answers
238k
views
Splitting string by the first occurrence of a delimiter
I have a string in the next format
id;some text here with possible ; inside
and want to split it to 2 strings by first occurrence of the ;. So, it should be: id and some text here with possible ; ...
40
votes
16
answers
118k
views
Bash script to convert all *flac to *.mp3 with FFmpeg?
I want to convert all *.flac to *.mp3 in the specific folder.
This is what I've tried, but not works:
# change to the home directory
cd ~/music
# convert all *.flac files
ffmpeg -i *.flac -acodec ...
93
votes
6
answers
337k
views
How to loop over the lines of a file?
Say I have this file:
hello
world
hello world
This program
#!/bin/bash
for i in $(cat $1); do
echo "tester: $i"
done
outputs
tester: hello
tester: world
tester: hello
tester: world
I'd like to ...
54
votes
8
answers
48k
views
How to display numbers in reverse order using seq(1)?
I have iterate over numbers in various order. I am able to display them in increasing order, even with steps like:
$ seq --separator="," 1 10
1,2,3,4,5,6,7,8,9,10
$ seq --separator="," 1 2 10
1,3,5,7,...
43
votes
15
answers
29k
views
Object-oriented shell for *nix
Preface: I love bash and have no intention of starting any sort of argument or holy-war, and hopefully this is not an extremely naive question.
This question is somewhat related to this post on ...