Sample Header Ad - 728x90

Unix & Linux Stack Exchange

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

Latest Questions

23 votes
6 answers
21513 views
Command-line tool to search docx files
Is there a command-line tool to text-search a docx file? I tried `grep`, but it doesn't work with docx even though it works fine with txt and xml files. I could convert the docx to txt first, but I'd prefer a tool that operates directly on docx files. I need the tool to work under Cygwin. OP edit: L...
Is there a command-line tool to text-search a docx file? I tried grep, but it doesn't work with docx even though it works fine with txt and xml files. I could convert the docx to txt first, but I'd prefer a tool that operates directly on docx files. I need the tool to work under Cygwin. OP edit: Later I found out that the easiest way to achieve the grep is actually to convert those docx to txt then grep over them.
RoundPi (331 rep)
Jan 6, 2012, 04:25 PM • Last activity: Jul 29, 2025, 01:51 PM
33 votes
1 answers
15788 views
Use case sensitive search for lower-case string using smartcase
I normally enable `smartcase` in my `.vimrc`. **Without `smartcase`:** - If I want case-insensitive search, I write in upper-case or lower-case (doesn't matter) and append a `\c` at the end of the query. - Case-sensitive search happens by default. **With `smartcase`:** - If I want case insensitive s...
I normally enable smartcase in my .vimrc. **Without smartcase:** - If I want case-insensitive search, I write in upper-case or lower-case (doesn't matter) and append a \c at the end of the query. - Case-sensitive search happens by default. **With smartcase:** - If I want case insensitive search, I write all in lower-case. - If I want case sensitive search, I write the upper-case letters in upper-case. But, with smartcase enabled, how can I say that I want a case sensitive search of a lower-case pattern? I was expecting that appending a \c would revert the type of search (so that if I had written all in lower-case, i.e. case-insensitive search, it would turn to a case-sensitive search; and if I had written some upper-case letters, i.e. case-sensitive search, it would turn to a case-insensitive search), but that does not seem to be the case (no pun intended).
user2891462 (433 rep)
Apr 5, 2017, 12:03 PM • Last activity: Jul 23, 2025, 08:07 PM
2 votes
1 answers
57 views
Want to search for a every paragraph that has a certain string and return some string from that paragraph
``` /* ----------------- WANT TO RETURN1 ----------------- */ machine: some value server: WANT TO RETURN1 owner: some value notification: some value informatica: some value sap: some value group: This line contains a3 but can have space and characters /* ----------------- WANT TO RETURN2 -----------...
/* ----------------- WANT TO RETURN1 ----------------- */  

machine: some value
server:	WANT TO RETURN1		
owner: some value	 	
notification: some value		
informatica: some value		
sap: some value	
group: This line contains a3 but can have space and characters


/* ----------------- WANT TO RETURN2 ----------------- */

server:	WANT TO RETURN2
machine: some value		
owner: some value	 	
notification: some value		
informatica: some value		
sap: some value	
group:  This line contains a1 but can have space and characters
unix: some value	 	
windows: some value		
aws: some value		
sap1: some value

/* ----------------- WANT TO RETURN3 ----------------- */  


owner: some value
machine: some value		
server:	WANT TO RETURN3	 	
notification: some value		
informatica: some value		
sap: some value	
group: This line contains a1 but can have space and characters


/* ----------------- WANT TO RETURN4 ----------------- */

server:	WANT TO RETURN4
machine: some value		
owner: some value	 	
notification: some value		
informatica: some value		
sap: some value	
group:  This line contains a2 but can have space and characters
unix: some value	 	
windows: some value		
aws: some value		
sap1: some value
Need to do with linux shell script if possible. I have some data in unix file as above. I have a file with multiple values for _STRING_TO_SEARCH_ like job.txt:
a1  
a2  
a3
Example - I want to run a for loop for each value of _STRING_TO_SEARCH_ and capture both _STRING_TO_SEARCH_ and server: value for that paragraph Desired output -
group: a1  
server: WANT TO RETURN2  
server: WANT TO RETURN3

group: a2  
server: WANT TO RETURN4  

group: a3  
server: WANT TO RETURN1
In each paragraph number of rows will vary and fields server: WANT TO RETURN and group:. This line contains a1 but can have space or characters can can occur on any row.
user9797 (23 rep)
Jul 17, 2025, 11:12 PM • Last activity: Jul 18, 2025, 11:08 AM
1 votes
2 answers
93 views
How to extract a sub-heading as string which is above a search for word
I'm new to Bash and I've been self-taught. I think I'm learning well, but I do have staggering gaps in my base knowledge. So sorry if this is woefully simple bbuuuttt... Essentially, I need to sift through a large amount of data and pull out specific phrases. I've been making slow and steady progres...
I'm new to Bash and I've been self-taught. I think I'm learning well, but I do have staggering gaps in my base knowledge. So sorry if this is woefully simple bbuuuttt... Essentially, I need to sift through a large amount of data and pull out specific phrases. I've been making slow and steady progress, but I'm now stuck on getting a heading for a line of data. Here's what the file looks like:
A lot (AND I MEAN A LOT) of data above  
STATE  1:
133a -> 135a  :     0.010884 (c= -0.10432445)
134a -> 135a  :     0.933650 (c= -0.96625573)

STATE  2:
129a -> 135a  :     0.016601 (c= -0.12884659)
130a -> 135a  :     0.896059 (c= -0.94660402)
130a -> 136a  :     0.011423 (c=  0.10687638)
130a -> 137a  :     0.023884 (c= -0.15454429)
130a -> 138a  :     0.020361 (c= -0.14269354)

STATE  3:
133a -> 135a  :     0.899436 (c= -0.94838591)
134a -> 136a  :     0.012334 (c= -0.11106052)       

STATE  4:
129a -> 135a  :     0.688049 (c= -0.82948703)
129a -> 136a  :     0.212819 (c= -0.46132295)
129a -> 137a  :     0.036987 (c=  0.19231930)
130a -> 135a  :     0.011990 (c=  0.10949722)
134a -> 135a  :     0.922010 (c= -0.98192034)
There are many more states (up to 30) of varying length below, which may also include what I'm looking for. And then more data below that I have got the numbers I am looking for saved in variables. (134 and 135 for this example) And I can use :
"${a}a -> ${b}a" File.Name;
to show me the lines that have 134 -> 135 on, but I need the STATE that they are in. I've tried using grep to look above the found lines to the nearest line with STATE in, but I couldn't figure out how to set the length of -B as a condition rather than a number (don't know if it can be done). I have also tried with awk and sed to find the line with STATE and look below to see if 134 -> 135 is benethe it before the next STATE, but I couldn't find a way to stop it and not print at the next STATE instead of just continuing until it found the next 134 -> 135. The Ideal output (for the above example) would be:
STATE  1
STATE  4
but
STATE  1:
133a -> 135a  :     0.010884 (c= -0.10432445)
134a -> 135a  :     0.933650 (c= -0.96625573)
STATE  4:
129a -> 135a  :     0.688049 (c= -0.82948703)
129a -> 136a  :     0.212819 (c= -0.46132295)
129a -> 137a  :     0.036987 (c=  0.19231930)
130a -> 135a  :     0.011990 (c=  0.10949722)
134a -> 135a  :     0.922010 (c= -0.98192034)
is also absolutely fine. I just need it to spit out the correct STATES and no others. It doesn't really matter what other data comes with it. Also, this is going to be applied to about 40 other files with similar layouts, so I need it not to be specific to this one (aka not grep STATE 1 and grep state 4) I'm hoping someone can help me or tell me if this is impossible to do.
TC575 (13 rep)
Jul 11, 2025, 08:37 PM • Last activity: Jul 16, 2025, 09:53 AM
-4 votes
1 answers
85 views
How combination of find with sh solves problem with filenames
I was directed to this StackExchange post: [Why is looping over find's output bad practice?][1] [1]: https://unix.stackexchange.com/questions/321697/why-is-looping-over-finds-output-bad-practice The core issue is that Unix filenames can contain any character except the null byte (\0). This means the...
I was directed to this StackExchange post: Why is looping over find's output bad practice? The core issue is that Unix filenames can contain any character except the null byte (\0). This means there is no printable character you can reliably use as a delimiter when processing filenames - newlines, spaces, and tabs can all appear in valid filenames. A common solution is to use a null byte as the delimiter, which is supported by GNU find with the -print0 option. This allows you to safely process filenames using tools like xargs -0 or while read -d ''. However, not all versions of find (especially non-GNU variants) support -print0. This raises the question: What should you do for portability when -print0 isn't available? Frankly, it seems that find implementations lacking -print0 are fundamentally flawed for robust scripting and should not be relied upon in scripts that need to handle arbitrary filenames safely. There was the suggestion to use find in combination with sh find dirname ... -exec sh -c 'for f do somecommandwith "$f"; done' find-sh {} + How does this fix the problem? One is still using a defective find. The link did not provide a clear explanation of why the combination of find with sh should work. How does it solve the problem?
Filangieri (179 rep)
Jun 9, 2025, 05:15 PM • Last activity: Jun 9, 2025, 11:33 PM
8 votes
4 answers
972 views
Elegant File String Search in Bash
I want to search for a string in files located in a certain directory using a bash script. The line number is printed near each match and the filename is printed once at the start. find "$search_dir" -type f -print0 | while read -d '' -r file; do matches=$(grep -n -- "$search_str" "$file" | sed 's/:...
I want to search for a string in files located in a certain directory using a bash script. The line number is printed near each match and the filename is printed once at the start. find "$search_dir" -type f -print0 | while read -d '' -r file; do matches=$(grep -n -- "$search_str" "$file" | sed 's/:/: /1' ) if [[ -n "$matches" ]]; then echo "$file" echo "$matches" echo fi done It has been suggested to me there's elegant alternatives to find … | while … read… For instance find -type f -exec some_command {} ';' And shopt -s extglob globstar for file in **/!(*/) ; do [[ -f "${file}" ]] && continue what_you_wanted_to_do done How can these elegant ways be adapted to my case?
Filangieri (179 rep)
Jun 8, 2025, 11:07 PM • Last activity: Jun 9, 2025, 07:12 PM
0 votes
2 answers
3538 views
Use reverse i search to cycle through only commands matching pattern
The contents of the command history file using `history | less` is: ``` 555 ls 556 ls -a 557 echo "hello" 558 echo "hello again" 559 cd 560 pwd 561 echo "hello hello" 562 ls 563 echo "hello hello hello" 564 cd 565 pwd ``` I want to use reverse i search ``(reverse-i-search)`echo':`` to cycle through...
The contents of the command history file using history | less is:
555 ls
556 ls -a
557 echo "hello"
558 echo "hello again"
559 cd
560 pwd
561 echo "hello hello"
562 ls
563 echo "hello hello hello"
564 cd
565 pwd
I want to use reverse i search `(reverse-i-search)echo':` to cycle through only commands in the command history file matching the pattern (i.e. in this case \echo') entered on the command line prompt, preferably until a specified line number in the command history file so that I only select previously used echo commands that I want to run again not all echo commands in the command history file Using reverse i search finds the most recent usage of the command echo (hence the name reverse i search i.e. reverse searching for a command). However when scrolling forward in reverse order (i.e. reverse searching) bash scrolls through each line of the command history file starting from the line returned by the reverse i search, thus including commands I don't want to search for. 1. The reverse i search prompt `(reverse-i-search)echo':`` disappears once you start scrolling through the history list. How do you prevent the reverse i search prompt from disappearing during scrolling so that when you scroll forward in a reverse i search only the commands matching the pattern are shown? 2. If 1 is possible then how do you temporarily limit the reverse i search to a specific line number in the command history file in order to only select commands from a section of the command history file? Note: a solution to this question would also be useful for those times when you have several instances of the same command line arguments and reverse i search does not find the one you're looking for
bit (1176 rep)
Jun 25, 2019, 08:14 PM • Last activity: May 26, 2025, 04:06 AM
1 votes
1 answers
58 views
"history-search-backward" but ignore sudo prefix in zsh?
According to this page: https://zsh.sourceforge.io/Doc/Release/Zsh-Line-Editor.html `history-search-backward`: "Search backward in the history for a line beginning with the first word in the buffer." But when command is prefixed with `sudo` that breaks everything. Can I make zsh ignore `sudo` for th...
According to this page: https://zsh.sourceforge.io/Doc/Release/Zsh-Line-Editor.html history-search-backward: "Search backward in the history for a line beginning with the first word in the buffer." But when command is prefixed with sudo that breaks everything. Can I make zsh ignore sudo for this command? Also want to apply same thing for history-search-forward
sloppy (121 rep)
Feb 23, 2025, 04:16 PM • Last activity: May 25, 2025, 09:47 AM
1 votes
5 answers
3536 views
Grep, but for binaries
I've got a fragment of an image file produced by data-recovery software. I suspect the complete original is somewhere on my home fileserver. If this were a fragment of a text file, I could just grab a unique-looking fragment, run `grep -r -l -F `, and come back in a few hours for the answer. However...
I've got a fragment of an image file produced by data-recovery software. I suspect the complete original is somewhere on my home fileserver. If this were a fragment of a text file, I could just grab a unique-looking fragment, run grep -r -l -F , and come back in a few hours for the answer. However, since this is a binary file, it's got all sorts of things that grep doesn't like (such as null bytes), and even if I can get past that, I don't know how to give grep input that isn't valid UTF-8. How can I search for the original, preferably without writing my own search program? (This is not a duplicate of this question : despite the likely-sounding title, that one is about finding strings in binary files, where I'm looking for binary data in binary files.)
Mark (4665 rep)
Feb 28, 2023, 10:41 PM • Last activity: May 22, 2025, 04:01 PM
19 votes
4 answers
7484 views
Binary search in a sorted text file
I have a big sorted file with billions of lines of variable lengths. Given a new line I would like to know which byte number it would get if it had been included in the sorted file. **Example** a\n c\n d\n f\n g\n Given the input 'foo' I would get the output 9. This is easy to do by simply going thr...
I have a big sorted file with billions of lines of variable lengths. Given a new line I would like to know which byte number it would get if it had been included in the sorted file. **Example** a\n c\n d\n f\n g\n Given the input 'foo' I would get the output 9. This is easy to do by simply going through the whole file, but being billions of lines of variable lengths it would be faster to do a binary search. Does such a text processing tool already exist? Edit: It does now: https://git.data.coop/tange/tangetools/src/branch/master/2search/2search
Ole Tange (37348 rep)
Dec 5, 2015, 09:45 AM • Last activity: May 6, 2025, 11:25 AM
1 votes
1 answers
86 views
How to find which Debian package might include a given source file?
I would like to debug my application in `gdb`, however with full sources of some system libraries I might need. For instance, at a certain point in my debug process in `gdb`, I arrive at a situation like: ```none ... (gdb) si 0x00047e28 in std::thread::detach() () (gdb) c Continuing. ... Thread 1 "m...
I would like to debug my application in gdb, however with full sources of some system libraries I might need. For instance, at a certain point in my debug process in gdb, I arrive at a situation like:
...
(gdb) si
0x00047e28 in std::thread::detach() ()
(gdb) c
Continuing.
...
Thread 1 "myProject" received signal SIGABRT, Aborted.
raise (sig=) at ../sysdeps/unix/sysv/linux/raise.c:51
51      ../sysdeps/unix/sysv/linux/raise.c: No such file or directory.
I am not sure why for std::thread::detach() I do not get a source file suggestion (I have looked it up manually online and found it is in [libstdc++-v3/src/c++11/thread.cc](https://github.com/gcc-mirror/gcc/blob/releases/gcc-6.3.0/libstdc%2B%2B-v3/src/c%2B%2B11/thread.cc)) , but for raise() I do get source file and line ../sysdeps/unix/sysv/linux/raise.c:51. Anyways, for either of these, thread.cc or raise.c, how can I find which *source* package on a given Debian includes them? So I could eventually get them with apt source [package] and provide the path to those files to gdb as described in https://stackoverflow.com/questions/48278881/gdb-complaining-about-missing-raise-c/48287761#48287761 so I could step through source lines (and especially see what underlying function receive as arguments from my code) ... I have tried apt-file, but it seems that it does not work with source packages:
$ apt-file search raise.c
gnulib: /usr/share/gnulib/lib/raise.c
gnulib: /usr/share/gnulib/tests/test-raise.c
` Cool, but none of those look like ../sysdeps/unix/sysv/linux/raise.c?!
$ apt-file search thread.cc
c++-annotations: /usr/share/doc/c++-annotations/examples/yo/threading/examples/functorthread.cc
libglibmm-2.4-doc: /usr/share/doc/libglibmm-2.4-doc/examples/thread/thread.cc
omniorb-doc: /usr/share/doc/omniorb-doc/examples/poa/threading/mainthread.cc
rust-src: /usr/src/rustc-1.24.1/src/libcompiler_builtins/compiler-rt/lib/asan/asan_thread.cc
rust-src: /usr/src/rustc-1.24.1/src/libcompiler_builtins/compiler-rt/lib/lsan/lsan_thread.cc
rust-src: /usr/src/rustc-1.24.1/src/libcompiler_builtins/compiler-rt/lib/msan/msan_thread.cc
rust-src: /usr/src/rustc-1.24.1/src/libcompiler_builtins/compiler-rt/lib/tsan/rtl/tsan_rtl_thread.cc
rust-src: /usr/src/rustc-1.24.1/src/libcompiler_builtins/compiler-rt/lib/tsan/tests/rtl/tsan_thread.cc
rust-src: /usr/src/rustc-1.24.1/src/libcompiler_builtins/compiler-rt/test/tsan/race_with_finished_thread.cc
rust-src: /usr/src/rustc-1.24.1/src/libcompiler_builtins/compiler-rt/test/tsan/signal_thread.cc
Ok, apt-file again found something, but all of them seem to be false positives, nothing matches the expected libstdc++-v3/src/c++11/thread.cc. So, is there any way to look up source files like this on Debian? Specifically, I'm on Raspbian Stretch on Raspberry Pi 3B+.
sdbbs (578 rep)
Mar 19, 2025, 08:25 PM • Last activity: Mar 20, 2025, 12:34 PM
0 votes
2 answers
269 views
Searching whole system for files with specific inode
I technically know how to do all these things, but combining it is problematic. Inode is saved in first line of text file (I can eventually read it directly from file), I need results saved to the same file. How can I do this?
I technically know how to do all these things, but combining it is problematic. Inode is saved in first line of text file (I can eventually read it directly from file), I need results saved to the same file. How can I do this?
Corporal Girrafe (13 rep)
May 24, 2019, 12:06 PM • Last activity: Mar 19, 2025, 06:03 PM
11 votes
12 answers
25254 views
How can I use grep to search only on the first line of files for a specific string?
How can I use grep to find a string in files, but only search in the first line of these files?
How can I use grep to find a string in files, but only search in the first line of these files?
Michiel Pater (249 rep)
Mar 23, 2015, 01:23 PM • Last activity: Jan 14, 2025, 03:13 PM
2 votes
1 answers
90 views
Adding custom search engines to Firefox system-wide
On multi-user Debian systems with `firefox-esr` 128 I'd like to add search engines to the firefox search bar eg. [stackexchange](https://stackexchange.com/opensearch.xml). I know how to do it manually but I did not find a solution on how to pre-configure it for all users at once. Already [asked this...
On multi-user Debian systems with firefox-esr 128 I'd like to add search engines to the firefox search bar eg. [stackexchange](https://stackexchange.com/opensearch.xml) . I know how to do it manually but I did not find a solution on how to pre-configure it for all users at once. Already asked this on serverfault without success :(
Christo (129 rep)
Jan 9, 2025, 01:48 PM • Last activity: Jan 9, 2025, 02:00 PM
27 votes
10 answers
12848 views
How to replace current word under cursor in Emacs
How do I replace current word under cursor in Emacs? I know that I can use query-replace or replace-string but every time I do so I have to type entire string to be replaced, this is just annoying. Vi has equivalent command cword and I can use a shortcut to pull the word under cursor for replacement...
How do I replace current word under cursor in Emacs? I know that I can use query-replace or replace-string but every time I do so I have to type entire string to be replaced, this is just annoying. Vi has equivalent command cword and I can use a shortcut to pull the word under cursor for replacement: nmap z :%s#\=expand("")\># Anybody know how to do it with Emacs?
Think Pl (483 rep)
Oct 14, 2012, 09:19 PM • Last activity: Dec 30, 2024, 10:12 PM
56 votes
7 answers
44885 views
Is there some sort of PDF-to-text converter?
I need PDF files in text so I can search over them in bulk from commandline. Is there some converter for Ubuntu, OBSD or similar distro? Perhaps related post, OCR with Ubuntu [here][1]. [1]: https://askubuntu.com/questions/8792/optical-character-recognition-software-on-ubuntu
I need PDF files in text so I can search over them in bulk from commandline. Is there some converter for Ubuntu, OBSD or similar distro? Perhaps related post, OCR with Ubuntu here .
otto (661 rep)
Dec 11, 2010, 02:46 PM • Last activity: Dec 30, 2024, 06:23 PM
4 votes
3 answers
5866 views
grep search for any number in a range
So I have a file with a number of records that look like the following: Peugeot:206:2000:Red:1 And I'm trying to grep search for any that contain a year 1995-1999. I have tried grep '[1995-1999]' file.txt to no avail. Any ideas? Thanks.
So I have a file with a number of records that look like the following: Peugeot:206:2000:Red:1 And I'm trying to grep search for any that contain a year 1995-1999. I have tried grep '[1995-1999]' file.txt to no avail. Any ideas? Thanks.
Harry Adams (143 rep)
Nov 19, 2015, 04:29 PM • Last activity: Dec 26, 2024, 06:02 PM
1 votes
3 answers
133 views
bash - isolating uppercase words
So, I have a directory containing around 50 directories having for name 3 letter uppercase words: AXC BCC EFC amongst other directories. I have already done a find to seek these 3 letter directories and store them in a list list=`find /data/opr/ucansit/ -type d -name "???"` The current output i get...
So, I have a directory containing around 50 directories having for name 3 letter uppercase words: AXC BCC EFC amongst other directories. I have already done a find to seek these 3 letter directories and store them in a list list=find /data/opr/ucansit/ -type d -name "???" The current output i get from an echo ${list[@]} is /data/opr/ucansit/CUG /data/opr/ucansit/TGV /data/opr/ucansit/PAS what i need now is seek the 3 letter directory names and store them in a stack as: CUG TGV PAS Note: I just need the names of the directories (the 3 letters) BRegards,
scandalous (197 rep)
Mar 24, 2016, 08:55 AM • Last activity: Dec 20, 2024, 11:55 AM
4 votes
3 answers
128 views
Fail to use "locate"
I have a file named `Fubini's_theorem.html` under some directory. But both locate "Fubini's_theorem" or locate Fubini do not return anything. The file has been there for months, and I am sure it is already in the database used by `locate`. So I wonder why the search is not successful? Thanks! My OS...
I have a file named Fubini's_theorem.html under some directory. But both locate "Fubini's_theorem" or locate Fubini do not return anything. The file has been there for months, and I am sure it is already in the database used by locate. So I wonder why the search is not successful? Thanks! My OS is Ubuntu 10.10.
Tim (106420 rep)
Aug 22, 2011, 10:07 PM • Last activity: Dec 20, 2024, 09:59 AM
3 votes
2 answers
1206 views
How to parse the file from end in awk
#!/bin/sh if [ $# -ne 8 ]; then echo "Usage: sh `basename $0` YYYYMMDD hour min masterid account destination filename forward|backward"; exit 1; fi day=${1} hour=${2} min=${3} masterid=${4} account=${5} destination=${6} file=${7} if [ "${8}" == "backward" ]; then backward="true"; fi if [ "${backward...
#!/bin/sh if [ $# -ne 8 ]; then echo "Usage: sh basename $0 YYYYMMDD hour min masterid account destination filename forward|backward"; exit 1; fi day=${1} hour=${2} min=${3} masterid=${4} account=${5} destination=${6} file=${7} if [ "${8}" == "backward" ]; then backward="true"; fi if [ "${backward}" != "true" ]; then awk -v day=${day} -v hour=${hour} -v min=${min} -v masterid=${masterid} -v account=${account} -v destination=${destination} -f /var/tmp/selfCross.awk /koba/c/dumper/${file} else tac "/koba/c/dumper/${file}" | awk -v day=${day} -v hour=${hour} -v min=${min} -v masterid=${masterid} -v account=${account} -v destination=${destination} -f /var/tmp/selfCross.awk fi if backward != true, it is working fine. But, tac is somehow not working what should I do? It is returning immediately. What am I doing wrong in the above script?
user14039
Jan 11, 2012, 04:27 PM • Last activity: Dec 20, 2024, 09:57 AM
Showing page 1 of 20 total questions