finding string in very long file without newlines


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers finding string in very long file without newlines
# 1  
Old 04-06-2010
finding string in very long file without newlines

What's the best way to find a string in a very long file without newlines in Unix? The standard utility I'm aware of for finding a string in a single file is grep, but for a long file without newlines, I think the output is just going to be the input. I suppose I could use sed to replace the string with another that catches my eye like "AAAAAAAAAAAAAAAAAAAAAAAAAAAAA," and search for it visually, but with an extremely large file that would be impractical.
# 2  
Old 04-06-2010
What if you just replaced the string with the original string + a newline?
# 3  
Old 04-06-2010
If I try typing sed "s/string/string\n" filename, I'll get "\n" inserted everywhere I want the newline, but they'll be interpreted as a literal "\" and literal "n" by grep, not as newlines, so the file won't get broken into lines, and the grep output will be the entire file. Do you have another method for inserting newlines automatically. In a large file, manual insertions would be time consuming and probably inaccurate.
# 4  
Old 04-06-2010
append newline after every 80 character as...

Code:
sed 's/\(.\{80\}\)/\1\n/g' inputfile

And that is not a literal \ and n, it is new line. Also if you wanted to see particular string instead of whole line as output, use -o option in grep.

Code:
grep -o 'string' file

# 5  
Old 04-07-2010
Quote:
Originally Posted by thegeek
Code:
sed 's/\(.\{80\}\)/\1\n/g' inputfile

This will surely work, but isn't

Code:
fmt -80 inputfile

a bit easier? After all, this was what "fmt" was designed for, no?

bakunin
# 6  
Old 05-25-2010
Got something to work

When I tried fmt as shown above, the output is the input. But, the following works, breaking up the lines in test-d at occurrences of "sd" (> is a command prompt):
sed '
> s/sd/\
> /g' test-d >> test-d2

test-d2 will contain the broken lines.

Thanks for all you help.

---------- Post updated at 05:14 PM ---------- Previous update was at 04:37 PM ----------

Note to others with this problem: This solution seems to work in bash, not in tcsh, but maybe in other shells.
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Finding files with newlines in filename

I want to use grep to find files that have newlines in the filename. For example, I have a directory where I create three files: $ touch file1 $ touch "file 2" $ touch "file > with > newlines" $ find . ./file 2 ./file1 ./file?with?newlinesI now want to pipe the find output into grep and... (4 Replies)
Discussion started by: Ralph
4 Replies

2. UNIX for Beginners Questions & Answers

Finding specific string in file and storing in another file

Text in input file is like this <title> <band height="21" isSplitAllowed="true" > <staticText> <reportElement x="1" y="1" width="313" height="20" key="staticText-1"/> <box></box> <textElement> <font fontName="Arial" pdfFontName="Helvetica-Bold"... (4 Replies)
Discussion started by: aankita30
4 Replies

3. UNIX for Beginners Questions & Answers

Finding largest files takes too long

Good evening. because the folder has thousand of files it takes too long and have some trouble to get the largest files and then compress files or delete it, for instance find . -size +10000000c -exec ls -ld {} \; |sort -k5n | grep -v .gz The above commad took an hour and i have to cancel... (10 Replies)
Discussion started by: alexcol
10 Replies

4. Shell Programming and Scripting

Finding a string in a file

Hello All, I have a text file, i want to search for a string in it and the string is repeated multiple times in a file i want to get the first occurence of the string in a variable. the content of file is like: I want to grepthe first occurance of "Configuration flow done" and store the... (7 Replies)
Discussion started by: anand2308
7 Replies

5. UNIX for Dummies Questions & Answers

Finding files with a certain name string inside of another file.

Hi, I have a very large file that contains a listing of all files on the system. I need to create a listing from that file of all files that start with the following format: s???_*, whereas the '?' represents characters, so the file name begins with an 's' followed by three other characters and... (4 Replies)
Discussion started by: tes218
4 Replies

6. Shell Programming and Scripting

Need help with finding unique string in log file

Shell script help Here is 3 sample lines from a log file <date> INFO <java.com.blah> abcd:ID= user login <date> DEBUG <java.com.blah> <nlah bla> abcd:ID=123 user login <date> INFO <java.com.blah> abcd:ID=3243 user login I want to find unique "ID" from this log... (3 Replies)
Discussion started by: gubbu
3 Replies

7. Shell Programming and Scripting

Finding out how long a script runs for and exit reason.

I am running a daemon program that sends texts via a connected mobile phone. I run this daemon via CLI, and it loops a few commands (checking to see if there are any new texts). It runs perfectly, the problem is, when I leave this to run on my Ubuntu Desktop, and come back to it hours later it... (2 Replies)
Discussion started by: daydreamer
2 Replies

8. UNIX for Advanced & Expert Users

finding a string in a file

hi all.. I dont know how to search for a string in a file.. I have tried doing.. I did google but didnt get effective answers..my code is as follows: int search(char* filename,const char* username,const char* passwd) { int flag=0; unsigned long fsize=0; unsigned long current=0;... (2 Replies)
Discussion started by: Ume1986
2 Replies

9. Shell Programming and Scripting

Finding a certain string on each line in a file

Hi, I need a script to get every line from a file where there are less then 17 ; on a line. Thank's (5 Replies)
Discussion started by: VODAFUN
5 Replies

10. UNIX for Dummies Questions & Answers

Finding out how long a command takes to run

Hi I am trying to find out the best way to find out how long a command takes to run in miliseconds .. Is there such a way of doing this in Unix ? Thanks (3 Replies)
Discussion started by: cfoxwell
3 Replies
Login or Register to Ask a Question