Visit Our UNIX and Linux User Community


How to export the string to a text file.


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting How to export the string to a text file.
# 1  
Old 08-24-2012
How to export the string to a text file.

Hi,

I have used this
Code:
 find . -type f -exec grep "491629461544"  {} \; > test.txt

to find the string and export it to file.

The problem is, when i used the above command in Shell script, the file is created but the searched string is recursively written in that file and even the file size exceeds 1 GB.

so, how to write the obtained string only once in newly created file.


Please Suggest me.

Last edited by nanthagopal; 08-24-2012 at 12:22 PM..
# 2  
Old 08-24-2012
assuming I understand:
Code:
find . -type f -exec grep "491629461544"  {} \; | head -1 | > test.txt

# 3  
Old 08-24-2012
I'm only curious, is there a difference between
Code:
find . -type f -exec grep "491629461544"  {} \; | head -1 | > test.txt

and
Code:
find . -type f -exec grep "491629461544"  {} \; | head -1 > test.txt

(without the last pipe)
# 4  
Old 08-24-2012
Does this even work?
Quote:
Code:
find . -type f -exec grep "491629461544"  {} \; | head -1 | > test.txt

I would think that the output of head is piped to a null process which doesn't output anything and thus test.txt should be empty.


--- edited out --- My head was in the sand the other night and the question I posted was just wrong.

Last edited by agama; 08-25-2012 at 11:40 AM.. Reason: rephrase
# 5  
Old 08-25-2012
Still I'm curious WHY that find didn't stop and test.txt is filled beyond belief. find is working on the current dir, in which test.txt is created as well. So grep could read that file, match the string in it, and output to it ad nauseam. But - at least my - grep refuses to do so:
Code:
grep: input file `./test.txt' is also the output

even if executed in a script. I'd like to see more of that script.
# 6  
Old 08-25-2012
Quote:
Originally Posted by RudiC
Still I'm curious WHY that find didn't stop and test.txt is filled beyond belief. find is working on the current dir, in which test.txt is created as well. So grep could read that file, match the string in it, and output to it ad nauseam. But - at least my - grep refuses to do so:
Code:
grep: input file `./test.txt' is also the output

even if executed in a script. I'd like to see more of that script.
It seems that grep is a built-in to your shell. There's no way otherwise that it would know that the file to search was also the target of redirection.

I just executed the original example find across a small set of files from which only one line out of the files matched the pattern. The output ended up with two lines. Adding the -H parameter to grep shows that the first line in the output file comes from the original file, and the second is tagged with the name of the output file.

---------- Post updated at 10:55 ---------- Previous update was at 10:51 ----------

I think this might be a better solution -- more efficient in terms of processes.

Code:
find . -type f  ! -name test.txt |  xargs grep "491629461544"  >test.txt

This User Gave Thanks to agama For This Post:
# 7  
Old 08-25-2012
Quote:
Originally Posted by agama
It seems that grep is a built-in to your shell. There's no way otherwise that it would know that the file to search was also the target of redirection.
I was thinking about this as well, but -
Code:
grep --v
grep (GNU grep) 2.10

- it's a file in /bin and not a built-in. It must be using functionality like lsof to determineits own open files in the target directory?!
I could reproduce what you posted by adding a pipe to cat; now the output file contains what you post, i.e. one line referring to itself.

I still wonder why nanthagopal's disk fills up, though ...

Last edited by RudiC; 08-25-2012 at 12:18 PM..

Previous Thread | Next Thread
Test Your Knowledge in Computers #349
Difficulty: Easy
AWK was originally written in 1977 and distributed with Version 7 Unix.
True or False?

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

If the 1th column of file f1 and file f2 is the same, then export those line with maximum string of

please help to write a awk command-line programs to achieve the following functions: Thank in advance. Requeset Description: compare two files f1 and f2, export to file f3: 1 Delete duplicate rows of in file f1 and file f2 2 If the 1th column of file f1 and file f2 is the same, then export... (1 Reply)
Discussion started by: weichanghe2000
1 Replies

2. UNIX for Beginners Questions & Answers

Search a string and display its location on the entire string and make a text file

I want to search a small string in a large string and find the locations of the string. For this I used grep "string" -ob <file name where the large string is stored>. Now this gives me the locations of that string. Now how do I store these locations in a text file. Please use CODE tags as... (7 Replies)
Discussion started by: ANKIT ROY
7 Replies

3. Shell Programming and Scripting

Read in search strings from text file, search for string in second text file and output to CSV

Hi guys, I have a text file named file1.txt that is formatted like this: 001 , ID , 20000 002 , Name , Brandon 003 , Phone_Number , 616-234-1999 004 , SSNumber , 234-23-234 005 , Model , Toyota 007 , Engine ,V8 008 , GPS , OFF and I have file2.txt formatted like this: ... (2 Replies)
Discussion started by: An0mander
2 Replies

4. Shell Programming and Scripting

Search a string in a text file and add another string at the end of line

Dear All I am having a text file which is having more than 200 lines. EX: 001010122 12000 BIB 12000 11200 1200003 001010122 2000 AND 12000 11200 1200003 001010122 12000 KVB 12000 11200 1200003 In the above file i want to search for string KVB... (5 Replies)
Discussion started by: suryanarayana
5 Replies

5. Shell Programming and Scripting

Search a string in a text file and add another string at the particular position of a line

I am having a text file which is having more than 200 lines. EX: 001010122 12000 BIB 12000 11200 1200003 001010122 2000 AND 12000 11200 1200003 001010122 12000 KVB 12000 11200 1200003 In the above file i want to search for string KVB and add/replace... (1 Reply)
Discussion started by: suryanarayana
1 Replies

6. Shell Programming and Scripting

Awk to convert a text file to CSV file with some string manipulation

Hi , I have a simple text file with contents as below: 12345678900 971,76 4234560890 22345678900 5971,72 5234560990 32345678900 71,12 6234560190 the new csv-file should be like: Column1;Column2;Column3;Column4;Column5 123456;78900;971,76;423456;0890... (9 Replies)
Discussion started by: FreddyDaKing
9 Replies

7. Shell Programming and Scripting

Read the text file and connect to DB then export the DB

hi, i'm new to shell scripting. I have a text file(sample.txt). It contains the different username & password for different users of oracle db. .txt content user1|password1 user2|password2 user3|password3 user4|password4 First read the text file and take the username and password then... (5 Replies)
Discussion started by: priya001
5 Replies

8. Shell Programming and Scripting

find string(s) in text file and nearby data, export to list help

Hi, So I'm kinda new to shell scripts and the like. I've picked up quite a bit of use from browsing the forums here but ran into a new one that I can't seem to find an answer for. I'm looking to parse/find a string AND the next 15 or so charachters that follow the string within a text file... (1 Reply)
Discussion started by: kar23me
1 Replies

9. UNIX for Dummies Questions & Answers

Output text from 1st paragraph in file w/ a specific string through last paragraph of file w/ string

Hi, I'm trying to output all text from the first paragraph in a file that contains a specific string through the last paragraph in that file that contains that string. Previously, I was outputting just each paragraph with that search string with: cat in_file | nawk '{RS=""; FS="\n";... (2 Replies)
Discussion started by: carpenn
2 Replies

10. Shell Programming and Scripting

appending string to text file based on search string

Hi, I need to append string "Hi" to the beginning of the lines containing some specific string. How can I achieve that? Please help. Malay (1 Reply)
Discussion started by: malaymaru
1 Replies

Featured Tech Videos