Sponsored Content
Top Forums UNIX for Dummies Questions & Answers String search - Command to find second occurance Post 302149587 by jim mcnamara on Thursday 6th of December 2007 03:59:57 PM
Old 12-06-2007
Other than looping or using awk, perl, or python, I have no other idea except an extreme hack like this:

Code:
#!/bin/ksh

# total line in file
wc -l inputfile | read filelen dummy

# find the line to start reading on
grep -n 'some string' inputfile | head -n 2 | tail -n 1 | read startline dummy

# subtract start line from total 
length=$(( $filelen - $startline ))

# tail the lines you need.
tail -n $length inputfile

If you are running this against a large file it will waste considerable resources.
It wastes resources anyway, because it reads thru the file several times.
 

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

Unix find command to print directory and search string

Hi i need to print pathname in which the string present using 'find' command sample output like this Pathname String to be searched ---------- -------------------- /usr/test/myfile get /opt/test/somefile get Thanks in... (4 Replies)
Discussion started by: princein
4 Replies

2. UNIX for Dummies Questions & Answers

search for a word and it's occurance at the output

hey to everybody this is my first post at this forum I need a very immediate answer for this question. If you can, I will be delightfull I have a file called example.txt and I want to seek for the for hello and learn the number of the occurance of hello (2 Replies)
Discussion started by: legendofanatoli
2 Replies

3. Shell Programming and Scripting

search and replace the last occurance of a match in a file

HI please let me know if there is any command to search and replace only the last occurence of a string in aline. Eg: " This cat is a cat with a tail longer of all cat." I need to replace only the last "cat" in the above line. thanks (3 Replies)
Discussion started by: harikris614
3 Replies

4. Shell Programming and Scripting

Awk - find string, search lines below string for other strings

What's the easiest way to search a file for a specific string and then look for other instances after that? I want to search for all Virtual Hosts and print out the Server Name and Document Root (if it has that info), while discarding the rest of the info. Basically my file looks like this: ...... (6 Replies)
Discussion started by: Mbohmer
6 Replies

5. Shell Programming and Scripting

Search and replace only the first occurance

Hi, I need to search a string and replace with nothing, but only the First occurring string using sed/perl/awk (3 Replies)
Discussion started by: suraj.sheikh
3 Replies

6. Linux

Linux command to find and replace occurance of more than two equal sign with "==" from XML file.

