Sample Header Ad - 728x90

Unix & Linux Stack Exchange

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

Latest Questions

1 votes
4 answers
520 views
How do I print line numbers but reset line counter at empty line?
I have a file.txt containing: ``` this is the first second line not last line fourth but first second in list seventh in file seventh with nl ``` Normally I would just `cat` and pipe `|` it into `nl` like so: ```bash $> cat file.txt | nl 1 this is the first 2 second line 3 not last line 4 fourth but...
I have a file.txt containing:
this is the first
second line
not last line

fourth but first
second in list
seventh in file
seventh with nl
Normally I would just cat and pipe | it into nl like so:
$> cat file.txt | nl
1  this is the first
2  second line
3  not last line

4  fourth but first
5  second in list
6  seventh in file
7  seventh with nl
But I need the line numbers to reset when it encounters an empty line like so:
$> alias_or_function file.txt
1  this is the first
2  second line
3  not last line

1  fourth but first
2  second in list
3  seventh in file
4  seventh with nl
How could I do this using a quick function or alias in my ~/.zshrc?
ntruter42 (197 rep)
Aug 22, 2020, 04:17 AM • Last activity: Mar 3, 2025, 07:36 PM
0 votes
0 answers
680 views
Deleting a hidden file with linux command
I tried using `rm -rf .*` command to remove a hidden file and I am met with this error. ``` rm: refusing to remove '.' or '..' directory: skipping '.' rm: refusing to remove '.' or '..' directory: skipping '..' ```
I tried using rm -rf .* command to remove a hidden file and I am met with this error.
rm: refusing to remove '.' or '..' directory: skipping '.'
rm: refusing to remove '.' or '..' directory: skipping '..'
Agbodike Chetam (1 rep)
Jan 18, 2024, 01:48 PM • Last activity: Jan 18, 2024, 02:15 PM
2 votes
1 answers
78 views
Extract content between first one match and first different match across one or multiple lines
Let's say I want to get the text between the first match of "start_" and the first of "_end", no matter it it's across same or multiple lines. Don't include the matches. Example text 1: This is a start_text with start_and some_end text with_end out text 1: text with start_and some Example text 2: Th...
Let's say I want to get the text between the first match of "start_" and the first of "_end", no matter it it's across same or multiple lines. Don't include the matches. Example text 1: This is a start_text with start_and some_end text with_end out text 1: text with start_and some Example text 2: This is a start_text with start_and some_end text with_end out text 2: text with start_and some I've seen lots of answer but they are line-focused, not file-focused. Any kind of tool or command will do as long as it's console-based.
Smeterlink (295 rep)
Dec 18, 2023, 07:55 AM • Last activity: Dec 18, 2023, 08:37 AM
1 votes
3 answers
737 views
fix slapcat 79 character line wrap in output
I use slapcat to do a full-text search in our very large ldap directory. As it is easier to match what I am looking for when I don't know where to look. The problem is that it wraps long lines slapcat -v | grep -A 1 "some search string" somelongvar::linesoftesttext12345667890987654321234567887654321...
I use slapcat to do a full-text search in our very large ldap directory. As it is easier to match what I am looking for when I don't know where to look. The problem is that it wraps long lines slapcat -v | grep -A 1 "some search string" somelongvar::linesoftesttext12345667890987654321234567887654321234567897654321 wraps like this
nelaaro (1363 rep)
Feb 27, 2018, 12:02 PM • Last activity: Jul 12, 2023, 07:33 AM
2 votes
1 answers
1468 views
Showing line numbers by default in ed
In the `ex` editor, one can show line numbers by default by adding this line to a `.exrc` file: set number Is there a way to have `ed` start with line numbers showing by default, when lines are invoked?
In the ex editor, one can show line numbers by default by adding this line to a .exrc file: set number Is there a way to have ed start with line numbers showing by default, when lines are invoked?
edman (588 rep)
Jul 14, 2021, 02:59 PM • Last activity: Jan 16, 2023, 10:05 AM
5 votes
3 answers
1091 views
Help with Find command: Find files or folders that start with a space (ore more than one space) on linux
How do I do a search for any of the following: Find folders that start with a space on linux and then remove any starting spaces so the folder can start with a letter or a number? Find FILES that start with a space on linux and then remove any starting spaces so the files can start with a letter or...
How do I do a search for any of the following: Find folders that start with a space on linux and then remove any starting spaces so the folder can start with a letter or a number? Find FILES that start with a space on linux and then remove any starting spaces so the files can start with a letter or a number?
JustAl (139 rep)
Sep 24, 2022, 09:03 PM • Last activity: Sep 25, 2022, 02:17 PM
0 votes
1 answers
69 views
How to search an argument/flag/option in man with a line break?
In `man` argument/flag/option chapters generally include a line break, as with: ``` -s silent ``` How could I find only the `-s` which includes a line break after it, just as in the above example? The purpose is to prevent a case where I find an `-s` inside a text block (i.e. without a line break af...
In man argument/flag/option chapters generally include a line break, as with:
-s
    silent
How could I find only the -s which includes a line break after it, just as in the above example? The purpose is to prevent a case where I find an -s inside a text block (i.e. without a line break after it).
Lahor (123 rep)
Mar 13, 2022, 03:55 PM • Last activity: Mar 13, 2022, 05:50 PM
0 votes
0 answers
128 views
FOLD not working on very long lines
I have a fold command as below to split a file with very long line into multiple lines. It works if the file is small. But if the length of the file is too long, it stops adding new line character half way. fold -b -w 3000 > Is there an alternate way to do this? I am on RHEL.
I have a fold command as below to split a file with very long line into multiple lines. It works if the file is small. But if the length of the file is too long, it stops adding new line character half way. fold -b -w 3000 > Is there an alternate way to do this? I am on RHEL.
Ram (25 rep)
Feb 24, 2022, 07:37 PM • Last activity: Feb 24, 2022, 07:43 PM
30 votes
10 answers
27216 views
How to remove empty lines from beginning and end of file?
I would like to remove empty lines from the beginning and the end of file, but not remove empty lines between non-empty lines in the middle. I think `sed` or `awk` would be the solution. Source: 1: 2: 3:line1 4: 5:line2 6: 7: 8: Output: 1:line1 2: 3:line2
I would like to remove empty lines from the beginning and the end of file, but not remove empty lines between non-empty lines in the middle. I think sed or awk would be the solution. Source: 1: 2: 3:line1 4: 5:line2 6: 7: 8: Output: 1:line1 2: 3:line2
Feriman (1069 rep)
Nov 14, 2019, 03:04 PM • Last activity: Aug 30, 2021, 11:23 AM
2 votes
3 answers
3843 views
How to make text wrap with diff -y?
When I use `diff -y` to compare two text files with long lines of text, the result cuts off each column and I cannot read an entire line. I would like instead the text to wrap (or fold as it is also called) so that I can read the whole line. For example, say I have two simple text files one line eac...
When I use diff -y to compare two text files with long lines of text, the result cuts off each column and I cannot read an entire line. I would like instead the text to wrap (or fold as it is also called) so that I can read the whole line. For example, say I have two simple text files one line each:
cat tmp.txt tmp2.txt
The quick brown fox jumped over the lazy dog. This line is very long and will not show when I use a diff. 
The quick brown fox did not jump over the lazy dog. This line is very long and will not show when I use a diff.
diffing them I get,
diff -y tmp.txt tmp2.txt
The quick brown fox jumped over the lazy dog. This line is ve |	The quick brown fox did not jump over the lazy dog. This line
which cuts off the lines in the middle. I know about the -W option to make it wider, but the lines are longer than my screen width so this does not help. Using a simple diff does make the text wrap, but there is no text wrapping with diff -y. So how can I use diff -y with text wrapping? I'm using the terminal on a Mac, if that helps. This seems like a simple thing to want, but after searching everywhere I cannot find a solution!
xenkan (21 rep)
Aug 26, 2019, 04:12 AM • Last activity: Jul 21, 2021, 07:33 AM
2 votes
2 answers
3011 views
In a file to find and replace/change multiple strings in a line with shell script
I am new to shell scripting What I have is a log file, each line begins like the below entry, ``` [2021.04.27.17:03:43.813]: ERROR - [2021.04.27.17:04:36.790]: SUCCESS - ``` The desired output to a new file is ``` 2021-04-27 17:03:43 813,ERROR, 2021-04-27 17:04:36 790,SUCCESS, ``` What I have coded...
I am new to shell scripting What I have is a log file, each line begins like the below entry,
The desired output to a new file is
2021-04-27 17:03:43 813,ERROR,
2021-04-27 17:04:36 790,SUCCESS,
What I have coded is
while read a; do
    echo ${a//"["/""}
    echo ${a//"]"/""}
done  resultlog.txt
The above code is removing square brackets for alternate lines meaning from the first line "[" opening square is removed and from second line "]" closing bracket is removed I want both to be removed from all the lines. Not only that the dot I want to change to hyphen has shown in the above example.
Mathewshr (23 rep)
Jun 10, 2021, 11:30 AM • Last activity: Jun 11, 2021, 02:09 AM
0 votes
1 answers
504 views
PureOS Command Line Window
I'm new to PureOS and I've been looking for a way to open a command line window without any luck. How do I open a console/command line window in PureOS?
I'm new to PureOS and I've been looking for a way to open a command line window without any luck. How do I open a console/command line window in PureOS?
Akela615 (1 rep)
Apr 13, 2021, 09:58 PM • Last activity: Apr 14, 2021, 10:26 PM
0 votes
0 answers
230 views
Gnuplot connect points when no value
I have below example, 1|5 2|8 3|1 6|8 7|5 8|10 With below code the chart connects between 3 and 6 x data is there a way to plot zero values between 3 and 6 x data, gnuplot << EOT set datafile separator "|" set grid set key off set yrange [0:10] set xrange [0:10] plot "test.jpeg" u 1:2 smooth frequen...
I have below example, 1|5 2|8 3|1 6|8 7|5 8|10 With below code the chart connects between 3 and 6 x data is there a way to plot zero values between 3 and 6 x data, gnuplot << EOT set datafile separator "|" set grid set key off set yrange [0:10] set xrange [0:10] plot "test.jpeg" u 1:2 smooth frequency lw 1 linecolor rgb "blue" EOT
ammar (87 rep)
Mar 30, 2021, 09:27 AM
1 votes
1 answers
46 views
merge x and x + n lines
I am writing a script which returns 15 lines of titles and 15 lines of URLs. ``` title 1 title 2 title 3 *snip* title 14 title 15 http://example.com/query?1 http://example.com/query?2 http://example.com/query?3 *snip* http://example.com/query?14 http://example.com/query?15 ``` I'd like to merge it i...
I am writing a script which returns 15 lines of titles and 15 lines of URLs.
title 1
title 2
title 3
*snip*
title 14
title 15
http://example.com/query?1 
http://example.com/query?2 
http://example.com/query?3 
*snip*
http://example.com/query?14 
http://example.com/query?15 
I'd like to merge it in such a way that produces the following output:
title 1 http://example.com/query?1 
title 2 http://example.com/query?2 
title 3 http://example.com/query?3 
*snip*
title 15 http://example.com/query?15 
Upon light inspection and the guidance of this answer , I found the command
. However,
does not allow for the performance of more complex behaviors like the ones described above. Is there another tool or combination of tools I can use in order to accomplish the aforementioned behavior? Do note that I'm looking to use all standard coreutils behavior, if at all possible.
here&#39;s a name (13 rep)
Dec 20, 2020, 03:37 AM • Last activity: Dec 20, 2020, 06:34 AM
0 votes
2 answers
1276 views
Is a text file bigger on Windows than on Linux because of carriage return?
Let's say I have the same ASCII text file on Linux and Windows: ```none one two three ``` The two files will have `\n` and `\r\n` respectively as EOL character on the two OSes. Does this mean that the file on Linux is smaller? This test, performed on linux, seems say yes: ```none $ echo -en 'one\ntw...
Let's say I have the same ASCII text file on Linux and Windows:
one
two
three
The two files will have \n and \r\n respectively as EOL character on the two OSes. Does this mean that the file on Linux is smaller? This test, performed on linux, seems say yes:
$ echo -en 'one\ntwo\nthree\n' | wc --bytes 
14
$ echo -en 'one\r\ntwo\r\nthree\r\n' | wc --bytes 
17
Enlico (2258 rep)
Sep 7, 2020, 09:08 AM • Last activity: Sep 7, 2020, 02:15 PM
0 votes
2 answers
211 views
using $ arguments on the command line and providing values for them at the end
rather than write scripts for every single process I need to do, I like to just save commands in a text file and copy and paste them on the command line. Of course, some of the specifics of the commands change each time I paste and run it. Then I need to arrow thru the command and change the importa...
rather than write scripts for every single process I need to do, I like to just save commands in a text file and copy and paste them on the command line. Of course, some of the specifics of the commands change each time I paste and run it. Then I need to arrow thru the command and change the important parts. So, I would like to do something like this. For a simple example:
chmod $1 /home/$2/.bashrc 740 bobsa
Can that be done some way? Is xargs something that can be used for this? I know I can set ENV variables before running the command to attain this but I was hoping from something else I can tac on the end thanks!
josa (3 rep)
Jul 4, 2020, 04:01 PM • Last activity: Jul 6, 2020, 07:55 PM
2 votes
4 answers
4689 views
Read file line by line with awk to replace characters in certain line numbers
I have got this script here. It is supposed to run the loop by reading the file LineNumbers.file line by line (each contains a line number) and then accordingly replacing 0/0 with ./. in the BEFORE_File.txt. It works, but it takes only the very last line of the file LineNumbers.file, instead of the...
I have got this script here. It is supposed to run the loop by reading the file LineNumbers.file line by line (each contains a line number) and then accordingly replacing 0/0 with ./. in the BEFORE_File.txt. It works, but it takes only the very last line of the file LineNumbers.file, instead of the >100 entries. I am not sure what I am doing wrong here. Can you please help me to get awk to read the LineNumbers.file line by line? I have got it to work with sed -i "${line}s/0\/0/\.\/\./" "${myFileTmp}", but it was really slow on the >3GB large files that I have got. So I thought that awk would be a faster option. Many thanks!
cat ./LineNumbers_TEMP/LineNumbers.file | while read line
do
myFileTmp=BEFORE_File.txt
awk -v var=${line} 'FNR==var { sub(/0\/0/, "\.\/\."); print }' "${myFileTmp}" > AFTER_File.txt
done
For example this is how the files look like:
cat ./LineNumbers_TEMP/LineNumbers.file
1
2
5
File.txt before script:
cat BEFORE_File.txt
0/0
0/0
0/1
0/1
0/0
0/0
0/0
This is how the file should look after running the script:
cat AFTER_File.txt
./.
./.
0/1
0/1
./.
0/0
0/0
At the moment I get only this:
./.
P. HamB (35 rep)
Apr 24, 2020, 04:45 AM • Last activity: Apr 25, 2020, 02:23 AM
13 votes
2 answers
3827 views
How to make a continuous vertical line in vim?
I want to make a table in vim. Making a horizontal line is easy ______________________________ For the vertical I use this yes "|" | head -10 But the result is bad | | | | | | | | | I want something contiguous like the horizontal line. How can I do this?
I want to make a table in vim. Making a horizontal line is easy ______________________________ For the vertical I use this yes "|" | head -10 But the result is bad | | | | | | | | | I want something contiguous like the horizontal line. How can I do this?
elbarna (13690 rep)
Oct 27, 2019, 07:17 PM • Last activity: Apr 13, 2020, 07:48 PM
1 votes
2 answers
856 views
How to delete particular lines in vim editor
Suppose I have 50 lines in my vim editor and I have to delete particular lines (5,9,16,28) in a single command.
Suppose I have 50 lines in my vim editor and I have to delete particular lines (5,9,16,28) in a single command.
Tanveer Ahmed Shaikh (11 rep)
Mar 9, 2020, 06:20 AM • Last activity: Mar 9, 2020, 11:03 AM
0 votes
1 answers
428 views
Dovecot add existent folder from command line
I've insert a .INBOX.test folder in my mailbox folder /home/user/Maildir but I dont see it from Thunderbird. What i have to do for see it?! Thanks
I've insert a .INBOX.test folder in my mailbox folder /home/user/Maildir but I dont see it from Thunderbird. What i have to do for see it?! Thanks
ankx7 (3 rep)
Nov 3, 2019, 02:26 PM • Last activity: Nov 3, 2019, 07:35 PM
Showing page 1 of 20 total questions