Please help me, wasted hrs:wall:, to find this soulution:- I need a command that will work on file (xml) and replace multiple occurrence (more than 2 times) Examples 1. '===' 2. '====' 3. '=======' should be replaced by just '==' Note :- single character should be replaced. (=... (13 Replies)
Discussion started by: RedRocks!!
13 Replies

7. Shell Programming and Scripting

Search for a pattern in a String file and count the occurance of each pattern

I am trying to search a file for a patterns ERR- in a file and return a count for each of the error reported Input file is a free flowing file without any format example of output ERR-00001=5 .... ERR-01010=10 ..... ERR-99999=10 (4 Replies)
Discussion started by: swayam123
4 Replies

8. Shell Programming and Scripting

Search several string and convert into a single line for each search string using awk command AIX?.

I need to search the file using strings "Request Type" , " Request Method" , "Response Type" and by using result set find the xml tags and convert into a single line?. below are the scenarios. Cat test Nov 10, 2012 5:17:53 AM INFO: Request Type Line 1.... (5 Replies)
Discussion started by: laknar
5 Replies

9. Shell Programming and Scripting

[Need help] perl script to find the occurance of string from a text file

I have two files 1. input.txt 2. keyword.txt input.txt has contents like .src_ref 0 "call.s" 24 first 0x000000 0x5a80 0x0060 BRA.l 0x60 .src_ref 0 "call.s" 30 first 0x000002 0x1bc5 RETI .src_ref 0 "call.s" 31 first 0x000003 0x6840 ... (2 Replies)
Discussion started by: acdc
2 Replies

10. Shell Programming and Scripting

How can I use find command to search string/pattern in a file recursively?

Hi, How can I use find command to search string/pattern in a file recursively? What I tried: find . -type f -exec cat {} | grep "make" \; Output: grep: find: ;: No such file or directory missing argument to `-exec' And this: find . -type f -exec cat {} \; -exec grep "make" {} \;... (12 Replies)
Discussion started by: cola
12 Replies
DHEX(1) 						    BSD General Commands Manual 						   DHEX(1)

NAME
dhex -- hex editor with a diff mode SYNOPSIS
dhex [-h] [-v] [-g] [-k] [-ab -ad -ah -ao base address] [-f config-file] [-m marker-file] [-ob -od -oh -oo offset] [-r searchlog] [-sa -sab string (ascii)] [-sh -shb string (hex)] [-w searchlog] [file] dhex [-h] [-v] [-g] [-k] [-a1b -a1d -a1h -a1o base address 1] [-a2b -a2d -a2h -a2o base address 2] [-cb] [-cd upper-limit] [-cl] [-f config-file] [-m marker-file] [-o1b -o1d -o1h -o1o offset1] [-o2b -o2d -o2h -o2o offset2] [-r1 searchlog1] [-r2 searchlog2] [-s1a -s1ab -s1h -s1hb string (ascii/hex)] [-s2a -s2ab -s2h -s2hb string (ascii/hex)] [-w1 searchlog1] [-w2 searchlog2] [file1 file2] DESCRIPTION
dhex is a hex editor. It can be used to alter individual bytes in large files. Since it is a text-mode programm based on ncurses, it can run in numerous scenarios. Its special feature is the diff mode: With it, the user has a visual tool for file comparison. This mode is invoked when dhex is called with two instead of one file as parameters. OPTIONS
All the options are case-insensitive and can be given as either upper- or lowercase characters. -ab -ad -ah -ao base address After loading a file, every address gets a base address other than 0. With this, it is easier to work on partial memory dumps. The base address can be given as a binary one with [-ab] , as a decimal one with [-ad] , as a hexadecimal one with [-ah] or an octal with [-ao]. -a1b -a1d -a1h -a1o base address 1 -a2b -a2d -a2h -a2o base address 2 For the diff mode, it is possible to set two different base addresses. Again, a binary address can be given as [-a1b -a2b] , as decimal one with [-a1d -a2d] , as hexadecimal one with [-a1h -a2h] or an octal one with [-a1o -a2o]. This base address is calculated into the marker files as well as the searchlogs. -cb -cl Diff mode only: The input files can be correlated from the command line with the best -cb or longest -cl match. This is very slow. -cd upper-limit Diff mode only: The input files can be correlated from the command line with the minimum difference. To improve the correlation speed, an upper limit can be provided. -f configfile Usually, .dhexrc is being read from the invoker's home directory. With this parameter, any other config file can be loaded. See dhexrc(5) for a description of its file format. -g Shows the license -h Shows the help screen -k Starts the keyboard setup manually before any file is being loaded. This is very helpful when calling dhex from an exotic termi- nal. -m markerfile It is possible in dhex to set bookmarks and store them in a markerfile for later use. With this parameter, the markerfile is being read at start time, making it unnecesarry to read them later through the gui. Their file format is described in dhex_markers(5). -ob -od -oh -oo offset After loading a file, the cursor is set to 0, and the first page of bytes is being shown on the screen. With one of those parame- ters it is possible to start at any other location in the file. The cursorposition could be given as a binary number with [-ob] , as a decimal one with [-od] , as a hexadecimal one with [-oh] or an octal with [-oo]. -o1b -o1d -o1h -o1o offset1 -o2b -o2d -o2h -o2o offset2 For the diff mode, it is possible to set two different cursorpositions at start time. Again, the cursorpositions can be given as a binary number with [-o1b -o2b] , as decimal one with [-o1d -o2d] , as hexadecimal one with [-o1h -o2h] or an octal one with [-o1o -o2o]. This way, the first few bytes in a file can be skipped, and just the rest can be compared. -r searchlog When searching from the command line, the offsets are being read from this searchlog. Its format is being decribed in dhex_searchlog(5). -r1 searchlog1 -r2 searchlog2 When searching in two files simultanously, the offsets can be read from two different searchlogs. -sa -sab -sh -shb string Instead of setting the cursor offset to an absolute value, it is possible to search for a specific string from the command line. If there is an additional [-ob -od -oh -oo offset] present, the search will start there. It is possible to read and write search logs with [-r searchlog] and [-w searchlog] respectively. With [-sa string] is being interpreted as ASCII. [-sh string] inter- prets it as hex. For backwards search, [-sab string] or [-shb string] can be applied. -s1a -s1ab -s1h -s1hb string1 -s2a -s2ab -s2h -s2hb string2 In the diff mode, it is possible to search for two strings in two files simultanously. -v Prints out the version of dhex. -w searchlog When searching from the command line, write the results into this searchlog and quit. It is being written in the format described in dhex_searchlog(5.) -w1 searchlog1 -w2 searchlog2 When searching in two files simultanously, write the results from both searches into those log files. USER INTERFACE
General Menus have hotkeys, they are being presented in a different color. To jump from one menu item to the next, the cursor keys or the TAB key can be used. Input fields can be closed by pressing ESC, ENTER, or any cursor key. Only pressing ESC will not save the changes made in there. The keyboard setup When running dhex for the first time, without any configfile present, or with the parameter -k, the first screen shown is that of the key- board setup. In this screen, the program asks the user to press certain keys. Which are (in order) ESCAPE, F1, F2, F3, F4, F5, F6, F7, F8, F9, F10, BACKSPACE, DEL, ENTER, TAB, UP, DOWN, RIGHT, LEFT, PG UP, PG DOWN, HOME, END. It also tells the user what it intends to do with those keys later. So the user can decide on any alternative he chooses. If he does not want to bind a specific function to a certain key, he can simply press ESCAPE and skip to the next question. After pressing all the keys, the user can chose whether or not to write those keys into the config file. The main screen The main screen is broken down into three columns: The first column contains the offset within the file for the current line. The second col- umn contains the bytes in the file in hex format. Finally, the third coumn contains the same bytes, but this time in ascii format. If a byte is not printable, it is being substituted with a '.'. How many bytes are are being shown in a line depends on the width of the terminal. For example, if the terminal is 80 characters wide, 16 bytes are being shown in each line. If no other [-o] or [-a] parameter was given at start time, the cursor is being set to offset 0. It is also being shown in the hex column. Here, it can be moved with the cursor keys. When entering a hexadecimal number, the file is being edited. The file can be edited in the ascii column as well, simply by pressing the TAB key (or whichever key was substituted for it in the keyboard setup). Pressing TAB again will return the cursor to the hex column. Pressing F9 (or its substitute) will undo the last of the changes. Changes are being shown in a differnt color. Editing is not possible in the diff mode. Here, pressing the cursor keys will move both files synchronously. The goto screen Pressing F1 (or its substitute) will open the Goto... screen. Here, it is possible to jump to a specific address directly, without the need of scrolling there with the cursor keys. The address can be entered in the 'To' field, either absolute or relative (to leap over a specified amount of bytes). An absolute address is being chosen by pressing '=', and a relative one by pressing '+' or '-', for a positive or negative leap respectively. Regardless of the adressing mode, it has to be entered as a hexadecimal number. It is also possible to set up to ten bookmarks in this screen: Pressing '0'...'9' will select one of them. Moving the cursor to "Set" and pressing ENTER will alter one of those book marks. The "Diff:" fields are showing the difference between the actual cursor position and the bookmark. Bookmarks can be stored and loaded, for this there are the "Save Markers" and "Load Markers" items on the bottom. Upon selecting one of them, the user is being prompted for a filename. It is possible to load a marker file at start time, by providing the [-m markerfile] parameter. dhex_markers(5) describes the format of the marker files. Searching Pressing F2 (or its subsitute) will open the Search... screen. Here, a short string can be entered (either in hex, or in ascii). If no log- files are being selected, the cursor will jump to the next occurance of this search string upon selection of "Go". It can be chosen if the search is supposed to be conducted forward of backwards. To jump to the next occurance, F3 (or its substitute) has to be pressed. To jump to the previous one, F4 has to be pressed. The search itself wraps at the edges of the file, meaning that when it reaches the end, it will start from the beginning and vice versa. Searchlogs are an advanced way of searching: Writing to the searchlog does not jump the cursor from one occurance to the next. Instead, it will write the offsets of all of them into the logfile. Their format is described in dhex_searchlog(5). Reading from this searchlog means that the search does not cover the whole file: Only the addresses which have been provided in this file are being searched. Thus it is possible to search for specific changes. Like for example the number of lives stored in the save file of a game. dhex_searchlog(5) describes the format of the searchlog. HexCal Pressing F5 (or its substitute) will open a small 64 bit calculator. This calculator is capable of not only performing arithmetic operations (+, -, *, /, modulo), but also logic ones. (and, or, xor, shift). There are three columns to enter numbers as hexadecimals, decimals or in binary format. Pressing 'x' will close this screen. Correlation When dhex(1) is running in diff mode, pressing F6 (or its substitute) will open the dialog for file correlation. This will try to find the optimal offset between the two files. There are three algorithms available for finding this offset: Searching for the best match (as many bytes as possible are the same), the longest match (as many consecutive bytes match as possible), or the minimum difference (as little dif- ferences between the bytes as possible). Even though it seems like the same at first, looking for the minimum difference is in fact faster. This can be improved even more, if the user sets an upper difference limit. Upon selecting Go, the program will search for the optimal offset. This will take some time. Saving and quitting Pressing F10 (or its substitute) will close dhex. In case there have been changes made to the file, a save dialog opens up. Here, it is pos- sible to select whether or not to write the changes back into the file. FILES
$HOME/.dhexrc: The default location of the config file. If the $HOME-variable is not set, its location has to be provided manually. BUGS
Report bugs to <dettus@dettus.net>. Make sure to include DHEX somewhere in the subject. AUTHOR
Written by Thomas Dettbarn SEE ALSO
dhexrc(5), dhex_markers(5), dhex_searchlog(5) BSD
May 12, 2012 BSD
All times are GMT -4. The time now is 01:34 PM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy